☆題目54
已知數(shù)據(jù)文件in.dat中存有300個四位數(shù),并已調(diào)用讀函數(shù)readdat()把這些數(shù)存入數(shù)組a中,請編制一函數(shù)jsvalue(),其功能是:求出千位數(shù)上的數(shù)加百位數(shù)上的數(shù)等于十位數(shù)上的數(shù)加個位數(shù)上的數(shù)的個數(shù)cnt,再把所有滿足此條件的四位數(shù)依次存入數(shù)組b中,然后對數(shù)組b的四位數(shù)按從大到小的順序進行排序,最后調(diào)用寫函數(shù)writedat()把結(jié)果輸出到out.dat文件。
例如:7153,7+1=5+3,則該數(shù)滿足條件存入數(shù)組b中,且個數(shù)cnt=cnt+1。
8129,8+1<>2+9,則該數(shù)不滿足條件忽略。
部分源程序存在文件prog1.c中。
程序中已定義數(shù)組:a[300],b[300],已定義變量:cnt
請勿改動主函數(shù)main()、讀函數(shù)readdat()和寫函數(shù)writedat()的內(nèi)容。
#include
int a[300],b[300],cnt=0;
jsvalue()
{ int i,j,qw,bw,sw,gw;
for(i=0;i<300;i++)
{ qw=a[i]/1000; bw=a[i]/100%10;
sw=a[i]%100/10; gw=a[i]%10;
if(qw+bw==sw+gw) b[cnt++]=a[i];
}
for(i=0;i for(j=i+1;j if(b[i] }
main()
{
int i;
readdat();
jsvalue();
writedat();
printf("cnt=%d\n",cnt);
for(i=0;i }
readdat()
{
file *fp;
int i;
fp=fopen("in.dat","r");
for(i=0;i<300;i++)fscanf(fp,"%d,",&a[i]);
fclose(fp);
}
writedat()
{
file *fp;
int i;
fp=fopen("out.dat","w");
fprintf(fp,"%d\n",cnt);
for(i=0;i fclose(fp);
}
已知數(shù)據(jù)文件in.dat中存有300個四位數(shù),并已調(diào)用讀函數(shù)readdat()把這些數(shù)存入數(shù)組a中,請編制一函數(shù)jsvalue(),其功能是:求出千位數(shù)上的數(shù)加百位數(shù)上的數(shù)等于十位數(shù)上的數(shù)加個位數(shù)上的數(shù)的個數(shù)cnt,再把所有滿足此條件的四位數(shù)依次存入數(shù)組b中,然后對數(shù)組b的四位數(shù)按從大到小的順序進行排序,最后調(diào)用寫函數(shù)writedat()把結(jié)果輸出到out.dat文件。
例如:7153,7+1=5+3,則該數(shù)滿足條件存入數(shù)組b中,且個數(shù)cnt=cnt+1。
8129,8+1<>2+9,則該數(shù)不滿足條件忽略。
部分源程序存在文件prog1.c中。
程序中已定義數(shù)組:a[300],b[300],已定義變量:cnt
請勿改動主函數(shù)main()、讀函數(shù)readdat()和寫函數(shù)writedat()的內(nèi)容。
#include
int a[300],b[300],cnt=0;
jsvalue()
{ int i,j,qw,bw,sw,gw;
for(i=0;i<300;i++)
{ qw=a[i]/1000; bw=a[i]/100%10;
sw=a[i]%100/10; gw=a[i]%10;
if(qw+bw==sw+gw) b[cnt++]=a[i];
}
for(i=0;i
main()
{
int i;
readdat();
jsvalue();
writedat();
printf("cnt=%d\n",cnt);
for(i=0;i
readdat()
{
file *fp;
int i;
fp=fopen("in.dat","r");
for(i=0;i<300;i++)fscanf(fp,"%d,",&a[i]);
fclose(fp);
}
writedat()
{
file *fp;
int i;
fp=fopen("out.dat","w");
fprintf(fp,"%d\n",cnt);
for(i=0;i
}