14th Week "Item 2-list of students saved by file" number of student names and C + + classes, high scores and English grades

Source: Internet
Author: User

To run the code:

/**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 student name and C + + class, high number and English scores 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 instead of the bool Pass ();p rivate:string num,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.num>>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.num<< "\ t";    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;}

Test data:

201152501104 Tian Cheng 65 69 68
201152501114 Zhanbing 94 89 63
201152501115 Zhou Lin 92 84 60
201152501138 Zhang Qiang 67 62 84
201152501140 Zheng Good 80 60 86
201152501145 Cao to still 88 56 67
201152501201 Tina 80 92 71
201152501202 Leo 59 80 55
201152501203 Zhang Jia Yi 62 62 95
201152501204 Dunson Winter 100 65 91
201152501205 Meijuan 73 90 94
201152501206 Zhaogjun 96 80 58
201152501207 Zhang Tao 91 63 91
201152501208 Car Yi Lin 88 84 79
201152501209 Liu Zhengyou 61 79 99
201152501210 Lee Eastern 62 58 66
201152501211 Li Hua 96 96 83
201152501212 Li Xiaopeng 73 88 93
201152501213 Jiang Junxia 69 64 62
201152501214 Dong Yue 74 59 69
201152501215 Zhao June Yuan 63 58 96
201152501216 Gondanche 89 82 60
201152501217 Zhang Yonghui 94 89 77
201152501218 Ding Mengyao 78 93 58
201152501219 Jin Ting 75 87 84
201152501220 Juan 90 65 62
201152501221 Jiangli 76 78 81
201152501222 Sun Yawen 90 87 73
201152501223 Guo Weight 88 68 63
201152501224 Wang Zuezhe 85 94 97
201152501225 Song Senji 99 69 82
201152501226 Du Yu FEI 91 80 98
201152501227 Yuxiaoxia 69 72 99
201152501228 Lili Wang 84 95 94
2.,011,525,012,29E,+15 JAS 98 95 64
201152501230 Chishuxia 67 100 83
201152501231 gaojing 98 81 67
201152501232 Uzeline 67 64 77
201152501233 Li Jing Yi 84 72 90
201152501234 Yao Yu 92 78 95
201152501235 to Ultra 87 82 86
201152501236 Jian Li 78 86 95
201152501237 Meng Yuwei 66 64 58
201152501238 Sun Huaze 98 98 74
201152501239 Zhu Yumeng 73 83 96
201,152,501,240 weeks Easy 56 69 84
201152501241 Cai Qing 60 78 71
201152501242 Kang 86 94 59
201152501243 孙雪梅 64 92 66
201152501244 Li Ying 97 100 55
201,152,501,245 Weeks Xiang 100 86 74
201152501246 LU Hongjiang 73 94 75
201153503240 wangtingting 68 73 69
201153503242 cases of snow Swallow 60 82 61
201153503243 in small culvert 79 60 78
201153503245 Zhong Yi 82 91 97
201153503246 Qing Qingmin 67 77 60
201153503248 Sheng 79 100 57
201153503249 Shiuan Chen 59 86 90
201153503251 He Chaoxia 56 87 88
201153503254 Zhang Li Fang 57 81 87
201154503207 Liu Rui 57 89 69
201158501330 Li Tao 96 89 73
201158501332 New Ming 68 59 64
201158501334 Cao Wen War 82 89 73
201160602108 Kong Lingtao 63 86 80
201160602124 Liu Dawei 96 69 98
201163501101 Mon Station 58 100 70
201163501102 Wang Suchi 75 63 89
201,163,501,104 Week TAO 68 95 84
201163501105 Jia Govern 86 84 100
201163501106 Yang Lei 92 66 88
2.,011,635,011,07E,+15 build new 71 56 83
201163501108 Li Ning 70 95 73
201163501110 Zhao Jiangyan 58 63 79
201163501111 Du Jun 161 75 70
201163501113 Shans 56 70 66
201163501114 Li Shunliang 95 90 86

Pass Data
201152501114 Zhanbing94 89 63 246
201152501115 Zhou Lin92 84 60 236
201152501201 Tina80 92 71 243
201152501204 Dunson Winter100 65 91 256
201152501205 Meijuan73 90 94 257
201152501207 Zhang Wenji91 63 91 245
201152501208 Cheilin88 84 79 251
201152501209 Liu Zhengyou61 79 99 239
201152501211 Li hua96 96 83 275
201152501212 Li Xiaopeng73 88 93 254
201152501217 Zhang Yonghui94 89 77 260
201152501219 Kim Jung Rye75 87 84 246
201152501221 Jiangli76 78 81 235
201152501222 Sun Yawen90 87 73 250
201152501224 Wang Zuezhe85 94 97 276
201152501225 Song Senji99 69 82 250
201152501226 Du Yu Fei91 80 98 269
201152501227 Yuxiaoxia69 72 99 240
201152501228 Lili Wang84 95 94 273
201152501229 Vanyalu98 95 64 257
201152501230 Chishuxia67 100 83 250
201152501231 Gaojing98 81 67 246
201152501233 Li Jing Yi84 72 90 246
201152501234 Yao Yu92 78 95 265
201152501235 Ultra87 82 86 255
201152501236 Jian Li78 86 95 259
201152501238 Sun Huaze98 98 74 270
201152501239 Zhu Yumeng73 83 96 252
201152501245 Chou Hsiang100 86 74 260
201152501246 Hongjiang Lu73 94 75 242
201153503245 Zhong82 91 97 270
201158501330 Li Tao96 89 73 258
201158501334 Cao Wen War82 89 73 244
201160602124 Liu Dawei96 69 98 263
201163501104 Zhou Tao68 95 84 247
201163501105 Jia86 84 100 270
201163501106 Yang Lei92 66 88 246
201163501108 Li ning70 95 73 238
201163501114 Li Shunliang95 90 86 271

Learning experience:

The data was not stored at the beginning and was discovered because the data was too large to exceed the double range

float            6-7               -3.4*10 ( -38) ~3.4*10 (a) double           15-16            -1.7*10 ( -308) ~1.7*10 (308) long Double          18-19         -1.2*10 (-4932) ~1.2*10 (4932)
After modifying the data! and reduce data!

Final code:

/**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 student name and C + + class, high number and English scores 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 int 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 instead of the bool Pass ();p rivate:string num,name;    Double CPP;    Double math;    Double 中文版;    Double total;  static int stu_num; Number of students, handling static members of the class suitable static int total_sum; Student total score and};int Student::stu_num = 0;int 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;} int Student::get_total_sum () {return total_sum;} istream& operator>> (IStream &in, Student &s) {in>>s.num>>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.num<< "\ t";    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 by file" number of student names and C + + classes, high scores and English grades

Related Article

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.