C++ 如何修复学生记录计划的GPA输出

C++ 如何修复学生记录计划的GPA输出,c++,C++,在我上一篇文章中,我需要帮助编写一个程序的代码,该程序将从一个.txt文件中输出数据,其中包含每个学生的ID、姓名、课程、学分、分数和GPA StudentRecords.txt 12546 Amy CS1 4 81 13455 Bill CS1 4 76 14328 Jim CS1 4 64 14388 Henry CS3 3 80 15667 Peter CS3 3 45 12546 Amy CS2 4 90 13455 Bill CS2 4 85 14328 Jim

在我上一篇文章中,我需要帮助编写一个程序的代码,该程序将从一个.txt文件中输出数据,其中包含每个学生的ID、姓名、课程、学分、分数和GPA

StudentRecords.txt

12546 Amy   CS1 4 81 
13455 Bill  CS1 4 76
14328 Jim   CS1 4 64
14388 Henry CS3 3 80
15667 Peter CS3 3 45
12546 Amy   CS2 4 90 
13455 Bill  CS2 4 85
14328 Jim   CS2 4 71

12546 Amy   CS3 3 90 
13455 Bill  CS3 3 75
14328 Jim   CS3 3 69
下表用于计算GPA(仅供参考):

有人建议我使用向量而不是数组。所以我重写了我的程序

我能够在一个有组织的数据结构中输出每个学生的ID、姓名、课程、学分和分数。然而,我的平均成绩对每个学生都不正确

我曾尝试将
int grade
分母+=记录[I]相乘。课程[j]。学分
分子+=记录[i]。课程[j]。学分,但没有成功

如果有人能在这方面帮助我,我将不胜感激

我当前的代码:

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

struct Course
{
    string CourseName;
    int Credit;
    int Score;
};

struct Student
{
    int ID;
    string Name;
    vector<Course> Courses;
};

int locateStudent(int id, vector<Student> records);

int main()
{
    fstream inputFile;
    string fileName = "StudentRecords.txt";
    inputFile.open(fileName.c_str(), ios::in);

    int stuList;
    int uniqueID;
    Course tempCourse;

    string name;
    string courseNm;
    int credit;
    int score;

    vector<Student> records;

        if (inputFile.is_open())
        {
            while(!inputFile.eof())
            {
                inputFile >> uniqueID;
                stuList = locateStudent(uniqueID, records);

                if (stuList == -1)
                {
                    Student tempSt;
                    tempSt.ID = uniqueID;
                    inputFile >> tempSt.Name >> courseNm >> credit >> score;
                    tempCourse = {courseNm, credit, score};

                    tempSt.Courses.push_back(tempCourse);
                    records.push_back(tempSt);
                }
                else
                {
                    inputFile >> name;
                    inputFile >> courseNm >> credit >> score;
                    tempCourse = {courseNm, credit, score};
                    records[stuList].Courses.push_back(tempCourse);
                }
            }
            inputFile.close();
        }
        else
        {
            cout << "File cannot be opened.";
        }


        for (unsigned int i = 0; i < records.size(); i++)
        {
            cout << records[i].ID << "\n" << records[i].Name << "\n";
            cout << "==========\n";

            int numerator = 0;
            int denominator = 0;
            for (unsigned int j = 0; j < records[i].Courses.size(); j++)
            {
                int curScore = records[i].Courses[j].Score;
                cout << records[i].Courses[j].CourseName << "  ";
                cout << records[i].Courses[j].Credit << "  ";
                int grade = curScore == 100 ? 4 : (curScore);
                cout << grade << "\n";

                numerator += records[i].Courses[j].Credit;
                denominator += records[i].Courses[j].Credit;
            }

            cout << "==========\n";
            cout << "GPA: " << (double)numerator / denominator << "\n\n";
        }
}

int locateStudent(int uniqueID, vector<Student> records)
{
    int curID;
    for (unsigned int check = 0; check < records.size(); check++)
    {
        curID = records[check].ID;
        if (uniqueID == curID)
        {
            return check;
        }
    }

    return -1;
}
预期产出:

12546
Amy
==========
CS1  4  81
CS2  4  90
CS3  3  90
==========
GPA: 3.64

13455
Bill
==========
CS1  4  76
CS2  4  85
CS3  3  75
==========
GPA: 2.36

14328
Jim
==========
CS1  4  64
CS2  4  71
CS3  3  69
==========
GPA: 1.36

