★☆題目55
已知數(shù)據(jù)文件in.dat中存有300個四位數(shù),并已調(diào)用讀函數(shù)readdat()把這些數(shù)存入數(shù)組a中,請編制一函數(shù)jsvalue(),其功能是:求出所有這些四位數(shù)是素數(shù)的個數(shù)cnt,再把所有滿足此條件的四位數(shù)依次存入數(shù)組b中,然后對數(shù)組b的四位數(shù)按從小到大的順序進行排序,最后調(diào)用寫函數(shù)writedat()把結(jié)果輸出到out.dat文件。
例如:5591是素數(shù),則該數(shù)滿足條件存入數(shù)組b中,且個數(shù)cnt=cnt+1。9812是非素數(shù),則該數(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;
int isp(int m){
int i;
for(i=2;i if(m%i==0)return 0;
return 1;
}
jsvalue()
{int i,j,value;
for(i=0;i<300;i++)
if(isp(a[i])) b[cnt++]=a[i];
for(i=0;i for(j=i+1;j if(b[i]>b[j]) {value=b[i]; b[i]=b[j]; b[j]=value;}
}
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ù)cnt,再把所有滿足此條件的四位數(shù)依次存入數(shù)組b中,然后對數(shù)組b的四位數(shù)按從小到大的順序進行排序,最后調(diào)用寫函數(shù)writedat()把結(jié)果輸出到out.dat文件。
例如:5591是素數(shù),則該數(shù)滿足條件存入數(shù)組b中,且個數(shù)cnt=cnt+1。9812是非素數(shù),則該數(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;
int isp(int m){
int i;
for(i=2;i
return 1;
}
jsvalue()
{int i,j,value;
for(i=0;i<300;i++)
if(isp(a[i])) 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
}

