2014-09-24 100 views
-1

我想創建C程序來讀取文本文件並按升序排序。文本文件的例子是C程序存儲文件鋸齒陣列和排序它

2 
3; 2, 5, 7 
6; 4, 7, 8, 9, 5, 2 

用第一行表示的行數,數字後的「;」指示元素的每行和元素用「,」分隔。 所以我的想法是創建一個動態鋸齒陣列作爲第一個數字的行,然後指出每個行與元素不同的數組。首先對指針數組排序,然後對每個數組的元素進行排序這是我迄今

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int SortLists(); 

int main() 
{ 
    int i,j = 0; 
    char filename[10];   //name of the file 

    char line[100]; 
    int rows = 3;    //I have to initialized this to test my code first 
    int cols; 

    int **jaggedArr;       //jagged array 
    jaggerArr = malloc (rows*sizeof(int*)) ; 

    printf("Enter the file name with .txt : "); 
    scanf("%s", filename); 

    FILE *filePtr = fopen(filename, "r"); 

    int num; 

    if (filePtr != NULL) 
    {  
     while (fgets(line, sizeof(line), filePtr) != NULL) //read each line of the text 
     {    
      cols = atoi(strtok(line, ";"));  //use strtk to break elements 
      for (i = 0; i < rows; i++) 
      { 
       for (j = 0; j < cols; j++) 
       { 
        jaggedArr[i][j] = atoi(strtok(line, ",")); //parse into the jagged array         
       } 
      }       
     }  
    fclose(filePtr);  
    } 
} 


int SortLists(int list[], int size)  //sort method 
{ 
    int i,j,temp; 

    for (i = 0; i < size; ++i) 
    { 
     for (j = i + 1; j < size; ++j) 
     { 
      if (list[i] > list[j]) 
      { 
       temp = list[i]; 
       list[i] = list[j]; 
       list[j] = temp; 
      } 
     } 
    } 
} 

試圖同C初學者,我不熟悉指針的想法,這與C#有很多不同。 對不起,我的英語不好,因爲它不是我的第一語言。非常感謝你幫助我。

+1

反覆'的strtok(行 「」)'不會做你想做的。由';'分隔的開啓者看起來很有希望,但是行處理的其餘部分應該用'strtok(NULL,',')'完成。也許爲這些行分配一些*內存* – WhozCraig 2014-09-24 17:19:04

+0

感謝您花時間回答我的問題。通過分配內存,你的意思是使用行中元素的數量來初始化數組的大小? – user4075830 2014-09-24 17:54:08

回答

1
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define S_(x) #x 
#define S(x) S_(x) 

void SortLists(int list[], int size); 

int main(void){ 
    char filename[FILENAME_MAX+1]; 
    char line[100]; 
    int rows, cols; 

    printf("Enter the file name with .txt : "); 
    scanf("%" S(FILENAME_MAX) "[^\n]%*c", filename); 
    FILE *filePtr = fopen(filename, "r"); 
    if(!filePtr) 
     return EXIT_FAILURE; 

    fscanf(filePtr, "%d ", &rows); 
    int **jaggedArr; 
    jaggedArr = malloc (rows * sizeof(int*)); 
    int *sizeArr = malloc(rows * sizeof(int)); 

    int r = 0, c; 
    while (fgets(line, sizeof(line), filePtr) != NULL){ 
     sizeArr[r] = cols = atoi(strtok(line, ";")); 
     jaggedArr[r] = malloc(cols * sizeof(int)); 
     for (c = 0; c < cols; ++c){ 
      jaggedArr[r][c] = atoi(strtok(NULL, ",")); 
     } 
     SortLists(jaggedArr[r++], cols); 
    } 
    fclose(filePtr); 
    //check print and deallocation 
    for(r = 0;r < rows; ++r){ 
     for(c = 0; c < sizeArr[r]; ++c) 
      printf("%d ", jaggedArr[r][c]); 
     printf("\n"); 
     free(jaggedArr[r]); 
    } 
    free(jaggedArr); 
    free(sizeArr); 
    return 0; 
} 

void SortLists(int list[], int size){ 
    int i,j,temp; 

    for (i = 0; i < size-1; ++i){ 
     for (j = i + 1; j < size; ++j){ 
      if (list[i] > list[j]){ 
       temp = list[i]; 
       list[i] = list[j]; 
       list[j] = temp; 
      } 
     } 
    } 
} 
+0

