2 shit 2012 shit_2012 于 2014.08.05 13:24 提问

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

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);
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!