โจทย์ ให้เขียน program หยิบข้อมูล Input จากแฟ้มข้อมูล
(StudentScore.txt) ซึ่งประกอบไปด้วยข้อมูล
รหัสนักศึกษา, ชื่อนักศึกษา, คะแนนเก็บ, คะแนน midterm ,คะแนน final
ดังนี้
5102026 Sakon 15 40 40 5102027 Damrong 10 25 26 5102028 Lanna 5 10 18 5102029 Kwee 10 10 15 2102030 Udon 5 30 29 2102031 Tee 20 35 40
ให้แสดงผล output ออกทางหน้าจอ ตามนี้
นักศึกษาที่ได้เกรด G ได้แก่
5102026 Sakon 95
2102031 Tee 95
นักศึกษาที่ได้เกรด P ได้แก่
5102027 Damrong 61
2102030 Udon 64
นักศึกษาที่ได้เกรด F ได้แก่
5102028 Lanna 33
5102029 Kwee 35
code ด้านล่างจะรันได้ก็ต่อเมื่อมี file StudentScore.txt อยู่แล้ว โดยสร้างตามโจทย์
#include <iostream> #include <fstream> #include <cstdlib> #include <string>
#define sourceFile “StudentScore.txt”
using namespace std;
int ProcessScore(int);
void PrintOutput(const string, const string, const int);
void main(){
cout << “Student Grade G ” << endl;
ProcessScore(1);
cout << endl;
cout << “Student Grade P ” << endl;
ProcessScore(2);
cout << endl;
cout << “Student Grade F ” << endl;
ProcessScore(3);
}
int ProcessScore(int type){
ifstream ifs;
ifs.open(sourceFile);
if(ifs.fail()){
cerr << “Error Cannot Open ” << sourceFile << endl;
return EXIT_FAILURE;
}
string studentcode, studentname;
int homework, midterm, finale;
int totalscore;
ifs >> studentcode >> studentname >> homework >> midterm >> finale;
while(!ifs.eof()){
totalscore = homework + midterm + finale;
if(type == 1){
if(totalscore >= 80){
PrintOutput(studentcode, studentname, totalscore);
}
}else if(type == 2){
if(totalscore >= 60 && totalscore <= 79 ){
PrintOutput(studentcode, studentname, totalscore);
}
}else if(type == 3){
if(totalscore < 60){
PrintOutput(studentcode, studentname, totalscore);
}
}
ifs >> studentcode >> studentname >> homework >> midterm >> finale;
}
ifs.close();
return 0;
}
void PrintOutput(const string studentcode, const string studentname, const int totalscore){
cout << studentcode << ” ” << studentname << ” ” << totalscore << endl;
}
