C程序将文件存储到锯齿状数组中并对其进行排序

标签 c sorting jagged-arrays

我正在尝试创建 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#有很大不同。 抱歉我的英语不好,因为它不是我的母语。非常感谢您对我的帮助。

最佳答案

#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;
            }
        }
    }
}

关于C程序将文件存储到锯齿状数组中并对其进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26022731/

相关文章:

c - 使用 FFTW 执行 2D DFT 时出错

c - 为什么 rand() 比 arc4random() 快得多?

c++ - 迭代合并排序,与冒泡排序速度相同

c# - 在锯齿状数组上手动索引?

c - 是否有 GetThemeColor/Visual Styles API 的有效参数组合列表

c - 输入 Kernighan 和 Ritchie 的例子

c# - 将字符串转换为可排序数字

C#最高字符串

arrays - Julia 的 Vector{Vector{T}} 是否连续存储在内存中?

javascript - 检查某些内容是否不为空后,我收到空或未定义类型错误。如何?