謝謝你幫助我。請問,'fscanf'(filePtr,「%d」,&rows);''獲得文本的行號,因爲它是文本文件中的第一個數字嗎?另外,什麼是指針'* sizeArr'呢?嘗試使用文本文件運行你的代碼,但它的工作不正常,所以這裏一定缺少一些東西 – user4075830 2014-09-24 18:38:46

+0

@ user4075830 _it是文本文件中的第一個數字_是的_什麼是指針* sizeArr的做法?_你需要保持大小因爲每一行的大小不同 – BLUEPIXY 2014-09-24 18:51:28

+0

@ user4075830 [DEMO](http://ideone.com/9GFx54) – BLUEPIXY 2014-09-24 18:55:49

0

對於額外的問題。

#include <stdio.h> 

void SortLists(int list[], int size); 
void SortRows(int *jaggedArr[], int size, int *rowSize); 

int main(void){ 
    int row1[] = {4,7,8,9,5,2}; 
    int row2[] = {2,5,7}; 
    int *jaggedArr[] = { row1, row2}; 
    int rows = 2; 
    int sizeArr[] = {6,3}; 
    int i, r, c; 
    for(i=0;i<rows;++i) 
     SortLists(jaggedArr[i], sizeArr[i]); 
    for(r = 0;r < rows; ++r){ 
     for(c = 0; c < sizeArr[r]; ++c) 
      printf("%d ", jaggedArr[r][c]); 
     printf("\n"); 
    } 
    printf("\n"); 
    SortRows(jaggedArr, rows, sizeArr); 
    for(r = 0;r < rows; ++r){ 
     for(c = 0; c < sizeArr[r]; ++c) 
      printf("%d ", jaggedArr[r][c]); 
     printf("\n"); 
    } 
    return 0; 
} 

void SortLists(int list[], int size){ 
    int i,j,temp; 

    for (i = 0; i < size-1; ++i){ 
     for (j = i + 1; j < size; ++j){ 
      if (list[i] > list[j]){ 
       temp = list[i]; 
       list[i] = list[j]; 
       list[j] = temp; 
      } 
     } 
    } 
} 
void SortRows(int *jaggedArr[], int size, int *rowSize){ 
    int i,j,temp,*tempp; 

    for (i = 0; i < size-1; ++i){ 
     for (j = i + 1; j < size; ++j){ 
      if (rowSize[i] > rowSize[j]){ 
       //swap in pairs 
       temp = rowSize[i]; 
       rowSize[i] = rowSize[j]; 
       rowSize[j] = temp; 
       tempp = jaggedArr[i]; 
       jaggedArr[i] = jaggedArr[j]; 
       jaggedArr[j] = tempp; 
      } 
     } 
    } 

} 

#include <stdio.h> 

void SortLists(int list[], int size); 
void SortRows(int *jaggedArr[], int size); 

int main(void){ 
    int row1[] = {6, 4,7,8,9,5,2};//The first element represents the number of elements. 
    int row2[] = {3, 2,5,7}; 
    int *jaggedArr[] = { row1, row2}; 
    int rows = 2; 
    //int sizeArr[] = {6,3};//Not required 
    int i, r, c; 
    for(i=0;i<rows;++i) 
     SortLists(jaggedArr[i]+1, jaggedArr[i][0]); 

    SortRows(jaggedArr, rows); 
    for(r = 0;r < rows; ++r){ 
     for(c = 1; c <= jaggedArr[r][0]; ++c) 
      printf("%d ", jaggedArr[r][c]); 
     printf("\n"); 
    } 
    return 0; 
} 

void SortLists(int list[], int size){ 
    int i,j,temp; 

    for (i = 0; i < size-1; ++i){ 
     for (j = i + 1; j < size; ++j){ 
      if (list[i] > list[j]){ 
       temp = list[i]; 
       list[i] = list[j]; 
       list[j] = temp; 
      } 
     } 
    } 
} 
void SortRows(int *jaggedArr[], int size){ 
    int i,j,*tempp; 

    for (i = 0; i < size-1; ++i){ 
     for (j = i + 1; j < size; ++j){ 
      if (jaggedArr[i][0] > jaggedArr[j][0]){ 
       tempp = jaggedArr[i]; 
       jaggedArr[i] = jaggedArr[j]; 
       jaggedArr[j] = tempp; 
      } 
     } 
    } 

}