对于作业,我们应该从文本文档中读取日期列表和相关值(温度)。每行中的值用逗号分隔。
示例数据:
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已经从前一行(新月)改变并执行某个动作(例如:计算)
对不起,如果这令人困惑!
我们还没有教过数组,只有操作,循环和函数。
答案 0 :(得分:2)
首先,我不认为你想要不同月份的“单独变量”。为什么不是按月索引的数组(即mm)?这可能会使您的代码长度减少一个数量级。哦,你还没有被教过阵列。
其次,在循环的顶部设置'last_mm'变量。这将是您上次循环时的mm值。将其初始化为-1或其他内容。当last_mm和mm不同时,您知道月份已更改。在循环的底部,将last_mm设置为mm。
通常,这是在循环期间检测更改的方法。
答案 1 :(得分:0)
保持某个月的上个月价值;完成月度相关计算并在您阅读的月份高于上个月时重置月度计数器
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;
}
}
答案 2 :(得分:0)
以下代码未经测试,但是这样的代码应该可以为您提供没有数组的技巧。我希望你知道开关/箱子。
//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
}