69,382
社区成员
发帖
与我相关
我的任务
分享
//将out.txt文件中的内容排序并去重,结果保存到unique.txt中
#include <stdio.h>
#include <stdlib.h>
#define MAXLNO 10000000 //能处理的最大行数
#define MAXLEN 20 //能处理的最大行宽,包括行尾的\n和字符串尾的\0
char buf[MAXLNO][MAXLEN];
int ln,i;
FILE *f;
int cmpfun( const void *arg1, const void *arg2 ) {
return strcmp((const char *)arg1,(const char *)arg2);
}
int main() {
f=fopen("out.txt","r");
if (NULL==f) {
printf("Can not find file out.txt\n");
return 1;
}
ln=0;
while (1) {
if (NULL==fgets(buf[ln],MAXLEN,f)) break;
ln++;
if (ln>=MAXLNO) {
printf("Lines >%d ignored.",MAXLNO);
break;
}
}
fclose(f);
if (ln>1) qsort(buf,ln,MAXLEN,cmpfun);
f=fopen("unique.txt","w");
if (NULL==f) {
printf("Can not create file unique.txt\n");
return 2;
}
fprintf(f,"%s",buf[0]);
if (ln>1) for (i=1;i<ln;i++) {
if (strcmp((const char *)buf[i-1],(const char *)buf[i])) fprintf(f,"%s",buf[i]);
}
fclose(f);
return 0;
}
#include <stdio.h>
int main(void)
{
int i, j;
int a[10], b[11];
b[0] = ~0;
for(i = 0, j = 0; i < 10; i++) {
scanf("%d", &a[i]);
if (b[j] < a[i])
b[++j] = a[i];
}
for (i = 1; i <= j; i++)
printf("%d\n", b[i]);
return 0;
}
#include <stdio.h>
int main(void)
{
int i, j;
int a[10], b[10];
for(i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
j = 0;
b[j] = a[0];
for(i = 1; i < 10; i++)
if (b[j] < a[i])
b[++j] = a[i];
for (i = 0; i <= j; i++)
printf("%d\n", b[i]);
return 0;
}