从txt中排序结构化数据
Sorting structured data from txt
我的问题是对从 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'
student.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
嗯,你的编译器的输出是这样的:
您的 bubble_sort
函数接受一个指针,但您将其传递给一个整数。
指针、地址、数组和整数是重要的C 概念,恐怕您必须温习一下您的C 基础知识;阅读一些参考代码将帮助您了解如何解决您的问题。我知道你很可能是 C 的新手,这个答案是一个答案,但不能立即解决你的问题,但是这里有太多的东西要解释,如果其他人出现并且将您的问题标记为低质量。
这是一个基于您的文件和结构的代码 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;
}
可以通过检查打开的文件来改进代码!!
there were lots of problems with the code
however the following should have all those problems corrected
and includes error checking
#include <stdio.h>
#include <stdlib.h> // exit
#include <string.h> // memcpy
#define MAX_GRADES (12)
struct student_grades
{
int number;
char name[10];
char surname[10];
int grade;
};
void bubble_sort(struct student_grades* pList, int n)
{ //Line 16
long c, d;
struct student_grades t;
for (c = 0 ; c < (n - 1); c++)
{
for (d = 0 ; d <(n - c - 1); d++)
{
if (pList[d].number > pList[d+1].number)
{
/* Swapping */
memcpy(&t, &pList[d], sizeof(struct student_grades));
memcpy(&pList[d], &pList[d+1], sizeof(struct student_grades));
memcpy(&pList[d+1], &t, sizeof(struct student_grades));
} // end if
} // end for
} // end for
} // end function: bubble_sort
int main()
{
FILE *stu = NULL; // file tipinde değişken tutacak
FILE *stub = NULL; // rises compiler warning about unused variable
if( NULL == (stu= fopen("student.txt","r")) )
{ // then, fopen failed
perror( "fopen for student.txt failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
if( NULL == (stub= fopen("stu_order.txt","a")) )
{ // then, fopen failed
perror( "fopen for stu_order.txt failed" );
fclose(stu); // cleanup
exit( EXIT_FAILURE );
}
// implied else, fopen successful
struct student_grades stg[MAX_GRADES];
char line[1000]; // should be enough for reading 4 fields
int i=0;
while((i<MAX_GRADES) && fgets(line, sizeof(line), stu) )
{
if( 4 != sscanf(line," %d %s %s %d",
&stg[i].number,
stg[i].name,
stg[i].surname,
&stg[i].grade) )
{ // fscanf failed
perror( "fscanf failed" );
fclose(stu); // cleanup
fclose(stub);
exit( EXIT_FAILURE );
}
// implied else, fscanf successful
//fprintf(stub,"%d %s %s %d\n",stg[i].number,stg[i].name,stg[i].surname,stg[i].grade);
++i;
} // end while
bubble_sort(stg,i); //Line 59
int j;
for(j=0;j<i;j++)
{
fprintf(stub,"%d %s %s %d\n",
stg[1].number,
stg[1].name,
stg[1].surname,
stg[1].grade); //control that is bubble success?
} // end for
fclose(stu); // leanup
fclose(stub);
return 0;
} // end function: main
我的问题是对从 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'
student.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
嗯,你的编译器的输出是这样的:
您的 bubble_sort
函数接受一个指针,但您将其传递给一个整数。
指针、地址、数组和整数是重要的C 概念,恐怕您必须温习一下您的C 基础知识;阅读一些参考代码将帮助您了解如何解决您的问题。我知道你很可能是 C 的新手,这个答案是一个答案,但不能立即解决你的问题,但是这里有太多的东西要解释,如果其他人出现并且将您的问题标记为低质量。
这是一个基于您的文件和结构的代码 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;
}
可以通过检查打开的文件来改进代码!!
there were lots of problems with the code
however the following should have all those problems corrected
and includes error checking
#include <stdio.h>
#include <stdlib.h> // exit
#include <string.h> // memcpy
#define MAX_GRADES (12)
struct student_grades
{
int number;
char name[10];
char surname[10];
int grade;
};
void bubble_sort(struct student_grades* pList, int n)
{ //Line 16
long c, d;
struct student_grades t;
for (c = 0 ; c < (n - 1); c++)
{
for (d = 0 ; d <(n - c - 1); d++)
{
if (pList[d].number > pList[d+1].number)
{
/* Swapping */
memcpy(&t, &pList[d], sizeof(struct student_grades));
memcpy(&pList[d], &pList[d+1], sizeof(struct student_grades));
memcpy(&pList[d+1], &t, sizeof(struct student_grades));
} // end if
} // end for
} // end for
} // end function: bubble_sort
int main()
{
FILE *stu = NULL; // file tipinde değişken tutacak
FILE *stub = NULL; // rises compiler warning about unused variable
if( NULL == (stu= fopen("student.txt","r")) )
{ // then, fopen failed
perror( "fopen for student.txt failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
if( NULL == (stub= fopen("stu_order.txt","a")) )
{ // then, fopen failed
perror( "fopen for stu_order.txt failed" );
fclose(stu); // cleanup
exit( EXIT_FAILURE );
}
// implied else, fopen successful
struct student_grades stg[MAX_GRADES];
char line[1000]; // should be enough for reading 4 fields
int i=0;
while((i<MAX_GRADES) && fgets(line, sizeof(line), stu) )
{
if( 4 != sscanf(line," %d %s %s %d",
&stg[i].number,
stg[i].name,
stg[i].surname,
&stg[i].grade) )
{ // fscanf failed
perror( "fscanf failed" );
fclose(stu); // cleanup
fclose(stub);
exit( EXIT_FAILURE );
}
// implied else, fscanf successful
//fprintf(stub,"%d %s %s %d\n",stg[i].number,stg[i].name,stg[i].surname,stg[i].grade);
++i;
} // end while
bubble_sort(stg,i); //Line 59
int j;
for(j=0;j<i;j++)
{
fprintf(stub,"%d %s %s %d\n",
stg[1].number,
stg[1].name,
stg[1].surname,
stg[1].grade); //control that is bubble success?
} // end for
fclose(stu); // leanup
fclose(stub);
return 0;
} // end function: main