用C语言实现从文本文件中读取数据后进行排序的功能
发布时间:2020-12-16 05:20:30 所属栏目:百科 来源:网络整理
导读:功能介绍 程序的功能是从外部读取一个包括int型数据的文本文件,然后将它保存到内部临时数组,对数组进行排序后,以文本形式输出到指定的文件上。因为是int类型的数据,没有很严重的损失精度的问题。 正常运行要求: 包括数据的源文件内不能包括其他任何除数
功能介绍 程序的功能是从外部读取一个包括int型数据的文本文件,然后将它保存到内部临时数组,对数组进行排序后,以文本形式输出到指定的文件上。因为是int类型的数据,没有很严重的损失精度的问题。 正常运行要求: 包括数据的源文件内不能包括其他任何除数字和空白字符(空格,制表符,换行符)之外的任何字符,源文件最开始必须是数字字符,要保证源文件的数据计数正确。同时保证文件名有效。 运行结果 data.txt: obj.txt: 完整代码 警告:版权所有,谨供参考! #include <stdio.h> #include <stdlib.h> #include <conio.h> /*============================= 制作于:Aug 16,2016 by QQ:1729403632 ===============================*/ #define ST 64 //字符串大小 void mergesort(int *,int); void _mergesort(int *,int,int *); void merge(int *,int *); char * s_gets(char *,int); int main(int argc,char * argv[]){ FILE * sor,* dest; //sor源文件 dest目标文件 int * ptr;//临时数组 int i,n; //n表示元素个数 char fname[ST]; //临时存储字符串 printf("请输入元素个数:"); while( scanf("%d",&n) != 1 || n <= 0 ){ printf("输入错误,请重新输入!n"); while(getchar() != 'n') continue; } while(getchar() != 'n') continue; ptr = (int *)malloc( (size_t)n * sizeof(int) ); //申请动态数组////// if(ptr == NULL){ fprintf(stderr,"FAIL TO ASK FOR MEMORY SPACEn"); exit(EXIT_FAILURE); } printf("请输入原文件名:"); if( s_gets(fname,ST) == NULL ){ fprintf(stderr,"Fail to get a stringn"); exit(EXIT_FAILURE); } sor = fopen(fname,"r"); //打开包含数据的源文件 if(sor == NULL){ fprintf(stderr,"Fail to open the source filen"); exit(EXIT_FAILURE); } for(i = 0; i < n; i++) //获取数据到动态数组 if( fscanf(sor,"%d",&ptr[i]) != 1 ){ fprintf(stderr,"Fail to get the datan"); exit(EXIT_FAILURE); } mergesort(ptr,n); //排序 printf("请输入要保存数据的文件名:"); if( s_gets(fname,"Fail to get a stringn"); exit(EXIT_FAILURE); } dest = fopen(fname,"w"); //打开目标文件 if(dest == NULL){ fprintf(stderr,"Fail to open the destination filen"); exit(EXIT_FAILURE); } for(i = 0; i < n; i++){ //输出数据到目标文件 if( fprintf(dest,"%dt",ptr[i]) < 0 ){ fprintf(stderr,"Fail to save the datan"); exit(EXIT_FAILURE); } if( ((i + 1) % 10) == 0){ //如果写满10个就换行 if( fprintf(dest,"n") < 0 ){ fprintf(stderr,"Fail to save the datan"); exit(EXIT_FAILURE); } } } if( fclose(sor) != 0 ){ //关闭源文件 fprintf(stderr,"Fail to close the source filen"); exit(EXIT_FAILURE); } if( fclose(dest) != 0 ){ //关闭目标文件 fprintf(stderr,"Fail to close the destination filen"); exit(EXIT_FAILURE); } free(ptr); //释放内存 printf("成功完成!n请按任意键继续^ ^bb"); getch(); return 0; } void mergesort(int * ar,int size){ if(size > 0){ int * temp; temp = (int *)malloc( (size_t)size * sizeof(int) ); ///// if(temp == NULL){ fprintf(stderr,"Fail to ask for MEMORY SPACEn"); exit(EXIT_FAILURE); } _mergesort(ar,size - 1,temp); //归并排序 free(temp); } } void _mergesort(int * ar,int start,int end,int * temp){ if(start < end){ int mid = (start + end) / 2; _mergesort(ar,start,mid,temp); //左子数组排序 _mergesort(ar,mid + 1,end,temp); //右子数组排序 merge(ar,temp); //合并子数组 } } void merge(int * ar,int p,int q,int r,int * temp){ int i = p,j = q + 1,k = 0; while(i <= q && j <= r){ if(ar[i] < ar[j]) temp[k++] = ar[i++]; else temp[k++] = ar[j++]; } while(i <= q) //如果序列[i...q]存在,追加 temp[k++] = ar[i++]; while(j <= r) //如果序列[j...r]存在,追加 temp[k++] = ar[j++]; for(k = 0; k <= (r - p); k++) ar[p + k] = temp[k]; } char * s_gets(char * st,int size){ char * re; int i = 0; re = fgets(st,size,stdin); if(re){ while(st[i] != 'n' && st[i] != ' ') //如果没有到输入字符串结束 i++; //递增 if(st[i] == 'n') //如果字符串最后一个字符是'n' st[i] = ' '; //把它变成' ' else //否则缓冲区内还有一部分超出读取范围的字符没有被读取 while(getchar() != 'n') //把这些字符读取完(清空缓冲区) continue; } return re; } 总结 以上就是用C语言实现从文本文件中读取数据后进行排序功能的全部内容,阅读这篇文章后,大家自己进行调试运行,相信会对于学习C语言的朋友们很有帮助的。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |