shit_2012 2014-08-05 05:24
浏览 2535

结构体数组中有数组成员,怎么对结构体数组查询并追加,追加至上限,删除最老记录

int dataNumber=0;
struct Target{
int Mjd; // Mjd
int targetNo; // Id of Target, 0: Sun
double xpos, ypos, ut1_utc;
double appra[49],appdec[49],gast[49],t[49];
time_t timestamp;
}target[100];

int calcSun(int year, int month, int day, double hour, struct Target *t)
{

int i;
time_t timep;
t->timestamp=time(&timep);
printf("timestamp=%d\n",timep);
/*
 Make structures of type 'on_surface' and 'observer-on-surface' containing
 the observer's position and weather (latitude, longitude, height,
 temperature, and atmospheric pressure).
 */

make_on_surface (latitude,longitude,height,temperature,pressure, &geo_loc);

make_observer_on_surface (latitude,longitude,height,temperature,pressure,
                          &obs_loc);

/*
 Make structures of type 'object' for the Mercury....Sun,Moon.
 */
make_cat_entry ("DUMMY","xxx",0,0.0,0.0,0.0,0.0,0.0,0.0,&dummy_star);

    if ((error = make_object (0,10,"sun",&dummy_star, &sun)) != 0)
    {
            printf ("Error %d from make_object (sun)\n", error);
            return (error);
    }

get_leap_sec(MJD,0.,&leapsecondt);
//printf("Leap: %lf\n",leapsecondt);
if (!get_iers_data(MJD, &ut1_utc, &xx, &yy))
{
printf("Error retrieve IERS Data\n");
exit(1);
};
t->xpos = xx;
t->ypos = yy;
t->ut1_utc = ut1_utc;
hour=0.;
for(i=0;i<49;i++)
{
jd_utc = julian_date (year,month,day,hour);
jd_tt = jd_utc+((double) leapsecondt + 32.184) / 86400.0;
jd_ut1 = jd_utc + ut1_utc / 86400.0;
delta_t = 32.184 + leapsecondt - ut1_utc;

    /*
     Apparent and topocentric place of the SUN.
     */

    if ((error = app_planet (jd_tt,&sun,accuracy, &ra,&dec,&dis)) != 0)
    {
        printf ("Error %d from app_planet.", error);
        return (error);
    }

    /* if ((error = topo_planet (jd_tt,&sun,delta_t,&geo_loc,accuracy,
     &rat,&dect,&dist)) != 0)
     {
     printf ("Error %d from topo_planet.", error);
     return (error);
     }
     */

    /*
     Greenwich and local apparent sidereal time and Earth Rotation Angle.
     */

    if ((error = sidereal_time (jd_ut1,0.0,delta_t,0,1,accuracy, &gast)) != 0)
    {
        printf ("Error %d from sidereal_time.", error);
        return (error);
    }
    t->appra[i]=ra;
    t->appdec[i]=dec;
    t->gast[i]=gast;
    t->t[i]=hour;
    //printf("appra[%d]=%.15f appdec[%d]=%.15f gast[%d] =%.15f t[%d]=%.15f\n",i,t->appra[i],i,t->appdec[i],i,t->gast[i],i,t->t[i]);
    hour=hour+0.5;
    }

}
// Query OK.
FILE *fp;
fp=fopen("ephpos.dat","r+");
char buffer[1024];

if(fp==NULL)
{
 printf("file open failed !\n");
 exit(0);
}
while(fgets(buffer,sizeof(buffer),fp) ) {

dataNumber++;
//printf("buffer=%s\n",buffer);
fseek(fp,0,0);
printf("dataNumber=%d\n",dataNumber);
for(i=0;i<dataNumber;i++)
   {

      fscanf(fp,"%d %d %lf %lf %lf %lf %lf %lf %lf %lf\n",&target[i].targetNo,&target[i].Mjd,&target[i].xpos,&target[i].ypos,
                    &target[i].ut1_utc,&target[i].appra[i],&target[i].appdec[i],&target[i].gast[i],&target[i].t[i]);
      printf("%d %d %.15f %.15f %.15f %.15f %.15f %.15f %.15f %.15f\n",target[i].targetNo,target[i].Mjd,target[i].xpos,target[i].ypos,
                    target[i].ut1_utc,target[i].appra[i],target[i].appdec[i],target[i].gast[i],target[i].t[i]);
      //getchar();
      if (target[i].targetNo == obsTarget &&target[i].jd ==JD)
        {
            printf("Query ok!\n");

            sprintf(result,"%d %.15f %.15f %.15f %.15f %.15f %.15f %.15f %.15f\n",target[i].Mjd,target[i].jd,target[i].xpos,target[i].ypos,
                    target[i].ut1_utc,target[i].appra[i],target[i].appdec[i],target[i].gast[i],target[i].t[i]);
            printf("Result:%s\n",result);
            exit(0);
        }

   }

// Query failed,compute from calcsun.

if(i==dataNumber)
  {
     printf("Query failed!\n");
     //printf("i=%d dataNumber=%d\n",i,dataNumber);
     calcSun(year, month, day, tt,&target[dataNumber++]);

     printf("Insert a new record!\n");

     fprintf(fp,"%d %d %.15f %.15f %.15f %.15f %.15f %.15f %.15f %.15f\n",obsTarget,MJD,JD,target[i].xpos,target[i].ypos,
                    target[i].ut1_utc,target[i].appra[i],target[i].appdec[i],target[i].gast[i],tt);
     printf("%d %d %.15f %.15f %.15f %.15f %.15f %.15f %.15f %.15f\n",obsTarget,MJD,JD,target[i].xpos,target[i].ypos,
                    target[i].ut1_utc,target[i].appra[i],target[i].appdec[i],target[i].gast[i],tt);
     //sprintf(result,"target[%d]=%d target[%d]=%.15f target[%d]=%.15f target[%d]=%.15f target[%d]=%.15f target[%d]=%.15f target[%d]=%.15f target[%d]=%.15f target[%d]=%.15f\n",i,MJD,
           //i,JD,i,target[i].xpos,i,target[i].ypos,i,target[i].ut1_utc,i,target[i].appra[i],i,target[i].appdec[i],i,target[i].gast[i],i,target[i].t[i]);
    // printf("Result:%s\n",result);
    //RA=parabola(target[i].appra,target[i].t,49,tt);
   // DEC=parabola(target[i].appdec,target[i].t,49,tt);
   // GAST=parabola(target[i].gast,target[i].t,49,tt);
    //sprintf(result,"%d %.15f %.15f %.15f %.15f %.15f %.15f %.15f\n",MJD,JD,target[i].xpos,target[i].ypos,target[i].ut1_utc,
               // RA,DEC,GAST);
    //printf("Result:%s\n",result);
    // select the long timestamp to update data
    if(dataNumber>100)

    printf("trying delete the long timestamp data in the target array...\n");
    time_t m=target[1].timestamp;
    for(i=1;i<101;i++)
    {
          if(target[i].timestamp<=m);
          m=target[i].timestamp;
    }
   target[i]=target[dataNumber++];
  }
fclose(fp);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 关于#python#的问题:自动化测试
    • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题