c - 对txt中的结构化数据进行排序

标签 c arrays sorting structure

我的问题是对从 txt 文件中获取的一些数字进行排序。 当我编译文件时,程序停止工作。

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



    struct student_grades{
    int number;
    char name[10];
    char surname[10];
    int grade;
    };

    typedef struct student_grades stgrade;


    void bubble_sort(int list[], int n){ //Line 16
  long c, d, t;

  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (list[d] > list[d+1])
      {
        /* Swapping */

        t         = list[d];
        list[d]   = list[d+1];
        list[d+1] = t;
      }
    }
  }
}


int main()
{
    int i=0;
    FILE *stu;  // file tipinde değişken tutacak
    FILE *stub;
    stu= fopen("student.txt","r");
    stub= fopen("stu_order.txt","a");


    stgrade stg[12];
        if(stu!=NULL){
        while(!feof(stu))
        {
            fscanf(stu,"%d",&stg[i].number);
            fscanf(stu,"%s",stg[i].name);
            fscanf(stu,"%s",stg[i].surname);
            fscanf(stu,"%d",&stg[i].grade);
            //fprintf(stub,"%d  %s  %s  %d\n",stg[i].number,stg[i].name,stg[i].surname,stg[i].grade);

               ++i;
        }
        bubble_sort(stg->number,12);    //Line 59

        fprintf(stub,"%d  %s  %s  %d\n",stg[1].number,stg[1].name,stg[1].surname,stg[1].grade); //control that is bubble  success?  

    }
    else
      printf("File Not Found");

    fclose(stu);
    fclose(stub);
    return 0;  

首先我写了第59行

bubble_sort(stg.number,12);    

像这样。但它会出错并且无法编译。我用

更改它
bubble_sort(stg->number,12);    

它已编译但停止工作并收到警告

格式化输出:
在函数“main”中:
59 3 [警告] 传递“bubble_sort”的参数 1 使指针来自整数而不进行强制转换[默认启用]
16 6 [注意] 预期为“int *”,但参数的类型为“int”

学生.txt

80701056 Sabri Demirel 45  
52801022 Burak Erkin 68  
13801045 Umut Korkmaz 88  
74801334 Semih Potuk 58  
15678544 Enes Sezer 76  
42125884 Ahmet Can 84  
12355488 Emre Ozdemir 47  
18744125 Ugur Yildiz 64  
62184111 Mustafa Ozturk 80  
18412548 Ugur Akkafa 72  
94541771 Hakan Aktas 92  
36945245 Fatih Yagci 98  

最佳答案

这是基于您的文件和结构 stgrade 的代码,它使用复杂度等于 O(log(n)) 的快速排序 ( qsort 函数)而不是库 stdlib.h 中的冒泡排序,它会生成所需的输出

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> //qsort function

struct student_grades{
    int number;
    char name[10];
    char surname[10];
    int grade;
};

typedef struct student_grades stgrade;

// the compare function is used by the qsort function to sort the structures 
// according to the grades values
int compare(const void* a, const void* b) 
{
return (*(stgrade *)a).grade - (*(stgrade *)b).grade;   
}

int main(int argc, char *argv[])
{
    FILE *stub, *stu;
    stu= fopen("student.txt","r");
    stub= fopen("stu_order.txt","a");

    stgrade tab[12]; // array that will contain the structures from student.txt file
    int i=0;
    for (i=0;i<12;i++)
    {
       // put the structures on the array
        fscanf(stu,"%d %s %s %d",&tab[i].number,tab[i].name,tab[i].surname,&tab[i].grade);
    }
      // use the qsort function that will sort the structures
    qsort(tab,12,sizeof(stgrade),compare);

    //loop to write the result on the output file
    for (i=0;i<12;i++)
    {
         // the write will be via the function fprintf 
        fprintf(stub,"%d %s %s %d\n",tab[i].number,tab[i].name,tab[i].surname,tab[i].grade);
    }

   // Check the output file :)

    return 0;
}

可以通过检查打开的文件来改进代码!

关于c - 对txt中的结构化数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27870621/

相关文章:

c - 使用 fwrite 将结构写入文件时出现 Valgrind 错误 - 系统调用参数 write(buf) 指向未初始化的字节

c - Visual Studio 13 中的 NASM 设置

java - 在Java中,如何在单个数组中表示二维坐标系?

c# - 在数据库内部排序还是在代码后面排序?哪个最好?

java - 使用 JRE 1.7 排序时哪个类抛出 IllegalArgumentException?

c# - .Sort(Function(x, y) -> 当没有要排序的内容时重新排列列表(即所有排序字段具有相同的值)

c - 如何在c中将任意十六进制转换为ascii?

c - 编写的逻辑编码不正确

javascript - 嵌套对象和数组解构

C++ 菜单和数组中的字符串搜索