在我以前的文章中,我需要有关正在编写的程序的一些代码的帮助,该程序可以从.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(仅供参考):
Range Grade:
90 -- 100 > 4.0
80 -- 89 > 3.0
70 -- 79 > 2.0
60 -- 69 > 1.0
0 -- 59 > 0.0
建议我使用向量而不是数组。所以我重写了我的程序。
我能够以有组织的数据结构输出每个学生的ID,姓名,课程,学分和分数。但是,我的GPA不适用于每个学生。
我试图将int grade
与denominator += records[i].Courses[j].Credit;
和numerator += records[i].Courses[j].Credit;
都乘以,但是没有成功。
如果有人可以帮助我,我将不胜感激!
我当前的代码:
#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: 1
13455
Bill
==========
CS1 4 76
CS2 4 85
CS3 3 75
==========
GPA: 1
14328
Jim
==========
CS1 4 64
CS2 4 71
CS3 3 69
==========
GPA: 1
14388
Henry
==========
CS3 3 80
==========
GPA: 1
15667
Peter
==========
CS3 3 45
==========
GPA: 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
答案 0 :(得分:0)
您正在尝试计算加权平均值,其中权重为功劳。分母是正确的:它是所有权重的总和。但是,分子是错误的:应该是每个分数的总和乘以其相应的权重。
这将为您提供0到100之间的平均分数(因为所有分数都属于此范围);因此最后必须将其缩小以使其介于0到4(GPA的值范围)之间。
结果代码如下:
int numerator = 0;
int denominator = 0;
for (unsigned int j = 0; j < records[i].Courses.size(); j++)
{
int curScore = records[i].Courses[j].Score;
int curCredit = records[i].Courses[j].Credit;
cout << records[i].Courses[j].CourseName << " ";
cout << curCredit << " ";
cout << curScore << "\n";
numerator += curScore * curCredit;
denominator += curCredit;
}
cout << "==========\n";
cout << "GPA: " << (double)numerator / denominator / 25.0 << "\n\n";
答案 1 :(得分:0)
我知道了!感谢@ Code-Apprentice的帮助!我编写了float mapGPA (int Score)
函数,该函数将有助于计算我的GPA以及添加头文件#include <iomanip>
以便为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;
}
我的更新输出:
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