1、上c语言文件复习题1、已知文件dat2.dat中存放了500个三位整型数据。要求:1)将文件中的数据存入一个一维数组中。2)编写函数int sxhs(int *p,int num)求数组元素中 “水仙花数”的个数,并把该函数放在头文件Fnumber.h中以便在主函数中调用该函数。(“水仙花数”的含义是一个数的每个数位的立方和等于该数。例153=13+53+33。)3)把“水仙花数”的个数显示到屏幕上,同时输出到数据文件d:dat6.dat中。答案:19头文件:int sxhs(int *p,int num) int *v,m=0,a,b,c; for(v=p;vp+500;v+) a=*v/1
2、00; b=*v/10%10; c=*v%10; if(*v=a*a*a+b*b*b+c*c*c) m+; return m;主程序:#include #include #include #includeFnumber.h#define N 500int main() FILE *fp; int arrayN,n; fp = fopen(d:dat2.dat,r); if(NULL =fp) printf(Can not open this file!n); exit(0); for(int i=0;iN;i+) fscanf(fp,%d,array+i); n=sxhs(array,N);
3、FILE *mp; mp = fopen(D:dat6.dat,w); if(NULL =mp) printf(Can not open this file!n); exit(0); fprintf(mp,%dn,n); printf(%dn,n); fclose(fp); fclose(mp); return 0;2、已知文件dat2.dat中存放了500个三位整型数据。要求:1)将文件中的数据存入一个一维数组中。2)编写函数int sxhs(int *p,int num)求数组元素中 “水仙花数”的和,并把该函数放在头文件Fnumber.h中以便在主函数中调用该函数。(“水仙花数”的含义是
4、一个数的每个数位的立方和等于该数。例153=13+53+33。)3)把“水仙花数”的和显示到屏幕上,同时输出到数据文件d:dat6.dat中。答案:6315头文件:int sxhs(int *p,int num) int *v,m=0,a,b,c; for(v=p;vp+500;v+) a=*v/100; b=*v/10%10; c=*v%10; if(*v=a*a*a+b*b*b+c*c*c) m+=*v; return m;主程序:#include #include #include #includeFnumber.h#define N 500int main() FILE *fp; in
5、t arrayN,n; fp = fopen(d:dat2.dat,r); if(NULL =fp) printf(Can not open this file!n); exit(0); for(int i=0;iN;i+) fscanf(fp,%d,array+i); n=sxhs(array,N); FILE *mp; mp = fopen(D:dat6.dat,w); if(NULL =mp) printf(Can not open this file!n); exit(0); fprintf(mp,%dn,n); printf(%dn,n); fclose(fp); fclose(mp
6、); return 0;3、已知文件dat3.dat中存放了500个五位整型数据。要求:1)将文件中的数据存入一个一维数组中。2)编写函数int hws(int *p,int num)求数组元素中回文数的个数,并把该函数放在头文件Fnumber.h中以便在主函数中调用该函数。(回文数的含义是:从左向右读与从右向左读是相同的,即对称。如:12321。)3)把回文数及回文数的个数显示到屏幕上,同时输出到数据文件d:dat6.dat中。答案:39693 33233 21312 23432 个数:4头文件:int hws(int *p,int num) int *v,m=0,a,b,c,d; for(
7、v=p;vp+500;v+) a=*v/10000; b=*v/1000%10; c=*v/10%10; d=*v%10; if(a=d&b=c) m+; printf(%d ,*v); return m;主程序:#include #include #include #includeFnumber.h#define N 500int main() FILE *fp; int arrayN,n; fp = fopen(d:dat3.dat,r); if(NULL =fp) printf(Can not open this file!n); exit(0); for(int i=0;iN;i+)
8、fscanf(fp,%d,array+i); n=hws(array,N); FILE *mp; mp = fopen(D:dat6.dat,w); if(NULL =mp) printf(Can not open this file!n); exit(0); fprintf(mp,%dn,n); printf(%dn,n); fclose(fp); fclose(mp); return 0;4、已知文件dat3.dat中存放了500个五位整型数据。要求:1)将文件中的数据存入一个一维数组中。2)编写函数int hws(int *p,int num)求数组元素中回文数的和,并把该函数放在头文件
9、Fnumber.h中以便在主函数中调用该函数。3)把回文数及回文数的和显示到屏幕上,同时输出到数据文件d:dat6.dat中。答案:39693 33233 21312 23432 和:117670头文件:int hws(int *p,int num) int *v,m=0,a,b,c,d; for(v=p;vp+500;v+) a=*v/10000; b=*v/1000%10; c=*v/10%10; d=*v%10; if(a=d&b=c) m+=*v; printf(%d ,*v); return m;主程序:#include #include #include #includeFnumb
10、er.h#define N 500int main() FILE *fp; int arrayN,n; fp = fopen(d:dat3.dat,r); if(NULL =fp) printf(Can not open this file!n); exit(0); for(int i=0;iN;i+) fscanf(fp,%d,array+i); n=hws(array,N); FILE *mp; mp = fopen(D:dat6.dat,w); if(NULL =mp) printf(Can not open this file!n); exit(0); fprintf(mp,%dn,n
11、); printf(%dn,n); fclose(fp); fclose(mp); return 0;5、已知文件dat2.dat中存放了500个整型数据。要求:1)将文件中的数据存入一个一维数组中。2)编写函数int ws(int *p,int num)求数组中“完数”的个数,并把该函数放在头文件Fnumber.h中以便在主函数中调用该函数。(完数的含义是:该数的所有因子之和等于该数。例6=1+2+3。)3)把 “完数”及“完数”的个数显示到屏幕上,同时输出到数据文件d:dat6.dat中。答案:496 496 496 496 496 496 个数:6头文件:int ws(int *p,in
12、t num) int *v,m=0,s,i; for(v=p;vp+500;v+) s=0; for(i=1;i*v;i+) if(*v%i=0) s+=i; if(*v=s) m+; printf(%d ,*v); return m;主程序:#include #include #include #includeFnumber.h#define N 500int main() FILE *fp; int arrayN,n; fp = fopen(d:dat2.dat,r); if(NULL =fp) printf(Can not open this file!n); exit(0); for(int i=0;iN;i+) fscanf(fp,%d,array+i); n=ws(array,N); FILE *mp; mp = fopen(D:dat6.dat,w); if(NULL =mp) printf(Can not open this file!n); exit(0); fprintf(mp,%dn,n); printf(%dn,n); fclose(fp); fclose(mp); return 0;6、已知文件dat2.dat中存放了500个整型数据。要求:1)将文件中的数据存入一个一维数组中。2)编写函数int ws(int *p,int num)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1