- 相關推薦
2006年計算機等級考試三級上機題庫練習及答案(5)
第9題:已知在文件in9.dat中存在若干個(<200)四位數字的正整數,函數readDat()讀取這若干個正整數并存入數組xx中。請編制函數CalValue(),其功能要求:(1)求出這個文件中共有多少個正整數totNum;(2)求這些數右移1位后,產生的新數是偶數的數的個數totCnt,以及滿足此條件的這些數(右移前的值)的算術平均值totPjz,最后調用函數writeDat()把所求的結果輸出到文件out9.dat中。
部分程序已經給出。
請勿改動主函數main()、讀函數readDat()和寫函數writeDat()的內容。
#include
#include
#define MAXNUM 200
int xx[MAXNUM];
int totNum=0;
int totCnt=0;
double totPjz=0.0;
int readDat(void);
void writeDat(void);
void CalValue(void)
{
}
void main()
{
int i;
clrscr();
for(i=0;iif(readDat())
{
printf("數據文件in9.dat不能打開!007n");
return;
}
CalValue();
printf("文件in9.dat中共有正整數=%d個n",totNum);
printf("符合條件的正整數的個數=%d個n",totCnt);
printf("平均值=%.2fn",totPjz);
writeDat();
}
int readDat(void)
{
FILE *fp;
int i=0;
if((fp=fopen("in9.dat","r"))==NULL)return 1;
while(!feof(fp))
{
fscanf(fp,"%d,",&xx[i++]);
}
fclose(fp);
return 0;
}
void writeDat(void)
{
FILE *fp;
fp=fopen("out9.dat","w");
fprintf(fp,"%dn%dn%6.2fn",totNum,totCnt,totPjz);
fclose(fp);
}
【答案】
void CalValue(void)
{int i,data;
for(i=0;i{
if(!xx[i]) break;
if(xx[i]>0) totNum++;
data=xx[i]>>1;
if(data%2==0)
{totCnt++;totPjz+=xx[i];}
}
totPjz/=totCnt;
}http://www.shddsc.com/
【計算機等級考試三級上機題庫練習及答案(5)】相關文章:
2004年全國計算機等級考試三級C語言全真上機試題解析08-10
計算機等級考試試題及答案08-09
全國計算機等級考試三級信息、網絡、數據庫上機編程題15道08-10
自考計算機上機基礎考試題及答案09-25
計算機三級網絡技術上機考試題08-05
2017年計算機等級考試試題及答案08-08
2017全國計算機等級考試試題及答案08-07
計算機一級考試題庫及答案08-09
計算機等級一級考試填空題及答案08-06