14th Week Item Two-Save student lists with documents

Source: Internet
Author: User

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.

int main () {    Student stud[200],t;//stud[200] is an array of objects that holds the data    string sname;    Double Total_avg;    int i=0;    Read data from file Score.dat, save to Object array    //Total score above average score and the information of the classmate without Hang Ke is saved to file Pass_score.dat    return 0;}

Discussion: Another solution to the number of students and their total score is to use global variables. But these two kinds of information is related to the student, is the student class "The attribute", becomes the student class the data member is suitable, these two kinds of information is decided by the student overall, uses as the static data member suitable. If you do not understand the reasons for this design, review the relevant content in front of the course.

#include <fstream> #include <iostream> #include <string> #include <cstdlib>using namespace std    ;//define Student Class Student{public:student () {};    ~student ();    Double Get_total ();    static int get_stu_num ();    Static double Get_total_sum ();  Friend istream& operator>> (IStream &in, Student &s); You can define the input function instead of friend ostream& operator<< (ostream &out, Student &s);    You can define the display function to replace the 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 total score and};int Student::stu_num = 0;double student::total_sum = 0;    Student::~student () {total_sum-=total; stu_num--;} Double Student::get_total () {return total;} int Student::get_stu_num () {return stu_num;} Double Student::get_total_sum () {return total_sum;} 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++;    In the process of reading data, the number of students and total score and student::total_sum+=s.total are recorded with static members. 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;} Returns whether all courses have been all over the bool Student::p () {return cpp>=60 && math>=60 && english>=60;}    int main () {Student stud[200],t;//stud[200] is an array of objects that holds the data string sname;    Double Total_avg;    int i=0;  Reads the data in the file into an array of objects Ifstream infile ("Score.dat", ios::in); Open file as input if (!infile)//test successfully opened {cerr<< "open error!"        <<endl;    Exit (1);   } while (!infile.eof ()) {infile>>stud[i++];    In the reading data, the number of people and other information is automatically recorded into the static member, see the implementation of operator overloading} infile.close (); The total score is averaged and output if (Student::get_stu_num () >0) {total_avg = student::get_tOtal_sum ()/Student::get_stu_num ();        Ofstream outfile ("Pass_score.dat", ios::out); if (!outfile) {cerr<< "Open error!"            <<endl;        Exit (1); } for (i=0; I<student::get_stu_num (); i++) {if (Stud[i].get_total () >total_avg&&stud[            I].pass ()) {outfile<<stud[i]<<endl;        }} outfile.close ();    cout<< "Please see the list in the file Pass_score.dat:" <<endl; } return 0;}


Operation Result:

Duan 63 90 96 249
He Yuzhong 90 73 82 245
Wang Lei 87 86 92 265
Feng Song 89 98 83 270
Ms. Penny PEI 75 82 91 248
Ruby 98 84 87 269
Li Tung 93 83 86 262
Zhang di 99 88 80 267
Zhang Li Ring 85 65 96 246
Wang Ruiqi 89 83 91 263
Song Zongjie 94 100 92 286
Shu 61 98 96 255
Virginia 70 91 90 251
Song Yuanhua 61 94 92 247
Wang Yan 90 87 67 244
Trizan 91 67 93 251
Li Peng 90 82 97 269
Dixon 60 90 100 250
Han Ming 83 97 88 268
Madachi 73 90 83 246
Wu Tsing 89 97 85 271
Clouds 89 88 71 248
Ge Zhiwei 100 79 71 250
Fan 98 87 89 274
Wang Fang 71 97 99 267
Yang Mengyu 89 99 67 255
Wang Qi 98 95 98 291
Yang Kuo 90 91 98 279
Liu Yanue 77 81 95 253
Dong Yiwei 93 88 80 261
Tocqueville 86 88 76 250
Quiet 93 88 85 266
Xu Jiaqi 90 75 87 252
Jiatong 100 94 80 274
Aloft 81 99 91 271
Zhaoxuyang 87 91 94 272
Zhang Hao 94 83 96 273
HD 76 83 84 243
Candy 87 80 96 263
Liu Surplus 99 72 93 264
杨洁 96 79 87 262
Wang Meng 67 97 89 253
Zhang Min 85 75 89 249
Wu Jialin 96 65 82 243
Huang Jinlong 85 90 78 253
Shendan 100 89 89 278
Liu Zi Bright 72 98 84 254

Learning experience:

Practice is the truth

14th Week Item Two-Save student lists with documents

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.