The names and C + + classes, high scores, and English grades of several students are saved in file Score.dat.
(1) Define student classes, which include names, C + + classes, high scores and English score and total data members.
Define class Student{public: //Declare necessary member functions private: string name; Double cpp; Double math; Double 中文版; Double total; static int stu_num; Number of students, the appropriate static double total_sum for the static members of the class ;//Student total score and};
(2) Use an array of objects to store the student's scores, read the scores and calculate the total score, and save the information of the students with the total score above the average score and the Pass_score.dat to the file.
#include <iostream> #include <cstdlib> #include <fstream>using namespace std;//define class student{for students Public:student () {}; void input (); void display (); Double Get_total () {return total; } static int Get_stu_num () {return stu_num; } static double Get_total_sum () {return total_sum; } bool Pass ();p rivate:string name; Double CPP; Double math; Double 中文版; Double total; static int stu_num; Number of pupils, handling static members of the class as appropriate static double total_sum; Student scores and friend istream& Operator>> (IStream &in, Student &s); Friend ostream& operator<< (ostream &out, Student &s);}; int student::stu_num=0;double student::total_sum=0;bool Student::p () {if (cpp>=60 && math>=60 &&A mp ENGLISH>=60) return true; else return false;} istream& operator>> (IStream &in, Student &s) {IN>>S.NAME>>S.CPP>>S.MATH>>s.english; S.total=s.cpp+s.math+s.english; student::stu_num++; Student::total_sum+=s.total; return in;} Ostream &operator<< (ostream &out, Student &s) {out<<s.name<< "\ t"; out<<s.cpp<< "\ t"; out<<s.math<< "\ t"; out<<s.english<< "\ t"; out<<s.total; return out;} int main () {Student stu[200]; int num=0,i; Double Total_avg; Ifstream infile ("Score.dat", ios::in); if (!infile) {cerr<< "error!" <<endl; Exit (1); } while (!infile.eof ()) {infile>>stu[num++]; } infile.close (); Total_avg = Student::get_total_sum ()/Student::get_stu_num (); Ofstream outfile ("Pass_score.dat", ios::out); if (!outfile) {cerr<< "error!" <<endl; Exit (1); } for (i=0; I<student::get_stu_num (); i++) {if (Stu[i].get_total () >total_avg) {if (s Tu[i].pass ()) {outfile<<stu[i]<<endl; } } }}
Operation Result:
@ Mayuko
14th Week Item 2-list of students saved with a file