南开第40题

来源:百度知道 编辑:UC知道 时间:2024/05/30 23:42:25
已知在IN.DAT中存有若干个(<200)四位数字的正整数,函数ReadDat()读取若干个正整数并存入数组xx中。请编写CalValue(),其功能是:1.求出文件一共有多少个正整数totNum;2.求这些数中各位数字之和是偶数的个数totCnt,以及满足此条件的这些数的算术平均值totPjz,最后调用writeDat()把所求的结果输出到文件OUT.DAT中。
注意:部分程序已经给出。
请勿改动主函数main(),读函数ReadDat()和写函数writeDat()的内容。
#include <stdio.h>
#include <conio.h>
#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;
for (i=0; i<MAXNUM; i++)
xx[i] = 0;
if (ReadDat())
{
printf("数据文件IN.DAT不能打开!\007\n");
return;
}
CalValue();
printf("文件IN.DAT中共有正整数=%d个\n", totNum);
printf("符合条件的正整数的个数=%d个\n", totCnt);
printf("平均值=%.2f\n", totPjz);
writeDat();
}
int ReadDat(void)
{
FILE *fp;
int i = 0;

if(xx[i]==0) break; //可能是这里,0也是偶数吧
5128.24*93=476 926.32
4630.35*103=476 926.05
改为
if(xx[i]==0)
{
totCnt++;
break;
}

if(xx[i]==0) break;
totNum++;
thou=xx[i]%10000/1000;
hun=xx[i]%1000/100;
ten=xx[i]%100/10;
data=xx[i]%10/1;
ab=thou+hun+ten+data;
这里判断偶数的方法对吗?