Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C中的结构和选择排序按字符串对文件进行排序_C_Arrays_Sorting_Structure - Fatal编程技术网

如何使用C中的结构和选择排序按字符串对文件进行排序

如何使用C中的结构和选择排序按字符串对文件进行排序,c,arrays,sorting,structure,C,Arrays,Sorting,Structure,已排序的打印不正确,可能是排序错误。我也不确定如何将每个字符串中的平均分数放在表中。 以下是示例输出: Original: +-------------------------+--------------+------+------+---------+---------+-------+-----+ | Student Name|Identification|Exam 1|Exam 2|Project 1|Project 1|Average|Grade| +-----------------

已排序的打印不正确,可能是排序错误。我也不确定如何将每个字符串中的平均分数放在表中。 以下是示例输出:

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);
    }
}
}
原件:
+-------------------------+--------------+------+------+---------+---------+-------+-----+
|学生姓名|身份|考试1 |考试2 |项目1 |项目1 |平均|成绩|
+-------------------------+--------------+------+------+---------+---------+-------+-----+
|霍尔特坎普,诺曼| N21102485 | 83 | 61 | 62 | 78 | 71.00 | C|
|贝洛米,沙万达| N94185259 | 74 | 96 | 80 | 98 | 87.00 | B|
|克拉特,洛里斯| N68760306 | 83 | 68 | 93 | 70 | 78.50 | C|
|鲁恩特里,Edythe | N76813896 | 98 | 91 | 90 | 81 | 90.00 | A|
|马里兰州瓦尔德克| N44293872 | 88 | 100 | 70 | 87 | 86.25 | B|
+-------------------------+--------------+------+------+---------+---------+-------+-----+
分类:
+-----+-------------------------+--------------+------+------+---------+---------+-------+-----+
|索引|学生姓名|身份|考试1 |考试2 |项目1 |项目1 |平均|成绩|
+-----+-------------------------+--------------+------+------+---------+---------+-------+-----+
|1 |朗特里,Edythe | N76813896 | 98 | 91 | 90 | 81 | 90.00 | A|
|2 | Bellomy,Shavonda | N94185259 | 74 | 96 | 80 | 98 | 87.00 | B|
|3 |马里兰州瓦尔德克| N44293872 | 88 | 100 | 70 | 87 | 86.25 | B|
|4 |杂波,洛里斯| N68760306 | 83 | 68 | 93 | 70 | 78.50 | C|
|5 |霍尔特坎普,诺曼| N21102485 | 83 | 61 | 62 | 78 | 71.00 | C|
+-----+-------------------------+--------------+------+------+---------+---------+-------+-----+
#包括
#包括
#包括
#包括
//结构将文件中的每一行分组
结构等级记录
{
int iIndex;//文件上的索引
char cStudentName[26];//学生姓名字段
字符为学生编号[9];/“学生编号”字段
int IEXAMGroupped[2];/“考试1”…“考试2”字段
int IProjects[2];
浮动平均值;
char cStudentGD;//“等级”字段
};
作废printUnsortedStringFromFile(整数金额、结构等级记录A[]);
作废printSortedStringFromFile(整数金额、结构等级记录A[]);
void flushScanf();
int main()
{
文件*spData=fopen(“records.ssv”,“r”);
int ch,线的数量=0;
做
{
ch=fgetc(spData);
如果(ch='\n')
_行数++;
}while(ch!=EOF);
if(ch!='\n'&&u行数!=0)
_行数++;
fclose(spData);
printf(“file records.ssv中有%d行。\n”,行数);
int amount=行数;
结构等级记录[金额];
printUnsortedStringFromFile(金额,A);
printSortedStringFromFile(金额,A);
//flushScanf();
返回0;
}
/*
*函数名称:printUnsortedStringFromFile
*
*输入参数:获取数组
*
*描述:此功能打印grades.csv中未排序的原始列表
*
*返回值:void
*/
作废printUnsortedStringFromFile(整数金额、结构等级记录[amount])
{
文件*spData;
spData=fopen(“records.ssv”,“r”);
if(spData==NULL)
{
fprintf(stderr,“打开文件records.ssv时出错。\n”);
出口(1);
}
printf(“+------------------+--------+--------+--------+--------+--------+----+----+----++----+\n”);
printf(“|学生姓名|身份|考试1 |考试2 |项目1 |项目2 |成绩|\n”);
printf(“+------------------+--------+--------+--------+--------+--------+----+----+----++----+\n”);
char sLine[amount];//读取一行的本地字符串
int j=0;//存储索引
while((fgets(sLine,amount,spData))!=NULL)
{
sscanf(sLine,“%20[^;];%9[^;];%5d;%5d;%5d;%5d;%c”,
A[j].cStudentName,A[j].iStudentINDnum,&(A[j].iexamgroupped[0]),&(A[j].iexamgroupped[1]),
&(A[j].iProjectGrouped[0]),&(A[j].iProjectGrouped[1]),&(A[j].cStudentGD));
如果(strcmp(A[j].cStudentName,”)>0){
printf(“|%20s |%9s |%5d |%5d |%5d |%5d |%c |\n”,
A[j].cStudentName,A[j].IStudentName,A[j].IEXAMGroupped[0],A[j].IEXAMGroupped[1],
A[j].iProjectGrouped[0],A[j].iProjectGrouped[1],A[j].cStudentGD);
}
j++;//下一行
}
printf(“+------------------+--------+--------+--------+--------+--------+----+----+----++----+\n”);
如果(fclose(spData)=EOF)
{
fprintf(stderr,“关闭文件records.ssv时出错。\n”);
出口(2);
}
}
/*
*函数名称:printSortedStringFromFile
*
*输入参数:取整数金额,结构等级记录A
*
*描述:此函数打印文件grades.csv的排序版本,忽略
*检查值,并给每个字符串一个索引号
*
*返回值:void
*/
作废printSortedStringFromFile(整数金额、结构等级记录A[金额])
{
文件*spData;
spData=fopen(“records.ssv”,“r”);
if(spData==NULL)
{
fprintf(stderr,“打开文件grades.csv时出错。\n”);
出口(1);
}
碳线[数量];
int iLine=0,iRow;
int x;
结构等级记录grRow;
while((fgets(sLine,amount,spData))!=NULL)
{
//提取一行并将其存储到grRow中
sscanf(sLine,“%20[^;];%9[^;];%5d;%5d;%5d;%2.2f;%c”,
grRow.cStudentName、grRow.iStudentINDnum、&(grRow.iexamgroupped[0])、&(grRow.iexamgroupped[1]),
&(grRow.iProjectGrouped[0]),&(grRow.iProjectGrouped[1]),&(grRow.fAverage),&(grRow.cStudentGD));
//保留该行的行索引
grRow.iIndex=直线;
//目标循环=选择排序算法
对于(iRow=0;iRowsscanf(sLine, "%20[^;]…
sscanf(sLine, "… %2.2f ; …
iLine = amount-1;   // You know that your 'amount' is 1 too high, don't you?