排序打印不正确,排序可能有误。我也不确定如何在每个字符串的平均成绩中排名。
下面是输出示例:
Original:
+-------------------------+--------------+------+------+---------+---------+-------+-----+
| Student Name|Identification|Exam 1|Exam 2|Project 1|Project 1|Average|Grade|
+-------------------------+--------------+------+------+---------+---------+-------+-----+
| Holtkamp, Norman| N21102485| 83| 61| 62| 78| 71.00| C|
| Bellomy, Shavonda| N94185259| 74| 96| 80| 98| 87.00| B|
| Clutter, Loris| N68760306| 83| 68| 93| 70| 78.50| C|
| Rountree, Edythe| N76813896| 98| 91| 90| 81| 90.00| A|
| Waldeck, Marylee| N44293872| 88| 100| 70| 87| 86.25| B|
+-------------------------+--------------+------+------+---------+---------+-------+-----+
Sorted:
+-----+-------------------------+--------------+------+------+---------+---------+-------+-----+
|Index| Student Name|Identification|Exam 1|Exam 2|Project 1|Project 1|Average|Grade|
+-----+-------------------------+--------------+------+------+---------+---------+-------+-----+
| 1 | Rountree, Edythe| N76813896| 98| 91| 90| 81| 90.00| A|
| 2 | Bellomy, Shavonda| N94185259| 74| 96| 80| 98| 87.00| B|
| 3 | Waldeck, Marylee| N44293872| 88| 100| 70| 87| 86.25| B|
| 4 | Clutter, Loris| N68760306| 83| 68| 93| 70| 78.50| C|
| 5 | Holtkamp, Norman| N21102485| 83| 61| 62| 78| 71.00| C|
+-----+-------------------------+--------------+------+------+---------+---------+-------+-----+
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
//Struct groups each line in the file
struct gradesRecord
{
int iIndex; // index on the file
char cStudentName[26]; // student name field
char iStudentINDnum[9]; // 'Student id ' field
int iExamGrouped[2]; // 'Exam 1'..'Exam 2' fields
int iProjectGrouped[2];
float fAverage;
char cStudentGD; // 'Grade' field
};
void printUnsortedStringFromFile(int amount, struct gradesRecord A[]);
void printSortedStringFromFile(int amount, struct gradesRecord A[]);
void flushScanf();
int main()
{
FILE* spData = fopen("records.ssv", "r");
int ch, number_of_lines = 0;
do
{
ch = fgetc(spData);
if (ch == '\n')
number_of_lines++;
} while (ch != EOF);
if (ch != '\n' && number_of_lines != 0)
number_of_lines++;
fclose(spData);
printf("There are %d lines in file records.ssv . \n", number_of_lines);
int amount = number_of_lines;
struct gradesRecord A[amount];
printUnsortedStringFromFile(amount, A );
printSortedStringFromFile(amount, A );
//flushScanf();
return 0;
}
/*
* Function Name: printUnsortedStringFromFile
*
* Input Parameters: takes array A
*
* Description: This fuction prints the original list that was unsorted in grades.csv
*
* Return Value: void
*/
void printUnsortedStringFromFile(int amount, struct gradesRecord A[amount])
{
FILE *spData;
spData = fopen("records.ssv", "r");
if(spData == NULL)
{
fprintf(stderr, "Error opening the file records.ssv.\n");
exit(1);
}
printf("+---------------------+--------------+------+------+---------+---------+-----+\n");
printf("| Student Name|Identification|Exam 1|Exam 2|Project 1|Project 2|Grade|\n");
printf("+---------------------+--------------+------+------+---------+---------+-----+\n");
char sLine[amount]; //local string to read one row
int j = 0; //storage index
while((fgets(sLine, amount, spData)) != NULL)
{
sscanf(sLine, "%20[^;] ; %9[^;] ; %5d ; %5d ; %5d ; %5d ; %c",
A[j].cStudentName, A[j].iStudentINDnum, &(A[j].iExamGrouped[0]), &(A[j].iExamGrouped[1]),
&(A[j].iProjectGrouped[0]), &(A[j].iProjectGrouped[1]), &(A[j].cStudentGD));
if(strcmp(A[j].cStudentName, " ")> 0){
printf("| %20s| %9s| %5d| %5d| %5d| %5d| %c| \n",
A[j].cStudentName, A[j].iStudentINDnum, A[j].iExamGrouped[0], A[j].iExamGrouped[1],
A[j].iProjectGrouped[0], A[j].iProjectGrouped[1], A[j].cStudentGD);
}
j++; // next row
}
printf("+---------------------+--------------+------+------+---------+---------+-----+\n");
if (fclose(spData) == EOF)
{
fprintf(stderr, "Error closing the file records.ssv. \n");
exit(2);
}
}
/*
* Function Name: printSortedStringFromFile
*
* Input Parameters: takes int amount, struct gradesRecord A
*
* Description: This function prints the sorted version of the file grades.csv omitting
* the exam values and giving each string a index number
*
* Return Value: void
*/
void printSortedStringFromFile(int amount, struct gradesRecord A[amount])
{
FILE *spData;
spData = fopen("records.ssv", "r");
if(spData == NULL)
{
fprintf(stderr, "Error opening the file grades.csv.\n");
exit(1);
}
char sLine[amount];
int iLine = 0, iRow;
int x;
struct gradesRecord grRow;
while((fgets(sLine, amount, spData)) != NULL)
{
// extract one Row and store it into grRow
sscanf(sLine, "%20[^;] ; %9[^;] ; %5d ; %5d ; %5d ; %5d ; %2.2f ; %c",
grRow.cStudentName, grRow.iStudentINDnum, &(grRow.iExamGrouped[0]), &(grRow.iExamGrouped[1]),
&(grRow.iProjectGrouped[0]), &(grRow.iProjectGrouped[1]), &(grRow.fAverage), &(grRow.cStudentGD));
// keep the line index of that row
grRow.iIndex = iLine;
// target loop = Selection sort algorithm
for (iRow = 0; iRow < iLine - 1; iRow++){
for(x = iRow + 1; x < iLine; x++){
if (A[iRow].cStudentGD < A[x].cStudentGD) {
struct gradesRecord tmp = A[iRow];
A[iRow] = A[x];
A[x] = tmp;
}
}
}
int j = 0;
printf("+-----+---------------------+--------------+------+------+---------+---------+-------+-----+\n");
printf("|Index| Student Name|Identification|Exam 1|Exam 2|Project 1|Project 2|Average|Grade|\n");
printf("+-----+---------------------+--------------+------+------+---------+---------+-------+-----+\n");
int index;
while (j < amount - 1)
{
index = j+1;
printf("| %4d| %20s| %9s| %5d| %5d| %5d| %5d| %2.2f| %c| \n",
index, A[j].cStudentName, A[j].iStudentINDnum, A[j].iExamGrouped[0], A[j].iExamGrouped[1],
A[j].iProjectGrouped[0], A[j].iProjectGrouped[1], A[j].fAverage, A[j].cStudentGD);
j++;
}
printf("+----+---------------------+--------------+------+------+---------+---------+-----+\n");
if (fclose(spData) == EOF)
{
fprintf(stderr, "Error closing the file records.ssv. \n");
exit(2);
}
}
}
这是“records.ssv”的信息
莱利亚装甲车;N58288536;89;82;91;65;B
巴斯勒,詹妮弗;N42495906;74;71;87;91;B
莱顿,辛迪;N66735910;67;93;76;79;C
卡洛琳主教;N85576519;86;94;92;69;B
鲁西,卡利;N55890919;86;84;88;87;B
花呢,米尔塔;N94974972;62;95;85;92;B
方特诺,玫瑰花结;N44585447;98;62;74;74;C
诺曼霍尔特坎普;N21102485;83;61;62;78;C
贝洛米,沙文达;N94185259;74;96;80;98;B
杂波,洛里斯;N68760306;83;68;93;70;C
朗特里,埃德斯;N76813896;98;91;90;81;A
马里兰州沃尔德克;N44293872;88;100;70;87;B
普特南,图耶;N82771281;69;99;68;67;C
迈克尔斯,阿奈特;N33948917;86;65;99;64;C
斯特劳德,温多林;N05586646;86;64;80;97;B
蒙图法,梅尔文;N36545740;80;61;74;92;C
费伊,莱塔;N61908241;73;89;71;68;C
德鲁纳,沃恩;N74322300;94;69;67;60;C
玛格丽塔韦弗;N37176367;95;92;95;70;B
白金汉,莱纳;N87562246;95;64;89;71;C
弗里德利,维罗纳;N53223806;67;83;71;61;C
格拉迪斯埃巴;N66138130;89;70;87;90;B
莫尼卡提琴诺尔;N40314334;88;62;62;100;C
多兰,中篇小说;N24182986;78;62;65;71;D
雷耶,马西;N64652923;89;95;72;76;B
肯德尔,罗马;N22064372;91;84;72;88;B
布兰诺克,亨利特;N19795353;67;92;63;90;C
莱恩,克林特;N83838870;88;83;82;77;B
莱琴巴赫,沙伦;N02253867;81;96;91;73;B
埃弗斯,汉娜;N05833153;75;79;75;98;B
阿马尔·雷吉纳;N52372967;95;73;73;89;B
莱特尔,德斯蒙德;N81006603;75;66;61;71;D
也门,In;N30566266;89;99;98;89;A
加林,尼雷达;N05192538;89;92;74;99;B
汉娜,阿森塔;N39624931;91;80;72;80;B
丹科,米根;N47365488;79;67;86;65;C
乌里奇,处女;N57019166;70;82;72;88;C
埃梅琳达,查普拉;N38233556;93;83;60;87;B
哈普,米娜;N46726472;84;98;60;66;C
德洛里斯苏特尔特;N88538002;86;71;82;82;B
巴勒莫,肯纳;N44857147;72;69;83;75;C
美国,威德曼;N29831009;86;78;89;81;B
塞义德,鲁斯;N47933985;78;62;99;99;B
科林,布雷尔;N75299461;77;85;99;99;A
埃迪赫克特;N58264115;92;96;96;89;A
丹佛波利;N77063394;99;95;65;95;B
韦弗,布兰奇;N59717716;66;80;89;80;C
基布尔,格林达;N68212959;94;95;66;94;B
威利斯柯克;N17878125;71;60;87;79;C
利维赛,阿里;N91011529;89;60;60;84;C
最佳答案
在printUnsortedStringFromFile()
和printSortedStringFromFile()
中
sscanf(sLine, "%20[^;]…
要读入
cStudentName
,但20
字符不足以读入Reichenbach, Sharleen
。既然定义了char cStudentName[26];
,那么25
的最大字段宽度就足够了。你有
sscanf(sLine, "… %2.2f ; …
在
printSortedStringFromFile()
中读取,但在grRow.fAverage
中没有此字段,并且从未初始化records.ssv
。你说分类可能是错的是对的。这是因为在排序之前没有设置
A[j].fAverage
:iLine = amount-1; // You know that your 'amount' is 1 too high, don't you?
关于c - 如何使用C中的结构和选择排序按字符串对文件排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40924100/