14388
Henry
==========
CS3  3  80
==========
GPA: 3

15667
Peter
==========
CS3  3  45
==========
GPA: 0
12546
Amy
==========
CS1  4  81
CS2  4  90
CS3  3  90
==========
GPA: 3.64

13455
Bill
==========
CS1  4  76
CS2  4  85
CS3  3  75
==========
GPA: 2.36

14328
Jim
==========
CS1  4  64
CS2  4  71
CS3  3  69
==========
GPA: 1.36

14388
Henry
==========
CS3  3  80
==========
GPA: 3

15667
Peter
==========
CS3  3  45
==========
GPA: 0

您正在尝试计算加权平均值,其中权重为积分。分母是对的:它是所有权重的总和。然而,分子是错误的:它应该是每个分数的总和乘以相应的权重

这将给你一个介于0和100之间的平均分数(因为所有分数都属于这个范围);因此,在最后,您必须将其缩小,使其位于0到4(GPA的值范围)之间

生成的代码如下所示:

int分子=0;
int分母=0;
for(unsigned int j=0;jcout您正在尝试计算加权平均值,其中权重为积分。分母是正确的:它是所有权重的总和。但是,分子是错误的:它应该是每个分数乘以其相应权重的总和

这将给你一个介于0和100之间的平均分数(因为所有分数都属于这个范围);因此在最后你必须缩小它的范围,使其介于0和4之间(GPA的值范围)

生成的代码如下所示:

int分子=0;
int分母=0;
for(unsigned int j=0;jcout我猜出来了!感谢@Code学徒的帮助!我编写了float mapGPA(int Score)函数,该函数将帮助计算我的GPA,并添加头文件#include,以便安排GPA的小数点

我的更新代码:

#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
using namespace std;

struct Course
{
    string CourseName;
    int Credit;
    int Score;
};

struct Student
{
    int ID;
    string Name;
    vector<Course> Courses;
};

float mapGPA (int Score)
{
    if (Score < 60)
    {
        return 0;
    }
    else if (Score < 70)
    {
        return 1;
    }
    else if (Score < 80)
    {
        return 2;
    }
    else if (Score < 90)
    {
        return 3;
    }
    else if (Score <= 100)
    {
        return 4;
    }

    return 0;
}

int locateStudent(int id, vector<Student> records);

int main()
{
    fstream inputFile;
    string fileName = "StudentRecords.txt";
    inputFile.open(fileName.c_str(), ios::in);

    int stList;
    int uniqueID;
    Course tempCourse;

    string name;
    string courseNm;
    int credit;
    int score;

    vector<Student> records;

        if (inputFile.is_open())
        {
            while(!inputFile.eof())
            {
                inputFile >> uniqueID;
                stList = locateStudent(uniqueID, records);

                if (stList == -1)
                {
                    Student tempSt;
                    tempSt.ID = uniqueID;
                    inputFile >> tempSt.Name >> courseNm >> credit >> score;
                    tempCourse = {courseNm, credit, score};

                    tempSt.Courses.push_back(tempCourse);
                    records.push_back(tempSt);
                }
                else
                {
                    inputFile >> name;
                    inputFile >> courseNm >> credit >> score;
                    tempCourse = {courseNm, credit, score};
                    records[stList].Courses.push_back(tempCourse);
                }
            }
            inputFile.close();
        }
        else
        {
            cout << "File cannot be opened.";
        }


        for (unsigned int i = 0; i < records.size(); i++)
        {
            cout << records[i].ID << "\n" << records[i].Name << "\n";
            cout << "==========\n";

            int numerator = 0;
            int denominator = 0;
            for (unsigned int j = 0; j < records[i].Courses.size(); j++)
            {
                int curScore = records[i].Courses[j].Score;
                cout << records[i].Courses[j].CourseName << "  ";
                cout << records[i].Courses[j].Credit << "  ";
                int curGrade = curScore == 100 ? 4 : (curScore);
                cout << curGrade << "\n";

                cout << setprecision(3);
                numerator += records[i].Courses[j].Credit * mapGPA(curScore);
                denominator += records[i].Courses[j].Credit;
            }

            cout << "==========\n";
            cout << "GPA: " << (double)numerator / denominator << "\n\n";
        }
}

int locateStudent(int uniqueID, vector<Student> records)
{
    int curID;
    for (unsigned int check = 0; check < records.size(); check++)
    {
        curID = records[check].ID;
        if (uniqueID == curID)
        {
            return check;
        }
    }

    return -1;
}


我算出了!谢谢@Code学徒的帮助!我编写了float mapGPA(int Score)函数,该函数将帮助计算我的GPA,并添加头文件#include,以便安排GPA的小数点

我的更新代码:

#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
using namespace std;

struct Course
{
    string CourseName;
    int Credit;
    int Score;
};

struct Student
{
    int ID;
    string Name;
    vector<Course> Courses;
};

float mapGPA (int Score)
{
    if (Score < 60)
    {
        return 0;
    }
    else if (Score < 70)
    {
        return 1;
    }
    else if (Score < 80)
    {
        return 2;
    }
    else if (Score < 90)
    {
        return 3;
    }
    else if (Score <= 100)
    {
        return 4;
    }

    return 0;
}

int locateStudent(int id, vector<Student> records);

int main()
{
    fstream inputFile;
    string fileName = "StudentRecords.txt";
    inputFile.open(fileName.c_str(), ios::in);

    int stList;
    int uniqueID;
    Course tempCourse;

    string name;
    string courseNm;
    int credit;
    int score;

    vector<Student> records;

        if (inputFile.is_open())
        {
            while(!inputFile.eof())
            {
                inputFile >> uniqueID;
                stList = locateStudent(uniqueID, records);

                if (stList == -1)
                {
                    Student tempSt;
                    tempSt.ID = uniqueID;
                    inputFile >> tempSt.Name >> courseNm >> credit >> score;
                    tempCourse = {courseNm, credit, score};

                    tempSt.Courses.push_back(tempCourse);
                    records.push_back(tempSt);
                }
                else
                {
                    inputFile >> name;
                    inputFile >> courseNm >> credit >> score;
                    tempCourse = {courseNm, credit, score};
                    records[stList].Courses.push_back(tempCourse);
                }
            }
            inputFile.close();
        }
        else
        {
            cout << "File cannot be opened.";
        }


        for (unsigned int i = 0; i < records.size(); i++)
        {
            cout << records[i].ID << "\n" << records[i].Name << "\n";
            cout << "==========\n";

            int numerator = 0;
            int denominator = 0;
            for (unsigned int j = 0; j < records[i].Courses.size(); j++)
            {
                int curScore = records[i].Courses[j].Score;
                cout << records[i].Courses[j].CourseName << "  ";
                cout << records[i].Courses[j].Credit << "  ";
                int curGrade = curScore == 100 ? 4 : (curScore);
                cout << curGrade << "\n";

                cout << setprecision(3);
                numerator += records[i].Courses[j].Credit * mapGPA(curScore);
                denominator += records[i].Courses[j].Credit;
            }

            cout << "==========\n";
            cout << "GPA: " << (double)numerator / denominator << "\n\n";
        }
}

int locateStudent(int uniqueID, vector<Student> records)
{
    int curID;
    for (unsigned int check = 0; check < records.size(); check++)
    {
        curID = records[check].ID;
        if (uniqueID == curID)
        {
            return check;
        }
    }

    return -1;
}


我建议您编写一个函数
float calculateGPA(学生s)
。这将帮助你专注于这一项操作,并给出一段较小的代码来说明你正在努力解决的问题。你能手工计算艾米的GPA吗?你如何从她的成绩和每门课的学分数中得到
3.64
?我建议你编写一个函数
float calculateGPA(学生s)
。这将帮助你专注于这一个操作,并给出一段较小的代码来说明你正在努力解决的问题。你能手工计算艾米的GPA吗?你如何从她的成绩和每门课的学分数中得到
3.64
?注意,我已经删除了这个
int grade=curScore==100?4:(curScore)
line:似乎您认为必须降低分数,但不知道如何做,因为这只会影响最高分数,而不会影响任何其他较低的分数。请注意,我已删除此
int grade=curScore==100?4:(curScore)
line:似乎您认为必须降低分数,但不知道如何降低分数,因为这只会影响最高分数,而不会影响任何其他较低的分数。
12546
Amy
==========
CS1  4  81
CS2  4  90
CS3  3  90
==========
GPA: 3.64

13455
Bill
==========
CS1  4  76
CS2  4  85
CS3  3  75
==========
GPA: 2.36

14328
Jim
==========
CS1  4  64
CS2  4  71
CS3  3  69
==========
GPA: 1.36

14388
Henry
==========
CS3  3  80
==========
GPA: 3

15667
Peter
==========
CS3  3  45
==========
GPA: 0