c語言,讀取文件內容 並統計
50
Year,USA,Japan,UK,China,Germany,France,Singapore,Netherlands1991, 258, 44, 75,0, 30, 25,0, 311992, 206, 74, 73,0, 28, 25,0, 241993, 182, 88, 81, 67, 26, 21,0, 261994, 1 78, 91, 91, 62, 35, 26, 6, 221995, 198, 116, 94, 71, 33, 28, 9, 281996, 188, 122, 90, 85, 40, 26, 10, 301997, 219, 119, 84, 113, 51, 35, 18, 251998, 194, 109, 95, 70, 59, 38, 17, 271999, 205, 114, 82, 69, 55, 36, 20, 322000, 212, 127, 81, 69, 50, 28, 21, 312001, 221, 160, 90, 70, 56, 43, 25, 482002, 233, 159, 80, 96, 52, 35, 26, 392003, 242, 168, 86, 84, 56, 44, 22, 382004, 256, 198, 105, 106, 67, 47, 35, 462005, 262, 204, 115, 107, 75, 49, 45, 542006, 295, 212, 114, 112, 76, 55, 44, 482007, 298, 232, 124, 93, 76, 56, 43, 502008, 311, 238, 119, 95, 77, 59, 46, 502009, 289, 224, 115, 96, 74, 66, 43, 542010, 288, 224, 113, 99, 72, 62, 41, 52
除第一行,其他每行的開頭是年份,統計每行的總和,並算出哪年最大。輸出結果應該像下面這樣:Year Sum2008 9952009 9612010 951 (要輸出全部年份)Year of Maximum Regional Offices = 2008Number of Regional Offices = 995
補充:Year,USA,Japan,UK,China,Germany,France,Singapore,Netherlands
1991, 258, 44, 75,0, 30, 25,0, 31
補充:1992, 206, 74, 73,0, 28, 25,0, 24
1993, 182, 88, 81, 67, 26, 21,0, 26
1994, 1 78, 91, 91, 62, 35, 26, 6, 22
1995, 198, 116, 94, 71, 33, 28, 9, 28
1996, 188, 122, 90, 85, 40, 26, 10, 30
1997, 219, 119, 84, 113, 51, 35, 18, 25
1998, 194, 109, 95, 70, 59, 38, 17, 27
1999, 205, 114, 82, 69, 55, 36, 20, 32
2000, 212, 127, 81, 69, 50, 28, 21, 31
2001, 221, 160, 90, 70, 56, 43, 25, 48
2002, 233, 159, 80, 96, 52, 35, 26, 39
2003, 242, 168, 86, 84, 56, 44, 22, 38
2004, 256, 198, 105, 106, 67, 47, 35, 46
2005, 262, 204, 115, 107, 75, 49, 45, 54
2006, 295, 212, 114, 112, 76, 55, 44, 48
2007, 298, 232, 124, 93, 76, 56, 43, 50
2008, 311, 238, 119, 95, 77, 59, 46, 50
2009, 289, 224, 115, 96, 74, 66, 43, 54
2010, 288, 224, 113, 99, 72, 62, 41, 52
補充:問題的格式不正確 ,應該是像補充裡面的那樣的!
除第一行,其他每行的開頭是年份,統計每行的總和,並算出哪年最大。輸出結果應該像下面這樣:
Year Sum
2008 995
2009 961
2010 951 (要輸出全部年份 1991-2010)
Year of Maximum Regional Offices = 2008
Number of Regional Offices = 995
最佳回答:
#include <stdio.h>
int main()
{
FILE* file;
int year,nums[8],myear,msum=0;
file=fopen("data.txt","r");
if(file)
{
fscanf(file,"Year,USA,Japan,UK,China,Germany,France,Singapore,Netherlands\n");
puts("Year Sum");
while(fscanf(file,"%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
&year,&nums[0],&nums[1],&nums[2],&nums[3],&nums[4],&nums[5],&nums[6],&nums[7])==9)
{
int i,sum=0;
for(i=0;i<8;i++)
sum+=nums[i];
printf("%d %d\n",year,sum);
if(sum>msum)
{
myear=year;
msum=sum;
}
}
if(msum>0)
printf("Year of Maximum Regional Offices = %d\nNumber of Regional Offices = %d\n",myear,msum);
fclose(file);
}
return 0;
}