开发者

Detecting change in value read by scanf

开发者 https://www.devze.com 2023-02-20 08:10 出处:网络
For an assignment we are supposed to read in a list of dates and associated values (temperatures) from a text document. Values in each line are separated by commas.

For an assignment we are supposed to read in a list of dates and associated values (temperatures) from a text document. Values in each line are separated by commas.

Example data:

dd,mm,yyyy,x,y,z,w
01,01,2011,1.1,5.2,6.5,7.5
02,01,2011,2.1,5.2,6.1,1.1
03,01,2011,4.5,2.5,6.1,2.1
...
30,01,2011,4.1,6.1,6.1,2.1
01,02,2011,2.5,6.1,7.1,6.3

So far I have implemented a loop to read each line:

while(scanf("%d,%d,%d,%f,%f,%f,%f", &dd, &mm, &yyyy, &x, &y, &z, &w) == 7)
{
}

We're given the assumption that there are no errors in the document, and no duplicate dates.

However, there may be missing entries (not each month have complete data; missing day开发者_如何学Cs).

I am having trouble detecting if each month's data (mm) is a complete month or only a partial month.

Eg: 31 days in March 2011. If I have 31 entries from March, I need to print 'Full month', otherwise if there are missing days I have to print 'Partial month'.

So far I have been using if(mm==1){} statements to separate each month inside the while(scanf(...)) loop and then incrementing them in separate variables, then comparing it with the amount of days in a complete month, but I don't know how to implement it so it detects that mm has changed from the previous line (new month) and perform a certain action (e.g.: calculations)

Sorry if this is confusing!

We have not been taught arrays yet, only operations, loops and functions.


First of all, I don't think you want "separate variables" for the different months. Why not an array, indexed by month (i.e. mm)? This will probably reduce your code length by an order of magnitude. Oh, you haven't been taught arrays yet.

Second, at the top of the loop set a 'last_mm' variable. This will be the value of mm last time you went through the loop. Initialize it to -1 or something. When last_mm and mm are different, you know the month has changed. At the bottom of the loop, set last_mm to mm.

In general, this is the way to detect changes during loops.


keep prev month value somewhere; finalize monthly related computations and reset the monthly counters when the month you are reading is higher than prev month

int curmonth = 1;
int dd,mm,yyy;
float x,y,z,w;
float xtotal = 0,ytotal = 0,ztotal = 0,wtotal = 0;
while(scanf("%d,%d,%d,%f,%f,%f,%f", &dd, &mm, &yyyy, &x, &y, &z, &w))==7 {
    if (mm == curmonth) {
        //add to current counters
        ztotal+=z;
        ytotal+=y;
        xtotal+=y;
        wtotal+=w;
    } else {
        if (mm < curmonth) 
            //not the expected order as per specs so better die now
            exit(1);
        printf("month=%d,my_calculations=%f,%f,%f,%f\n",xtotal,ytotal,ztotal,wtotal);
        xtotal = ytotal = ztotal = wtotal = 0; curmonth=mm;
    }
} 


The following code is untested, but something like this should do the trick for you without arrays. I hope you know switch/case already.

//Keep track of which month we are fetching data for
int currentMonth = 1;
//Number of days for which data have been read for the current month
int numberOfDaysInCurrentMonth = 0;
//Full = 1 would represent if all data is available for currentMonth
int full = 0;
//This is set to 1 only when data is read for a month different to currentMonth
int monthChanged = 0;

while(scanf("%d,%d,%d,%f,%f,%f,%f", &dd, &mm, &yyyy, &x, &y, &z, &w) == 7)
{
    // If month changed in last iteration  
    if(monthChanged == 1) {
        //Check if full = 1 print "Full Month" to output
        if(full == 1) {
             printf("Full Month");
        //Else there was partial data 
        } else {
             printf("Partial Month");
        }

        //Once the output is on the screen set this back to 0, so it will be set to 
        //1 only when month changes next time.
        monthChanged = 0;
    }

    //If currentMonth is the same as mm, just add 1 to number of days in this month
    //for which data is provided  
    if(currentMonth == mm) {
        ++numberOfDaysInCurrentMonth;

    //otherwise
    } else { 
        switch(currentMonth) {
            //In case the month is January, March, May, July, August, October, December
            case 1, 3, 5, 7, 8, 10, 12:
                //Number of days should be 31 for full to be 1
                if(numberOfDaysInCurrentMonth == 31)
                     full = 1;
            break;
            //In case the month is February
            case 2:
                //Number of days should be 28 for month to be full 
                //(ignoring leap years)  
                if(numberOfDaysInCurrentMonth == 28)
                     full = 1;
            break;
            //In case the month is April, June, September, November
            case 4, 6, 9, 11:
                //Number of days should be 28 for month to be full 
                if(numberOfDaysInCurrentMonth == 30)
                     full = 1;
            break;     
        }
        //Now that we have set what we desired, set
        //currentMonth to mm 
        currentMonth = mm;
        //The month just changed, otherwise we would not have been in this part
        of the code
        monthChanged = 1;
        //Number of days in current month starts from 1
        numberOfDaysInCurrentMonth = 1;
    }

    //Do whatever you are doing with data here
}
0

精彩评论

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