七、其他數(shù)學(xué)計算
請編制函數(shù)READDAT()實現(xiàn)從文件IN.DAT中讀取1000個十進(jìn)制整數(shù)到數(shù)組XX中;再
編制函數(shù)COMPUTE()分別計算出XX中奇數(shù)的個數(shù)ODD,偶數(shù)的個數(shù)EVEN,平均值`AVER以及方
差TOTFE的值,最后調(diào)用函數(shù)WRITEDAT()把結(jié)果輸出到OUT.DAT文件中.
計算方差的公式如下:
原始數(shù)據(jù)文件存放的格式是:每行存放10個數(shù),并用逗號隔開(每個數(shù)均大于0且小于等于2000).
#include
#include
#include
#define MAX 1000
int xx[MAX],odd=0,even=0;
double aver=0.0,totfc=0.0;
void WriteDat(void) ;
int ReadDat(void)
{
FILE *fp ;
if((fp=fopen("in.dat","r"))==NULL) return 1;
fclose(fp) ;
return 0 ;
}
void Compute(void)
{
}
void main()
{
int i ;
for(i=0;i xx[i]=0;
if(ReadDat())
{printf("Can't open the data file in.dat!\007\n") ;
return;
}
Compute();
printf("ODD=%d\nEVEN=%d\nAVER=%lf\nTOTFC=%lf\n", odd,even,aver,t
otfc);
WriteDat();
}
void WriteDat(void)
{
FILE *fp;
int i;
fp=fopen("out.dat", "w") ;
fprintf(fp, "%d\n%d\n%lf\n%lf\n",odd,even,aver,totfc);
fclose(fp) ;
}
/* 注:*/
int ReadDat(void)
{
FILE *fp ;
int i;
if((fp=fopen("in.dat","r"))==NULL) return 1;
for(i=0;i {fscanf(fp,"%d,",&xx[i]);
if(feof(fp)) break;
}
fclose(fp) ;
return 0 ;
}
void Compute(void)
{
int i,yy[1000];
for(i=0;i {aver+=xx[i];
if(xx[i]%2)
odd++;
else
even++;
}
aver/=(odd+even);
for(i=0;i totfc+=(xx[i]-aver)*(xx[i]-aver)/(odd+even);
}
八、數(shù)字或字符移位后的計算
已知在文件in.dat中存有若干個(個數(shù)<200)四位數(shù)字的正整數(shù),函數(shù)readdat
()讀取這若干個正整數(shù)并存入數(shù)組xx中。請編制函數(shù)calvalue(),其功能要求:1、求出
這文件中共有多少個正整數(shù)totnum;2、求這些數(shù)右移1位后,產(chǎn)生的新數(shù)是偶數(shù)的數(shù)的
個數(shù)totcnt,以及滿足此條件的這些數(shù)(右移前的值)的算術(shù)平均值totpjz,最后調(diào)用
函數(shù)writedat()把所求的結(jié)果輸出到文件out.dat中。
部分源程序已給出。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)readdat()和輸出數(shù)據(jù)函數(shù)writedat()的內(nèi)容。
#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;i if(readdat())
{printf("Can't open the data file in.dat!\007\n");
return;
}
calvalue();
printf("totnum=%d\n",totnum);
printf("totcnt=%d\n",totcnt);
printf("totpjz=%.2lf\n",totpjz);
writedat();
}
int readdat(void)
{
FILE *fp;
int i=0;
if((fp=fopen("in.dat","r"))==NULL) return 1;
while(!feof(fp))
fscanf(fp,"%d,",&xx[i++]);
fclose(fp);
return 0;
}
void writedat(void)
{
FILE *fp;
fp=fopen("out.dat","w");
fprintf(fp,"%d\n%d\n%.2lf\n",totnum,totcnt,totpjz);
fclose(fp);
}
/* 注:本題用if(!xx[i]) break;來判斷xx[i]是否為0,若是則跳出循環(huán)。亦是較簡單。*/
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;
}
請編制函數(shù)READDAT()實現(xiàn)從文件IN.DAT中讀取1000個十進(jìn)制整數(shù)到數(shù)組XX中;再
編制函數(shù)COMPUTE()分別計算出XX中奇數(shù)的個數(shù)ODD,偶數(shù)的個數(shù)EVEN,平均值`AVER以及方
差TOTFE的值,最后調(diào)用函數(shù)WRITEDAT()把結(jié)果輸出到OUT.DAT文件中.
計算方差的公式如下:
原始數(shù)據(jù)文件存放的格式是:每行存放10個數(shù),并用逗號隔開(每個數(shù)均大于0且小于等于2000).
#include
#include
#include
#define MAX 1000
int xx[MAX],odd=0,even=0;
double aver=0.0,totfc=0.0;
void WriteDat(void) ;
int ReadDat(void)
{
FILE *fp ;
if((fp=fopen("in.dat","r"))==NULL) return 1;
fclose(fp) ;
return 0 ;
}
void Compute(void)
{
}
void main()
{
int i ;
for(i=0;i
if(ReadDat())
{printf("Can't open the data file in.dat!\007\n") ;
return;
}
Compute();
printf("ODD=%d\nEVEN=%d\nAVER=%lf\nTOTFC=%lf\n", odd,even,aver,t
otfc);
WriteDat();
}
void WriteDat(void)
{
FILE *fp;
int i;
fp=fopen("out.dat", "w") ;
fprintf(fp, "%d\n%d\n%lf\n%lf\n",odd,even,aver,totfc);
fclose(fp) ;
}
/* 注:*/
int ReadDat(void)
{
FILE *fp ;
int i;
if((fp=fopen("in.dat","r"))==NULL) return 1;
for(i=0;i
if(feof(fp)) break;
}
fclose(fp) ;
return 0 ;
}
void Compute(void)
{
int i,yy[1000];
for(i=0;i
if(xx[i]%2)
odd++;
else
even++;
}
aver/=(odd+even);
for(i=0;i
}
八、數(shù)字或字符移位后的計算
已知在文件in.dat中存有若干個(個數(shù)<200)四位數(shù)字的正整數(shù),函數(shù)readdat
()讀取這若干個正整數(shù)并存入數(shù)組xx中。請編制函數(shù)calvalue(),其功能要求:1、求出
這文件中共有多少個正整數(shù)totnum;2、求這些數(shù)右移1位后,產(chǎn)生的新數(shù)是偶數(shù)的數(shù)的
個數(shù)totcnt,以及滿足此條件的這些數(shù)(右移前的值)的算術(shù)平均值totpjz,最后調(diào)用
函數(shù)writedat()把所求的結(jié)果輸出到文件out.dat中。
部分源程序已給出。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)readdat()和輸出數(shù)據(jù)函數(shù)writedat()的內(nèi)容。
#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;i
{printf("Can't open the data file in.dat!\007\n");
return;
}
calvalue();
printf("totnum=%d\n",totnum);
printf("totcnt=%d\n",totcnt);
printf("totpjz=%.2lf\n",totpjz);
writedat();
}
int readdat(void)
{
FILE *fp;
int i=0;
if((fp=fopen("in.dat","r"))==NULL) return 1;
while(!feof(fp))
fscanf(fp,"%d,",&xx[i++]);
fclose(fp);
return 0;
}
void writedat(void)
{
FILE *fp;
fp=fopen("out.dat","w");
fprintf(fp,"%d\n%d\n%.2lf\n",totnum,totcnt,totpjz);
fclose(fp);
}
/* 注:本題用if(!xx[i]) break;來判斷xx[i]是否為0,若是則跳出循環(huán)。亦是較簡單。*/
void calvalue(void)
{
int i,data;
for(i=0;i
if(xx[i]>0) totnum++;
data=xx[i]>>1;
if(data%2==0)
{totcnt++;
totpjz+=xx[i];
}
}
totpjz/=totcnt;
}

