检测 scanf 读取的值的变化

发布于 2024-10-27 14:07:36 字数 823 浏览 2 评论 0原文

对于作业,我们应该从文本文档中读取日期和相关值(温度)的列表。每行中的值以逗号分隔。

示例数据:

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

到目前为止,我已经实现了一个循环来读取每一行:

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

我们假设文档中没有错误,并且没有重复的日期。

但是,可能会缺少条目(并非每个月都有完整的数据;缺少日期)。

我无法检测每个月的数据 (mm) 是完整的月份还是仅部分月份。

例如:2011 年 3 月有 31 天。如果我从 3 月开始有 31 个条目,我需要打印“完整月份”,否则如果缺少天数,我必须打印“部分月份”。

到目前为止,我一直在使用 if(mm==1){} 语句在 while(scanf(...)) 循环内分隔每个月,然后递增它们在单独的变量中,然后将其与完整月份的天数进行比较,但我不知道如何实现它,因此它检测到 mm 已从上一行(新月份)更改并执行特定操作(例如:计算)

抱歉,如果这令人困惑!

我们还没有学过数组,只学过运算、循环和函数。

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 days).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风挽心 2024-11-03 14:07:36

首先,我认为您不需要不同月份的“单独变量”。为什么不是一个按月份(即 mm)索引的数组?这可能会将您的代码长度减少一个数量级。哦,你还没有学过数组。

其次,在循环的顶部设置一个“last_mm”变量。这将是上次循环时 mm 的值。将其初始化为-1或其他值。当last_mm和mm不同时,你就知道月份已经改变。在循环的底部,将 last_mm 设置为 mm。

一般来说,这是检测循环期间变化的方法。

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.

固执像三岁 2024-11-03 14:07:36

将上个月的值保留在某处;当您正在读取的月份高于上个月时,完成每月相关计算并重置每月计数器

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;
    }
} 

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;
    }
} 
停顿的约定 2024-11-03 14:07:36

以下代码未经测试,但类似的代码应该可以在没有数组的情况下为您解决问题。我希望你已经知道 switch/case 了。

//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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文