Die Sortierung wird nicht korrekt ausgedruckt, Sortierung ist wahrscheinlich falsch. Ich bin mir auch nicht sicher, wie ich den Durchschnitt der Noten in jeder Saite platzieren soll. Hier ist das Beispiel für die Ausgabe:So sortieren Sie eine Datei nach Zeichenfolgen mit einer Struktur und Auswahl sortieren in C
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);
}
}
}
Hier sind die Informationen, die "records.ssv"
Panzer, Lelia;N58288536;89;82;91;65;B Basler, Jennifer;N42495906;74;71;87;91;B Leaton, Cindi;N66735910;67;93;76;79;C Bishop, Carolyne;N85576519;86;94;92;69;B Lucey, Callie;N55890919;86;84;88;87;B Tweed, Mirta;N94974972;62;95;85;92;B Fontenot, Rosette;N44585447;98;62;74;74;C Holtkamp, Norman;N21102485;83;61;62;78;C Bellomy, Shavonda;N94185259;74;96;80;98;B Clutter, Loris;N68760306;83;68;93;70;C Rountree, Edythe;N76813896;98;91;90;81;A Waldeck, Marylee;N44293872;88;100;70;87;B Putnam, Tuyet;N82771281;69;99;68;67;C Michaels, Arnette;N33948917;86;65;99;64;C Strawder, Wendolyn;N05586646;86;64;80;97;B Montufar, Melvin;N36545740;80;61;74;92;C Fey, Letha;N61908241;73;89;71;68;C Deluna, Vaughn;N74322300;94;69;67;60;C Wever, Marguerita;N37176367;95;92;95;70;B Buckingham, Lena;N87562246;95;64;89;71;C Fridley, Verona;N53223806;67;83;71;61;C Ebarb, Gladis;N66138130;89;70;87;90;B Tichenor, Monika;N40314334;88;62;62;100;C Doran, Novella;N24182986;78;62;65;71;D Relyea, Mazie;N64652923;89;95;72;76;B Kendall, Roma;N22064372;91;84;72;88;B Brannock, Henriette;N19795353;67;92;63;90;C Laine, Clint;N83838870;88;83;82;77;B Reichenbach, Sharleen;N02253867;81;96;91;73;B Evers, Hanna;N05833153;75;79;75;98;B Regina, Amal;N52372967;95;73;73;89;B Lightle, Desmond;N81006603;75;66;61;71;D Yeoman, In;N30566266;89;99;98;89;A Garling, Nereida;N05192538;89;92;74;99;B Hanna, Assunta;N39624931;91;80;72;80;B Danko, Mignon;N47365488;79;67;86;65;C Urich, Virgen;N57019166;70;82;72;88;C Czapla, Ermelinda;N38233556;93;83;60;87;B Happ, Mina;N46726472;84;98;60;66;C Sudderth, Deloris;N88538002;86;71;82;82;B Palermo, Kenna;N44857147;72;69;83;75;C Wiedemann, Usha;N29831009;86;78;89;81;B Saeed, Ruth;N47933985;78;62;99;99;B Burell, Colin;N75299461;77;85;99;99;A Heckert, Edie;N58264115;92;96;96;89;A Polley, Denver;N77063394;99;95;65;95;B Weaver, Blanch;N59717716;66;80;89;80;C Kibble, Glinda;N68212959;94;95;66;94;B Kirker, Willis;N17878125;71;60;87;79;C Livesay, Arie;N91011529;89;60;60;84;C
können Sie einen Tag für die Programmiersprache hinzufügen? Ich nehme an, es ist "C". –
Ja, das ist C, ich nehme an, das Problem liegt in der Auswahl sortieren, aber ich bin mir nicht sicher, was ich dagegen tun soll. – fgdark