当前位置: 代码迷 >> 综合 >> C语言实现记事本行级元素进行字母顺序排列
  详细解决方案

C语言实现记事本行级元素进行字母顺序排列

热度:61   发布时间:2024-01-18 08:01:42.0
#include <stdio.h>
#include <string.h>
void main(void)
{char ch[10000][100] = { {""} };char max[100],lj[100];int i=0, j=0,k,int_max;FILE *fp;printf("请输入要排序的文件路径及文件名:");scanf("%s",lj);fp = fopen(lj, "r");while ((ch[i][j] = fgetc(fp)) != EOF){putchar(ch[i][j]);if (ch[i][j] == '\n'){i++;j =-1;}j++;}int_max = i;fclose(fp);printf("\n\n\n\n");printf("程序已经加载成功,请输入输出文件的路径及文件名:");scanf("%s",lj);fp = fopen(lj, "w");printf("\n\n\n\n");for (i = 0;i < int_max;i++){strcpy(max, ch[i]);for (j = i+1;j < int_max;j++)if (strcmp(max, ch[j]) > 0){strcpy(max, ch[j]);k = j;}strcpy(ch[k], ch[i]);strcpy(ch[i], max);printf("%s",ch[i]);for (j = 0;j < 10000;j++){fputc(ch[i][j], fp);if (ch[i][j] == '\n')break;}}fclose(fp);puts("\n排序成功");putchar('\n');
}

 

  相关解决方案