c语言高手进 解决个小程序20行顶多(在线等)

来源:百度知道 编辑:UC知道 时间:2024/05/12 05:57:15
2.编一个C程序,它能读入文本文件f1.c和f2.c中的所有整数,并把f1.c和f2.c都包含的数按从大到小的次序写到文本文件f3.c(同一个数在文件f3.c中最多只能出现一次。文件f1.c、f2.c和f3.c都存于你的账号目录下)中。文件中的相邻两个整数都用空格隔开,每10个换行。文件f1.c和f2.c中的整数个数都不超过1000。

#include "stdio.h"
FILE *f1,*f2,*f3;
int s[299999];
qsort(int l,int r)
{ int i=l,j=r,t,x=s[r/2+1];
while (i<=j)
{ while (s[i]<x) i++;
while (s[j]>x) j--;
if(i<j){t=s[i];s[i]=s[j];s[j]=t;i++;j--;}
else if(i==j) i++,j--;
}
if(l>j) qsort(l,j);
if(i<r) qsort(i,r);
}

print(int i)
{ int n;
fprintf(f3,"%d",s[i]);
for(n=i-1;n>0;n--)
if (s[n]!=s[n+1]) fprintf(f3,"%d",s[n]);
}

main()
{ f1=fopen("f1.c","r");
f2=fopen("f2.c","r");
f3=fopen("f3.c","w");
int i=1;
while(fscanf(f1,"%d",s+i)!=EOF)
i++;
while(fscanf(f2,"%d",s+i)!=EOF)
i++;
qsort(1,i);
print(i);
fclose(f1);
fclose(f2);
fclose(f3);
}

可不简单啊 !!