10i.cpp
来自「C/C++程序设计导论(第二版)》程序源文件」· C++ 代码 · 共 33 行
CPP
33 行
// gradtest.cpp Grade student quiz answers in file `students.txt' against
// the key in disk file `key.txt'. Report each student's score and a class
// summary of the number correct for each question.
#include <iostream.h>
#include <fstream.h>
const int SIZE = 5;
void main ()
{ ifstream keyfile_in ("key.txt", ios::in);
ifstream studentfile_in ("students.txt", ios::in);
char answers[SIZE], thisanswer;
int ans, summary[SIZE] = {0, 0, 0, 0, 0}, id, n, score;
for (n=0; n<SIZE; n++)
keyfile_in >> answers[n]; // input answer key array
while (studentfile_in >> id) // loop over students
{ score = 0; // assume a zero score
for (ans=0; ans<SIZE; ans++) // loop over answers
{ studentfile_in >> thisanswer; // get next answer
if (thisanswer==answers[ans]) // is it right?
{ score++; // if so, increase grade
summary[ans]++; // and tabulate
}
} // end of loop over answers (ans)
cout << "student: "<< id <<" score: " << score << endl;
} // end of loop over students (while)
cout << " correct answers sumary for each question; ";
for (n=0; n<SIZE; n++)
cout <<summary[n] << " "; // output summary table
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?