• 一个实用的从文本文件读取数据进行排序的程序


    程序说明

      这是一个十分可靠的程序,这个程序的查错能力非常强悍。程序包含了文件操作,归并排序和字符串输入等多种技术。

      程序的功能是从外部读取一个包括int型数据的文本文件,然后将它保存到内部临时数组,对数组进行排序后,以文本形式输出到指定的文件上。因为是int类型的数据,没有很严重的损失精度的问题。

      正常运行要求:

      包括数据的源文件内不能包括其他任何除数字和空白字符(空格,制表符,换行符)之外的任何字符,源文件最开始必须是数字字符,要保证源文件的数据计数正确。同时保证文件名有效。

    完整代码

    警告:版权所有,谨供参考

    #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, int *);
    void merge(int *, int, int, 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("输入错误,请重新输入!
    ");
            while(getchar() != '
    ')
                continue;
        }
        while(getchar() != '
    ')
                continue;
    
        ptr = (int *)malloc( (size_t)n * sizeof(int) ); //申请动态数组//////
        if(ptr == NULL){
            fprintf(stderr, "FAIL TO ASK FOR MEMORY SPACE
    ");
            exit(EXIT_FAILURE);
        }
    
        printf("请输入原文件名:");
        if( s_gets(fname, ST) == NULL ){
            fprintf(stderr, "Fail to get a string
    ");
            exit(EXIT_FAILURE);
        }
    
        sor = fopen(fname, "r"); //打开包含数据的源文件
        if(sor == NULL){
            fprintf(stderr, "Fail to open the source file
    ");
            exit(EXIT_FAILURE);
        }
    
        for(i = 0; i < n; i++) //获取数据到动态数组
            if( fscanf(sor, "%d", &ptr[i]) != 1 ){
                fprintf(stderr, "Fail to get the data
    ");
                exit(EXIT_FAILURE);
            }
    
        mergesort(ptr, n); //排序
    
        printf("请输入要保存数据的文件名:");
        if( s_gets(fname, ST) == NULL ){
            fprintf(stderr, "Fail to get a string
    ");
            exit(EXIT_FAILURE);
        }
    
        dest = fopen(fname, "w"); //打开目标文件
        if(dest == NULL){
            fprintf(stderr, "Fail to open the destination file
    ");
            exit(EXIT_FAILURE);
        }
    
        for(i = 0; i < n; i++){ //输出数据到目标文件
            if( fprintf(dest, "%d	", ptr[i]) < 0 ){
                fprintf(stderr, "Fail to save the data
    ");
                exit(EXIT_FAILURE);
            }
            if( ((i + 1) % 10) == 0){ //如果写满10个就换行
                if( fprintf(dest, "
    ") < 0 ){
                    fprintf(stderr, "Fail to save the data
    ");
                    exit(EXIT_FAILURE);
                }
            }
        }
    
        if( fclose(sor) != 0 ){ //关闭源文件
            fprintf(stderr, "Fail to close the source file
    ");
            exit(EXIT_FAILURE);
        }
        if( fclose(dest) != 0 ){ //关闭目标文件
            fprintf(stderr, "Fail to close the destination file
    ");
            exit(EXIT_FAILURE);
        }
        free(ptr); //释放内存
    
        printf("成功完成!
    请按任意键继续^ ^");
    
        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 SPACE
    ");
                exit(EXIT_FAILURE);
            }
            _mergesort(ar, 0, 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, start, mid, end, 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] != '
    ' && st[i] != '') //如果没有到输入字符串结束
                i++;    //递增
            if(st[i] == '
    ') //如果字符串最后一个字符是'
    '
                st[i] = ''; //把它变成''
            else //否则缓冲区内还有一部分超出读取范围的字符没有被读取
                while(getchar() != '
    ') //把这些字符读取完(清空缓冲区)
                    continue;
        }
    
        return re;
    }
    main.c

    运行结果

    data.txt:

    obj.txt:

  • 相关阅读:
    org.json.JSONObject的optXXX方法
    android Fragment的数据传递
    android .9图片的制作
    android handler
    CSS中的!important属性用法
    JS中的prototype
    JavaScript 函数创建思想
    css笔记
    Frameset使用教程
    HDU 5536 Chip Factory 【01字典树删除】
  • 原文地址:https://www.cnblogs.com/mrblug/p/5778088.html
Copyright © 2020-2023  润新知