14th Week Item 2-list of students saved with a file

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.

#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 &AMP;&A mp    ENGLISH&GT;=60) return true; else return false;} istream& operator>> (IStream &in, Student &s) {IN&GT;&GT;S.NAME&GT;&GT;S.CPP&GT;&GT;S.MATH&GT;&GT;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

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.