欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

用C語言實現(xiàn)從文本文件中讀取數(shù)據(jù)后進行排序的功能

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

功能介紹

程序的功能是從外部讀取一個包括int型數(shù)據(jù)的文本文件,然后將它保存到內部臨時數(shù)組,對數(shù)組進行排序后,以文本形式輸出到指定的文件上。因為是int類型的數(shù)據(jù),沒有很嚴重的損失精度的問題。

正常運行要求:

包括數(shù)據(jù)的源文件內不能包括其他任何除數(shù)字和空白字符(空格,制表符,換行符)之外的任何字符,源文件最開始必須是數(shù)字字符,要保證源文件的數(shù)據(jù)計數(shù)正確。同時保證文件名有效。

運行結果

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, 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;//臨時數(shù)組
  int i, n; //n表示元素個數(shù)
  char fname[ST]; //臨時存儲字符串

  printf("請輸入元素個數(shù):");
  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) ); //申請動態(tài)數(shù)組//////
  if(ptr == NULL){
    fprintf(stderr, "FAIL TO ASK FOR MEMORY SPACE\n");
    exit(EXIT_FAILURE);
  }

  printf("請輸入原文件名:");
  if( s_gets(fname, ST) == NULL ){
    fprintf(stderr, "Fail to get a string\n");
    exit(EXIT_FAILURE);
  }

  sor = fopen(fname, "r"); //打開包含數(shù)據(jù)的源文件
  if(sor == NULL){
    fprintf(stderr, "Fail to open the source file\n");
    exit(EXIT_FAILURE);
  }

  for(i = 0; i < n; i++) //獲取數(shù)據(jù)到動態(tài)數(shù)組
    if( fscanf(sor, "%d", &ptr[i]) != 1 ){
      fprintf(stderr, "Fail to get the data\n");
      exit(EXIT_FAILURE);
    }

  mergesort(ptr, n); //排序

  printf("請輸入要保存數(shù)據(jù)的文件名:");
  if( s_gets(fname, ST) == NULL ){
    fprintf(stderr, "Fail to get a string\n");
    exit(EXIT_FAILURE);
  }

  dest = fopen(fname, "w"); //打開目標文件
  if(dest == NULL){
    fprintf(stderr, "Fail to open the destination file\n");
    exit(EXIT_FAILURE);
  }

  for(i = 0; i < n; i++){ //輸出數(shù)據(jù)到目標文件
    if( fprintf(dest, "%d\t", ptr[i]) < 0 ){
      fprintf(stderr, "Fail to save the data\n");
      exit(EXIT_FAILURE);
    }
    if( ((i + 1) % 10) == 0){ //如果寫滿10個就換行
      if( fprintf(dest, "\n") < 0 ){
        fprintf(stderr, "Fail to save the data\n");
        exit(EXIT_FAILURE);
      }
    }
  }

  if( fclose(sor) != 0 ){ //關閉源文件
    fprintf(stderr, "Fail to close the source file\n");
    exit(EXIT_FAILURE);
  }
  if( fclose(dest) != 0 ){ //關閉目標文件
    fprintf(stderr, "Fail to close the destination file\n");
    exit(EXIT_FAILURE);
  }
  free(ptr); //釋放內存

  printf("成功完成!\n請按任意鍵繼續(xù)^ ^\b\b");

  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\n");
      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);  //左子數(shù)組排序
    _mergesort(ar, mid + 1, end, temp);  //右子數(shù)組排序
    merge(ar, start, mid, end, temp);  //合并子數(shù)組
  }
}

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] != '\0') //如果沒有到輸入字符串結束
      i++;  //遞增
    if(st[i] == '\n') //如果字符串最后一個字符是'\n'
      st[i] = '\0'; //把它變成'\0'
    else //否則緩沖區(qū)內還有一部分超出讀取范圍的字符沒有被讀取
      while(getchar() != '\n') //把這些字符讀取完(清空緩沖區(qū))
        continue;
  }

  return re;
}

總結

以上就是用C語言實現(xiàn)從文本文件中讀取數(shù)據(jù)后進行排序功能的全部內容,閱讀這篇文章后,大家自己進行調試運行,相信會對于學習C語言的朋友們很有幫助的。

上一篇:C語言 結構體和指針詳解及簡單示例

欄    目:C語言

下一篇:C語言 指針與二維數(shù)組詳解

本文標題:用C語言實現(xiàn)從文本文件中讀取數(shù)據(jù)后進行排序的功能

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2095.html

網頁制作CMS教程網絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網 版權所有