題目60
在文件in.dat中有200組數(shù)據(jù),每組有3個數(shù),每個數(shù)均是三位數(shù)。函數(shù)readdat()讀取這200組數(shù)據(jù)存放到結(jié)構(gòu)數(shù)組aa中,請編制函數(shù)jssort(),其函數(shù)的功能是:要求在200組數(shù)據(jù)中找出條件為每組中的第二個數(shù)大于第一個數(shù)加第三個數(shù)的之和,其中滿足條件的組數(shù)作為函數(shù)jssort() 的返回值,同時把滿足條件的數(shù)據(jù)存入結(jié)構(gòu)數(shù)組bb中,再對bb中的數(shù)據(jù)按照每組數(shù)據(jù)的第二個數(shù)加第三個之和的大小進(jìn)行降序排列(第二個數(shù)加第三個數(shù)的和均不相等),排序后的結(jié)果仍重新存入結(jié)構(gòu)數(shù)組bb中,最后調(diào)用函數(shù)writedat()把結(jié)果bb輸出到文件out.dat中。
部分源程序存在文件prog1.c中。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)readdat()和輸出數(shù)據(jù)函數(shù)writedat()的內(nèi)容。
#include
#include
#include
typedef struct{
int x1,x2,x3;
}data;
data aa[200],bb[200];
int jssort()
{int i, j, cnt=0;
data ch;
for(i=0;i<200;i++)
if(aa[i].x2>aa[i].x1+aa[i].x3) bb[cnt++]=aa[i];
for(i=0;i for(j=i+1;j if(bb[i].x2+bb[i].x3 return cnt;
}
void main()
{
int count;
readdat();
count=jssort(); /*返回滿足條件的個數(shù)*/
writedat(count);
}
readdat()
{
file *in;
int i;
in=fopen("in.dat","r");
for(i=0; i<200; i++)
fscanf(in,"%d %d %d",&aa[i].x1,&aa[i].x2,&aa[i].x3);
fclose(in);
}
writedat(int count)
{
file *out;
int i;
out=fopen("out.dat","w");
clrscr();
for(i=0; i printf("%d,%d,%d 第二個數(shù)+第三個數(shù)=%d\n",bb[i].x1,bb[i].x2,bb[i].x3,bb[i].x2+bb[i].x3); fprintf(out,"%d %d %d\n",bb[i].x1,bb[i].x2,bb[i].x3);
}
fclose(out);
}
在文件in.dat中有200組數(shù)據(jù),每組有3個數(shù),每個數(shù)均是三位數(shù)。函數(shù)readdat()讀取這200組數(shù)據(jù)存放到結(jié)構(gòu)數(shù)組aa中,請編制函數(shù)jssort(),其函數(shù)的功能是:要求在200組數(shù)據(jù)中找出條件為每組中的第二個數(shù)大于第一個數(shù)加第三個數(shù)的之和,其中滿足條件的組數(shù)作為函數(shù)jssort() 的返回值,同時把滿足條件的數(shù)據(jù)存入結(jié)構(gòu)數(shù)組bb中,再對bb中的數(shù)據(jù)按照每組數(shù)據(jù)的第二個數(shù)加第三個之和的大小進(jìn)行降序排列(第二個數(shù)加第三個數(shù)的和均不相等),排序后的結(jié)果仍重新存入結(jié)構(gòu)數(shù)組bb中,最后調(diào)用函數(shù)writedat()把結(jié)果bb輸出到文件out.dat中。
部分源程序存在文件prog1.c中。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)readdat()和輸出數(shù)據(jù)函數(shù)writedat()的內(nèi)容。
#include
#include
#include
typedef struct{
int x1,x2,x3;
}data;
data aa[200],bb[200];
int jssort()
{int i, j, cnt=0;
data ch;
for(i=0;i<200;i++)
if(aa[i].x2>aa[i].x1+aa[i].x3) bb[cnt++]=aa[i];
for(i=0;i
}
void main()
{
int count;
readdat();
count=jssort(); /*返回滿足條件的個數(shù)*/
writedat(count);
}
readdat()
{
file *in;
int i;
in=fopen("in.dat","r");
for(i=0; i<200; i++)
fscanf(in,"%d %d %d",&aa[i].x1,&aa[i].x2,&aa[i].x3);
fclose(in);
}
writedat(int count)
{
file *out;
int i;
out=fopen("out.dat","w");
clrscr();
for(i=0; i
}
fclose(out);
}

