开发者

C, reading date and a number, and sorting

I cannot for the life of me figure out how to do this:

I am supposed to read this input in this format:

yyyy mm dd total(of sales)

i.e.

1990 11 22 6.89

1990 11 24 7.20

1991 2 4 5.36

... (all entries are assumed to be sorted by year, then month, then day)

Then I am to print the input values, subtotals for each month, subtotals for each year, and a grand total.

My question: How do I store the input values while keeping each entry consistent?? I have to be able to check if I am in the same year and month when calculating subtotals. And I need to access each float number in order to calculate subtotals.

Oh and I can only use stdio.h or create my own functions.

So how do I store them so that I can reference each value while kee开发者_开发问答ping them consistent??

Any help would be GREATLY appreciated. Thank you in advance.


You normally want to do things like this with a struct:

struct sale {
    int year;
    int month;
    int day;
    double total;
};

You can then create an array of sale items, and refer to an individual field like: sales[i].year == sales[i+1].year.


Well, you might declare a struct containing an int for the year, two chars for the month and day, and and a float for the value. Then create an array of these structs. This is easier if you know ahead of time how many lines there are going to be in the file. Then read the data into the structs, and then it's a simple matter of running through the array to calculate your totals.


Beyond what's already been said about how to store the resulting data, there's also the input parsing side of things.

For your specific objective (use only <stdio.h>), you'd need to do something like:

struct sale *s;
for (s = sales; !feof(stdin); s++) {
    if (fscanf(stdin, "%4u %2u %2u %f\n",
        &s->year, &s->month, &s->day, &s->total) != 4) {
            error("Parsing error on <stdin>");
    }
    if (!date_is_valid(s))
        error("Invalid date given");
}

But error handling / invalid format detection is easier if using existing functionality made for the purpose:

Standard C (UN*X / POSIX, rather), has the function strptime() (in <time.h> which is a standard include file) for this purpose. The advantage of using this instead of rolling your own is that it can parse a wide variety of formats, checks for date validity (you know, like month/day mixups that happen when dealing with american and european customers) and returns you the date in commonly used datastructures (struct tm).

That's another topic, though...


works only for sorted years AND months:

  char line[100],lasty[100]="",lastm[100]="",y[100],m[100];
  double subm,suby,x;
  while( fgets(line,100,yourfilepointer) )
  {
    if( 3==sscanf(line,"%s%s%*s%lf",y,m,&x) )
    {
      if( strcmp(lastm,m) )
      {
        if( *lastm ) printf("\n%s %s =%f",lasty,lastm,subm);
        strcpy(lastm,m); subm=x;
        if( strcmp(lasty,y) )
        {
          if( *lasty ) printf("\n%s =%f",lasty,suby);
          strcpy(lasty,y); suby=x;
        }
        else
          suby+=x;
      }
      else
        subm+=x,suby+=x;
    }
  }
  if( *lastm ) printf("\n%s %s =%f",lasty,lastm,subm);
  if( *lasty ) printf("\n%s =%f",lasty,suby);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