二級B上級模擬試題及答案(2)

二級B上級模擬試題及答案(2),第1張

二級B上級模擬試題及答案(2),第2張

請編譯函數readdat()將文件fc.in中的1000個十進制整數讀入數組xx;請編譯函數compute()計算xx中的奇數、偶數、aver平均值和方差totfc,最後使用函數writedat()將結果輸出到fc1.out文件。
計算方差的公式如下:
1N
到TFC =──∑(xx-aver)2
n I = 1
存儲原始數據文件的格式爲:每行存儲10個數字,用逗號隔開
。(每個數字大於0小於等於2000)
注意:有些源程序存儲在prog1.c中
不要改變主函數main()和輸出數據函數writedat()的內容。

# include
# include
# include
#定義最大1000

int xx[max],奇數= 0,偶數= 0;
double aver = 0.0,totfc = 0.0
void write dat(void);

int read dat(void)
{
file * FP;
int i,j;
char c,strtot fc/= max;
};

if((fp = fopen("fc.in"," r")) == null)返廻1;


for(I = 0;i < maxi )
{
j = 0;

while((c = (char) fgetc(fp))!= eof)
{
if(c == ',')
{
str[j]= ' \ 0 ';
break;
}

else if(c!= '\n' && c!= '\r')
{
str[j]= c;
j;
}

}

xx = atoi(str);

if(c = = eof)
break;
}

fclose(FP);
返廻0;
}

void compute(void)
{
int I;
long count = 0;

for(I = 0;i < maxi )
{
if(xx & 1)
odd ;
else
even ;

count = xx;
}

aver =(double)count/max;

for(I = 0;i < maxi )
totfc =(xx-aver)*(xx-aver);

[20]

void main()
{
int I;

for(I = 0;i < maxi )xx = 0;
if(read dat()){
printf("數據文件fc.in無法打開!\ 007 \ n");
退貨;
}
compute();
printf(" odd = % d \ n even = % d \ naver = % lf \ ntotfc = % lf \ n",odd,even,aver,totfc);
write dat();
}

void writedat(void)
{
file * FP;
int I;

fp = fopen("fc1.out"," w");
fprintf(fp," %d\n%d\n%lf\n%lf\n",odd,even,aver,totfc);
f close(FP);
}

位律師廻複

生活常識_百科知識_各類知識大全»二級B上級模擬試題及答案(2)

0條評論

    發表評論

    提供最優質的資源集郃

    立即查看了解詳情