我的主题可能令人困惑,这是我在C ++中的代码,如何在程序结束时显示每个年级的学生人数的摘要,可以有任何人帮吗?
#include <iostream>
#include <fstream>
#include <time.h>
#include <iomanip>
#include <algorithm>
#include <vector>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
这是我想要的输出,任何人都可以帮忙吗?
这是我的实际输出 [节目输出] [1]
编辑:感谢你们的回复,因为我现在正在学习所需的编程课程,虽然我之前已经完成了100%的材料,但我还是用C ++完成了所有这些,所以我的语法还不是很好,我们仅通过讲座中讨论过的函数/循环/方法来讲述,看起来我还需要付出更多努力。
答案 0 :(得分:0)
我不确定我是否正确理解了这个问题,但是每次你确定if-else-cascade中的等级然后std :: cout在while循环之后的结果时你可以增加指定的计数器。
答案 1 :(得分:0)
您可以尝试Wum建议的内容,因此代码可能如下所示:
#include <iostream>
#include <fstream>
#include <time.h>
#include <iomanip>
#include <algorithm>
#include <vector>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
char date[9];
ifstream in;
ofstream out;
string name;
char grade;
double asg1, asg2, test, quiz, exam, coursework, overallScore, numbergrade;
int numStudentsA = 0;
int numStudentsB = 0;
int numStudentsC = 0;
int numStudentsD = 0;
int numStudentsF = 0;
in.open("Student.txt");
out.open("Result.txt");
in >> name >> asg1 >> asg2 >> test >> quiz >> exam;
//to keep reading the data in input file
while (!in.eof()) {
coursework = asg1 + asg2 + test + quiz;
overallScore = coursework + exam;
if (overallScore >= 70 ) {
grade = 'A';
numStudentsA++;
}
else if (overallScore >= 60) {
grade = 'B';
numStudentsB++;
}
else if (overallScore >= 50) {
grade = 'C';
numStudentsC++;
}
else if (overallScore >= 40) {
grade = 'D';
numStudentsD++;
}
else if (overallScore >= 0) {
grade = 'F';
numStudentsF++;
}// grade
out << left << setw(15) << name ;
out << left << setw(3) <<coursework ; //coursework
out << left << setw(3) << exam ; //exam
out << left << setw(4) << overallScore ; //overall score
out << grade ;
out << endl;
in >> name >> asg1 >> asg2 >> test >> quiz >> exam;
}
cout << "Result Summary Date: " << date << endl;
cout << "Subeject: Programming Methodology" << endl;
cout << "Grade" << setw(10) << "Student" << endl;
cout << "A" << setw(10) << numStudentsA << endl;
cout << "B" << setw(10) << numStudentsB << endl;
cout << "C" << setw(10) << numStudentsC << endl;
cout << "D" << setw(10) << numStudentsD << endl;
cout << "F" << setw(10) << numStudentsF << endl;
cout << "Total Student = 10" << endl;
return 0;
}
我对你的代码应用了一些缩进,尝试使用单一的编码风格。另外,您应该了解为什么不建议使用eof()
。
答案 2 :(得分:0)
使用数组来计算每个年级的学生人数,并在最后打印出数组。[寻找内联评论]
使用枚举索引数组以便于阅读。
您可以使用地图而不是数组是其中一个建议。
在上述情况下,eof是正常的,第一次读取在while循环之外,然后检查在开始时完成。此后的读取也在while结束时,因此程序将在读取后立即循环回来以检查eof条件并退出(不处理空缓冲区)。
以下代码检查while (in>>name>>asg1>>asg2>>test>>quiz>>exam)
#include <iostream>
#include <fstream>
#include <time.h>
#include <iomanip>
#include <algorithm>
#include <vector>
enum { GRADE_A = 0 , GRADE_B = 1, GRADE_C = 2 , GRADE_D = 3 ,GRADE_F = 4}; // use an enum to index into the array for each grade
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
char date[9];
_strdate(date);
ifstream in;
ofstream out;
int grades[5]; // to store the grades count
string name;
char grade;
double asg1,asg2,test,quiz,exam,coursework,overallScore,numbergrade;
in.open("Student.txt");
out.open("Result.txt");
grades[GRADE_A] = 0 ; grades[GRADE_B] = 0 ; grades[GRADE_C] = 0 ; grades[GRADE_D] = 0 ; grades[GRADE_F] = 0 ; // initialize array
while (in>>name>>asg1>>asg2>>test>>quiz>>exam) //to keep reading the data in input file
{
coursework = asg1 + asg2 + test + quiz;
overallScore = coursework + exam;
if (overallScore >= 70 )
{grade = 'A' ;grades[GRADE_A]++;} // increment count for each grade
else if (overallScore >= 60)
{grade = 'B' ;grades[GRADE_B]++;}
else if (overallScore >= 50)
{grade = 'C' ;grades[GRADE_C]++;}
else if (overallScore >= 40)
{grade = 'D' ;grades[GRADE_D]++;}
else if (overallScore >= 0)
{grade = 'F' ;grades[GRADE_F]++;}; // grade
out<< left << setw(15) << name ;
out<< left << setw(3) <<coursework ; //coursework
out<< left << setw(3) << exam ; //exam
out<< left << setw(4) << overallScore ; //overall score
out<< grade ;
out<< endl;
}
cout<<"Result Summary Date: " << date << endl;
cout<<"Subeject: Programming Methodology"<<endl;
cout<< "Grade"<< setw(10) << "Student" <<endl;
cout<< "A" <<setw(10)<<grades[GRADE_A]<<endl; // output grade count
cout<< "B" <<setw(10)<<grades[GRADE_B]<<endl;
cout<< "C" <<setw(10)<<grades[GRADE_C]<<endl;
cout<< "D" <<setw(10)<<grades[GRADE_D]<<endl;
cout<< "F" <<setw(10)<<grades[GRADE_F]<<endl;
cout<<setw(0)<<endl;
cout<<"Total Student = 10"<<endl;
//At the end of the program, display the summary of number of student count for each Grade