上c语言文件复习题.docx

上传人:b****5 文档编号:2865645 上传时间:2022-11-16 格式:DOCX 页数:76 大小:22.77KB
下载 相关 举报
上c语言文件复习题.docx_第1页
第1页 / 共76页
上c语言文件复习题.docx_第2页
第2页 / 共76页
上c语言文件复习题.docx_第3页
第3页 / 共76页
上c语言文件复习题.docx_第4页
第4页 / 共76页
上c语言文件复习题.docx_第5页
第5页 / 共76页
点击查看更多>>
下载资源
资源描述

上c语言文件复习题.docx

《上c语言文件复习题.docx》由会员分享,可在线阅读,更多相关《上c语言文件复习题.docx(76页珍藏版)》请在冰豆网上搜索。

上c语言文件复习题.docx

上c语言文件复习题

1、已知文件dat2.dat中存放了500个三位整型数据。

要求:

1)将文件中的数据存入一个一维数组中。

2)编写函数intsxhs(int*p,intnum)

{

}

求数组元素中“水仙花数”的个数,并把该函数放在头文件Fnumber.h中以便在主函数中调用该函数。

(“水仙花数”的含义是一个数的每个数位的立方和等于该数。

例153=1^3+5^3+3^3。

3)把“水仙花数”的个数显示到屏幕上,同时输出到数据文件d:

\dat6.dat中。

答案:

19

头文件:

intsxhs(int*p,intnum)

