检测 scanf 读取的值的变化
对于作业,我们应该从文本文档中读取日期和相关值(温度)的列表。每行中的值以逗号分隔。
示例数据:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我认为您不需要不同月份的“单独变量”。为什么不是一个按月份(即 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.
将上个月的值保留在某处;当您正在读取的月份高于上个月时,完成每月相关计算并重置每月计数器
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
以下代码未经测试,但类似的代码应该可以在没有数组的情况下为您解决问题。我希望你已经知道 switch/case 了。
The following code is untested, but something like this should do the trick for you without arrays. I hope you know switch/case already.