第17周
来源:互联网 发布:win7 专业版网络设置 编辑:程序博客网 时间:2024/06/03 00:10
#include <iostream> #include <string> #include <fstream> using namespace std; class Student { private: string name; float C; float math; float english; float addition; float average; public: Student(); Student(string na, float c,float m, float e, float add, float a); friend void readfile(Student s[]); friend void writefile(Student s[]); friend void showfile(Student s[]); }; Student::Student() { name = " "; C = 0; math = 0; english = 0; addition = 0; average = 0; } Student::Student(string na, float c,float m, float e, float add, float a) { name = na; C = c; math = m; english = e; addition = add; average = a; } void readfile(Student s[]) { ifstream file("score.dat", ios::in); if(! file) { cerr << "open error!" << endl; exit(1); } for(int i = 0; i < 100; i++) { file >> s[i].name >> s[i].C >> s[i].math >> s[i].english; s[i].addition = s[i].C + s[i].math + s[i].english; s[i].average = s[i].addition / 3; } file.close(); } void writefile(Student s[]) { ofstream file("binary_score.dat", ios::out|ios::binary); if(! file) { cerr << "open error!" << endl; exit(1); } file << "姓名" << '\t' << "C++" << '\t' << "高数"<< '\t' << "英语" << '\t' << "总成绩" << '\t' << "平均成绩" << endl; for(int i = 0; i < 100; i++) { file << s[i].name << '\t' << s[i].C << '\t' << s[i].math << '\t' << s[i].english << '\t' << s[i].addition << '\t' << s[i].average << endl; } file << "郭广建" << '\t' << "100" << '\t' << "100" << '\t' << "100" << '\t' << "300" << '\t' << "100" << endl; file.close(); } void showfile(Student s[]) { cout << "姓名" << '\t' << "C++" << '\t' << "高数" << '\t' << "英语" << '\t' << "总成绩" << '\t' << "平均成绩" << endl; for(int i = 0; i < 100; i++) { cout << s[i].name << '\t' << s[i].C << '\t' << s[i].math << '\t' << s[i].english << '\t' << s[i].addition << '\t' << s[i].average << endl; } cout << "郭广建" << '\t' << "100" << '\t' << "100" << '\t' << "100" << '\t' << "300" << '\t' << "100" << endl; } int main() { Student st[100]; readfile(st); writefile(st); showfile(st); system("PAUSE"); return 0; }