{

int*v,m=0,a,b,c;

for(v=p;v

{

a=*v/100;

b=*v/10%10;

c=*v%10;

if(*v==a*a*a+b*b*b+c*c*c)

m++;

}

returnm;

}

主程序:

#include

#include

#include

#include"Fnumber.h"

#defineN500

intmain()

{

FILE*fp;

intarray[N],n;

fp=fopen("d:

\\dat2.dat","r");

if(NULL==fp)

{

printf("Cannotopenthisfile!

\n");

exit(0);

}

for(inti=0;i

fscanf(fp,"%d",array+i);

n=sxhs(array,N);

FILE*mp;

mp=fopen("D:

\\dat6.dat","w");

if(NULL==mp)

{

printf("Cannotopenthisfile!

\n");

exit(0);

}

fprintf(mp,"%d\n",n);

printf("%d\n",n);

fclose(fp);

fclose(mp);

return0;

}

2、已知文件dat2.dat中存放了500个三位整型数据。

要求:

1)将文件中的数据存入一个一维数组中。

2)编写函数intsxhs(int*p,intnum)

{

}

求数组元素中“水仙花数”的和,并把该函数放在头文件Fnumber.h中以便在主函数中调用该函数。

(“水仙花数”的含义是一个数的每个数位的立方和等于该数。

例153=1^3+5^3+3^3。

3)把“水仙花数”的和显示到屏幕上,同时输出到数据文件d:

\dat6.dat中。

答案:

6315

头文件:

intsxhs(int*p,intnum)

{

int*v,m=0,a,b,c;

for(v=p;v

{

a=*v/100;

b=*v/10%10;

c=*v%10;

if(*v==a*a*a+b*b*b+c*c*c)

m+=*v;

}

returnm;

}

主程序:

#include

#include

#include

#include"Fnumber.h"

#defineN500

intmain()

{

FILE*fp;

intarray[N],n;

fp=fopen("d:

\\dat2.dat","r");

if(NULL==fp)

{

printf("Cannotopenthisfile!

\n");

exit(0);

}

for(inti=0;i

fscanf(fp,"%d",array+i);

n=sxhs(array,N);

FILE*mp;

mp=fopen("D:

\\dat6.dat","w");

if(NULL==mp)

{

printf("Cannotopenthisfile!

\n");

exit(0);

}

fprintf(mp,"%d\n",n);

printf("%d\n",n);

fclose(fp);

fclose(mp);

return0;

}

3、已知文件dat3.dat中存放了500个五位整型数据。

要求:

1)将文件中的数据存入一个一维数组中。

2)编写函数inthws(int*p,intnum)

{

}

求数组元素中回文数的个数,并把该函数放在头文件Fnumber.h中以便在主函数中调用该函数。

(回文数的含义是:

从左向右读与从右向左读是相同的,即对称。

如:

12321。

3)把回文数及回文数的个数显示到屏幕上,同时输出到数据文件d:

\dat6.dat中。

答案:

39693332332131223432个数:

4

头文件:

inthws(int*p,intnum)

{

int*v,m=0,a,b,c,d;

for(v=p;v

{

a=*v/10000;

b=*v/1000%10;

c=*v/10%10;

d=*v%10;

if(a==d&&b==c)

{

m++;

printf("%d",*v);

}

}

returnm;

}

主程序:

#include

#include

#include

#include"Fnumber.h"

#defineN500

intmain()

{

FILE*fp;

intarray[N],n;

fp=fopen("d:

\\dat3.dat","r");

if(NULL==fp)

{

printf("Cannotopenthisfile!

\n");

exit(0);

}

for(inti=0;i

fscanf(fp,"%d",array+i);

n=hws(array,N);

FILE*mp;

mp=fopen("D:

\\dat6.dat","w");

if(NULL==mp)

{

printf("Cannotopenthisfile!

\n");

exit(0);

}

fprintf(mp,"%d\n",n);

printf("%d\n",n);

fclose(fp);

fclose(mp);

return0;

}

4、已知文件dat3.dat中存放了500个五位整型数据。

要求:

1)将文件中的数据存入一个一维数组中。

2)编写函数inthws(int*p,intnum)

{

}

求数组元素中回文数的和,并把该函数放在头文件Fnumber.h中以便在主函数中调用该函数。

3)把回文数及回文数的和显示到屏幕上,同时输出到数据文件d:

\dat6.dat中。

答案:

39693332332131223432和:

117670

头文件:

inthws(int*p,intnum)

{

int*v,m=0,a,b,c,d;

for(v=p;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);

}

}

returnm;

}

主程序:

#include

#include

#include

#include"Fnumber.h"

#defineN500

intmain()

{

FILE*fp;

intarray[N],n;

fp=fopen("d:

\\dat3.dat","r");

if(NULL==fp)

{

printf("Cannotopenthisfile!

\n");

exit(0);

}

for(inti=0;i

fscanf(fp,"%d",array+i);

n=hws(array,N);

FILE*mp;

mp=fopen("D:

\\dat6.dat","w");

if(NULL==mp)

{

printf("Cannotopenthisfile!

\n");

exit(0);

}

fprintf(mp,"%d\n",n);

printf("%d\n",n);

fclose(fp);

fclose(mp);

return0;

}

5、已知文件dat2.dat中存放了500个整型数据。

要求:

1)将文件中的数据存入一个一维数组中。

2)编写函数intws(int*p,intnum)

{

}

求数组中“完数”的个数,并把该函数放在头文件Fnumber.h中以便在主函数中调用该函数。

(完数的含义是:

该数的所有因子之和等于该数。

例6=1+2+3。

3)把“完数”及“完数”的个数显示到屏幕上,同时输出到数据文件d:

\dat6.dat中。

答案:

496496496496496496个数:

6

头文件:

intws(int*p,intnum)

{

int*v,m=0,s,i;

for(v=p;v

{

s=0;

for(i=1;i<*v;i++)

if(*v%i==0)

s+=i;

if(*v==s)

{

m++;

printf("%d",*v);

}

}

returnm;

}

主程序:

#include

#include

#include

#include"Fnumber.h"

#defineN500

intmain()

{

FILE*fp;

intarray[N],n;

fp=fopen("d:

\\dat2.dat","r");

if(NULL==fp)

{

printf("Cannotopenthisfile!

\n");

exit(0);

}

for(inti=0;i

fscanf(fp,"%d",array+i);

n=ws(array,N);

FILE*mp;

mp=fopen("D:

\\dat6.dat","w");

if(NULL==mp)

{

printf("Cannotopenthisfile!

\n");

exit(0);

}

fprintf(mp,"%d\n",n);

printf("%d\n",n);

fclose(fp);

fclose(mp);

return0;

}

6、已知文件dat2.dat中存放了500个整型数据。

要求:

1)将文件中的数据存入一个一维数组中。

2)编写函数intws(int*p,intnum)

{

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 调查报告

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1