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

Source: Internet
Author: User

Tag:class   c++    inherit    

/**copyright (c) 2014, College of Computer and Control engineering, Yantai University *all rights reserved.* file name: d.cpp* Author: Zhang Wanhua * Completion Date: June 3, 2015 * version number: v1.0*//** " Item 2-The list of students saved with a file "* file Score.dat is a number of students whose names and C + + classes, high scores and English grades are saved. * * #include <fstream> #include <iostream> #include <string> #include <cstdlib>using namespace    std;//defines 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.txt", 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;}


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.