上c语言文件复习题.docx
《上c语言文件复习题.docx》由会员分享,可在线阅读,更多相关《上c语言文件复习题.docx(76页珍藏版)》请在冰豆网上搜索。
![上c语言文件复习题.docx](https://file1.bdocx.com/fileroot1/2022-11/16/8102051b-db17-4b93-94d1-73bc73b4a193/8102051b-db17-4b93-94d1-73bc73b4a1931.gif)
上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;ifscanf(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;ifscanf(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;ifscanf(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;ifscanf(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;ifscanf(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)
{