c++ - 如何从文本文件中输出数据?(学生成绩单计划)
问题描述
我目前正在编写一个程序,该程序包括输出学生的 ID、姓名、课程、学分和分数。数据都在这个文本文件中:
“学生记录.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 循环中缺少一些 else 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++;
}
请参阅:为什么循环条件内的 !.eof() 总是错误的。. 本质上,在您读取最后一个 VALID 后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)
代替直接分配)
在整理好您的输入之后,虽然您可以对数组进行排序,但实际上没有必要这样做。一个简单的方法是遍历学生数据并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
======================
如果您有任何问题,请查看并告诉我(并认真考虑使用可用的 STL 容器,例如std::vector
和std::map
)
推荐阅读
- python - 无法建立新连接:[Errno 111] Connection refused'))
- node.js - Ramda 中 Lodash _.transform 的等价物是什么?
- c++ - 将具有相反操作数的两个函数重构为一个
- javascript - 我可以使用 javascript 或 jquery 以及从 MVC 中的 razor 生成的动态单选按钮吗
- angular - 如何在 Angular 8 中降低 Material datepicker 日历的宽度和高度?
- c# - Boostrap 4 模式中的输入元素不是 Asp.Net 形式的回发
- ffmpeg - 为什么 ffmpeg 需要 DNS 解析器作为其依赖项?
- sql - Oracle SQL 会话的生命周期是什么?
- html - 我如何将其居中
- excel - Passing a variable produces error code "ByRef argument type mismatch" for "..."