我目前正在编写一个程序,其中包括输出学生的ID,姓名,类(class),学分和分数。数据全部在此文本文件中:
“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
我现在遇到的问题是我的输出。我正在尝试使其与期望的输出匹配,但似乎无法弄清楚。
它可能与第二个for循环中缺少其他if()语句有关。如果有人可以为我提供一些建议/提示,以使我的输入发挥作用并显示预期的输出,我将不胜感激!
我当前的代码:
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
struct Student
{
int ID = -1;
string Name = "";
string Course = "";
int Credit = -1;
int Score = -1;
};
const int SIZE = 99;
int main()
{
ifstream inputFile;
string fileName = "StudentRecords.txt";
Student studArr[SIZE];
inputFile.open(fileName.c_str(), ios::in);
int n = 0;
if (inputFile.is_open())
{
while(!inputFile.eof())
{
Student st;
inputFile >> st.ID;
inputFile >> st.Name;
inputFile >> st.Course;
inputFile >> st.Credit;
inputFile >> st.Score;
studArr[n] = st;
n++;
}
inputFile.close();
}
else
{
cout << "File cannot be opened.";
return 1;
}
// sorts the array by ID and Course
for (int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
if(studArr[i].ID > studArr[j].ID)
{
Student temp = studArr[i];
studArr[i] = studArr[j];
studArr[j] = temp;
}
else if(studArr[i].Course > studArr[j].Course)
{
Student temp = studArr[i];
studArr[i] = studArr[j];
studArr[j] = temp;
}
}
}
int check = 0;
float dividend = 0;
float divisor = 0;
for(int i = 0; i < n; i++)
{
if(studArr[i].ID != studArr[check].ID)
{
cout << "======================\nGPA "
<< round((dividend / divisor)) << endl << endl;
}
else if(i == 0)
{
cout << studArr[i].ID << " " << studArr[i].Name
<< endl << endl;
dividend = 0;
divisor = 0;
}
float gradepoints;
if(studArr[i].Score < 60)
{
gradepoints = 0.0;
}
else if(studArr[i].Score < 70)
{
gradepoints = 1.0;
}
else if(studArr[i].Score < 80)
{
gradepoints = 2.0;
}
else if(studArr[i].Score < 90)
{
gradepoints = 3.0;
}
else if(studArr[i].Score < 100)
{
gradepoints = 4.0;
}
dividend += gradepoints * studArr[i].Credit;
divisor += studArr[i].Credit;
cout << studArr[i].Course << " "
<< studArr[i].Score << " "
<< gradepoints << ".0" << endl << endl;
}
cout << "======================\nGPA "
<< round((dividend / divisor)) << endl << endl;
cout << endl << endl;
return 0;
}
我当前的输出:
12546 Amy
CS1 81 3.0
CS2 90 4.0
CS3 90 4.0
======================
GPA 4
CS1 76 2.0
======================
GPA 3
CS2 85 3.0
======================
GPA 3
CS3 75 2.0
======================
GPA 3
CS1 64 1.0
======================
GPA 3
CS2 71 2.0
======================
GPA 3
CS3 69 1.0
======================
GPA 2
CS3 80 3.0
======================
GPA 3
CS3 45 0.0
======================
GPA 2
预期产量:
12546 Amy
CS1 4 81 3.0
CS2 4 90 4.0
CS3 3 90 4.0
======================
GPA 3.64
======================
13455 Bill
CS1 4 76 2.0
CS2 4 85 3.0
CS3 3 75 2.0
======================
GPA 2.36
======================
14328 Jim
CS1 4 64 1.0
CS2 4 71 2.0
CS3 3 69 1.0
======================
GPA 1.36
======================
14388 Henry
CS3 3 80 3.0
======================
GPA 3
======================
15667 Peter
CS3 3 45 0.0
======================
GPA 0
最佳答案
在很多方面,您自己会遇到一些麻烦,但是最大的问题是您尝试输入信息。当您输入以下内容时,您的输入将保证失败:
while(!inputFile.eof())
{
Student st;
inputFile >> st.ID;
inputFile >> st.Name;
inputFile >> st.Course;
inputFile >> st.Credit;
inputFile >> st.Score;
studArr[n] = st;
n++;
}
请参阅:Why !.eof() inside a loop condition is always wrong.。本质上,在读取了最后一个有效的
inputFile >> st.Score;
之后,没有设置eofbit
,因此再次循环到inputFile >> st.ID;
失败而st
中的所有值都不确定的情况下,您不检查每个输入的结果,因此分配studArr[n] = st;
破坏了studArr
。确保不陷入陷阱的一种方法是继续编写
>>
重载以处理结构的输入。在这种情况下,很容易做到:struct Student {
int ID = -1;
std::string Name = "";
std::string Course = "";
int Credit = -1;
int Score = -1;
/* overloading the >> and << to read and write your struct helps */
friend std::istream& operator >> (std::istream& is, Student& s) {
is >> s.ID >> s.Name >> s.Course >> s.Credit >> s.Score;
return is;
}
...
};
现在,读取和验证所有输入变得非常简单:
#define MAXS 99 /* if you need a constant, #define one (or more) */
...
int main (int argc, char **argv) {
Student studArr[MAXS], tmp;
size_t n = 0;
std::ifstream f(argc > 1 ? argv[1] : "dat/StudentRecords.txt");
while (n < MAXS && f >> tmp)
studArr[n++] = tmp;
...
如果按照建议,如果您将STL容器(例如
std::vector
)用于学生存储而不是基本数组类型,则验证将进一步简化为while (f >> tmp)
(您还将使用其他方法(例如.push_back(tmp)
)代替直接分配)在整理好输入内容之后,尽管可以对数组进行排序,但实际上没有必要。一种简单的方法是遍历您的学生数据,并在另一个数组(或 vector 等)中收集唯一的
ID
值。然后,您只需将唯一的ID作为外部循环进行遍历,并遍历该学生数组的元素,如下所示:内循环并处理ID与当前外循环值匹配的每个学生。重写GPA映射逻辑和执行遍历学生数组的功能,可以执行以下操作:/* simple map GPA function that returns grade points given score */
int 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 >= 90)
return 4;
return 0;
}
查找每个学生的GPA的逻辑:
/* a function to handle outputting the student grades
* in your desired format.
*/
void dogrades (Student *s, size_t n)
{
int uniqueID[MAXS] = {0}; /* array to hold unique IDs */
size_t seen = 0; /* number of unique IDs seen */
for (size_t i = 0; i < n; i++) { /* loop collecting unique IDs */
for (size_t j = 0; j < seen; j++)
if (uniqueID[j] == s[i].ID)
goto next;
uniqueID[seen++] = s[i].ID;
next:;
}
for (size_t j = 0; j < seen; j++) { /* loop ever unique IDs */
int heading = 0, /* simple flag indicating name printed */
sumcredits = 0, /* variable to hold sum of credits */
sumpoints = 0; /* variable to hold sum of gradepoints */
for (size_t i = 0; i < n; i++) { /* loop over each stuct element */
int gradepts = 0; /* var to map score->gradepoits */
if (s[i].ID == uniqueID[j]) { /* if struct element == ID */
if (!heading) { /* if no heading output, do it */
std::cout << s[i].ID << " " << s[i].Name << "\n\n";
heading = 1; /* set flag indicating done */
}
gradepts = mapgpa(s[i].Score); /* get gradepts from score */
/* output current course and gradepoints */
std::cout << s[i].Course << " "
<< s[i].Credit << " "
<< s[i].Score << " "
<< gradepts << ".0\n\n";
sumcredits += s[i].Credit; /* sum credits */
sumpoints += s[i].Credit * gradepts; /* sum gradepoints */
}
}
std::cout.precision(3); /* set precsion and output GPA */
std::cout << "======================\n\n"
<< "GPA " << (double)sumpoints/sumcredits << "\n\n"
<< "======================\n\n";
}
}
通过在
dogrades
函数中处理逻辑,您的main()
减少为:int main (int argc, char **argv) {
Student studArr[MAXS], tmp;
size_t n = 0;
std::ifstream f(argc > 1 ? argv[1] : "dat/StudentRecords.txt");
while (n < MAXS && f >> tmp)
studArr[n++] = tmp;
dogrades (studArr, n);
}
综上所述,您可以编译一个将数据文件名作为第一个参数读取(或默认情况下从
"dat/StudentRecords.txt"
读取)的示例,您可以执行以下操作:#include <iostream>
#include <iomanip>
#include <fstream>
#define MAXS 99 /* if you need a constant, #define one (or more) */
struct Student {
int ID = -1;
std::string Name = "";
std::string Course = "";
int Credit = -1;
int Score = -1;
/* overloading the >> and << to read and write your struct helps */
friend std::istream& operator >> (std::istream& is, Student& s) {
is >> s.ID >> s.Name >> s.Course >> s.Credit >> s.Score;
return is;
}
friend std::ostream& operator << (std::ostream& os, const Student& s) {
os << std::setw(5) << s.ID << " "
<< std::setw(8) << std::left << s.Name << " "
<< std::setw(5) << s.Course << " "
<< s.Credit << " "
<< std::setw(3) << s.Score << '\n';
return os;
}
};
/* simple map GPA function that returns grade points given score */
int 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 >= 90)
return 4;
return 0;
}
/* a function to handle outputting the student grades
* in your desired format.
*/
void dogrades (Student *s, size_t n)
{
int uniqueID[MAXS] = {0}; /* array to hold unique IDs */
size_t seen = 0; /* number of unique IDs seen */
for (size_t i = 0; i < n; i++) { /* loop collecting unique IDs */
for (size_t j = 0; j < seen; j++)
if (uniqueID[j] == s[i].ID)
goto next;
uniqueID[seen++] = s[i].ID;
next:;
}
for (size_t j = 0; j < seen; j++) { /* loop ever unique IDs */
int heading = 0, /* simple flag indicating name printed */
sumcredits = 0, /* variable to hold sum of credits */
sumpoints = 0; /* variable to hold sum of gradepoints */
for (size_t i = 0; i < n; i++) { /* loop over each stuct element */
int gradepts = 0; /* var to map score->gradepoits */
if (s[i].ID == uniqueID[j]) { /* if struct element == ID */
if (!heading) { /* if no heading output, do it */
std::cout << s[i].ID << " " << s[i].Name << "\n\n";
heading = 1; /* set flag indicating done */
}
gradepts = mapgpa(s[i].Score); /* get gradepts from score */
/* output current course and gradepoints */
std::cout << s[i].Course << " "
<< s[i].Credit << " "
<< s[i].Score << " "
<< gradepts << ".0\n\n";
sumcredits += s[i].Credit; /* sum credits */
sumpoints += s[i].Credit * gradepts; /* sum gradepoints */
}
}
std::cout.precision(3); /* set precsion and output GPA */
std::cout << "======================\n\n"
<< "GPA " << (double)sumpoints/sumcredits << "\n\n"
<< "======================\n\n";
}
}
int main (int argc, char **argv) {
Student studArr[MAXS], tmp;
size_t n = 0;
std::ifstream f(argc > 1 ? argv[1] : "dat/StudentRecords.txt");
while (n < MAXS && f >> tmp)
studArr[n++] = tmp;
dogrades (studArr, n);
}
(注意:还添加了
<<
运算符的重定向,以简化输出原始结构信息(如果您需要的话))示例使用/输出
$ ./bin/studentrecords
12546 Amy
CS1 4 81 3.0
CS2 4 90 4.0
CS3 3 90 4.0
======================
GPA 3.64
======================
13455 Bill
CS1 4 76 2.0
CS2 4 85 3.0
CS3 3 75 2.0
======================
GPA 2.36
======================
14328 Jim
CS1 4 64 1.0
CS2 4 71 2.0
CS3 3 69 1.0
======================
GPA 1.36
======================
14388 Henry
CS3 3 80 3.0
======================
GPA 3
======================
15667 Peter
CS3 3 45 0.0
======================
GPA 0
======================
查看情况,让我知道是否有疑问(并认真考虑使用
std::vector
和std::map
等可用的STL容器)