第十六周任务2

来源:互联网 发布:淘宝客 qq空间 编辑:程序博客网 时间:2024/05/18 18:03
/* (程序头部注释开始)* 程序的版权和版本声明部分* Copyright (c) 2012, 烟台大学计算机学院学生 * All rights reserved.* 文件名称:student文件读入* 作 者: 于宸* 完成日期: 2012 年 06 月 03 日* 版 本 号: 1.076* 对任务及求解方法的描述部分* 输入描述: ......* 问题描述: ......* 程序输出: ......* 程序头部的注释结束*/
#include<iostream>#include<string>#include<fstream>using namespace std;class Student{public:double get_project(char pro);double all_score();double ave_score();void read_score(ifstream &in);void write_score(ofstream &out);private:string name;double score_cpp;double score_math;double score_English;double score_all;double score_average;};double Student::get_project(char pro){switch(pro){case 'c':return this->score_cpp;break;case 'm':return this->score_math;break;case 'E':return this->score_English;break;case 'A':return this->all_score();break;default:return this->ave_score();break;}}double Student::all_score(){this->score_all = this->score_cpp + this->score_math + this->score_English;return this->score_all;}double Student::ave_score(){this->score_average = (this->score_cpp + this->score_math + this->score_English) / 3;return this->score_average;}void Student::read_score(ifstream &in){in >> this->name >> this->score_cpp >> this->score_math >> this->score_English;}void Student::write_score(ofstream &out){out << this->name << '\t' << this->score_cpp << '\t' << this->score_math << '\t' << this->score_English << endl;}void readfile(Student * s, int num){ifstream infile("score.dat",ios::in);if(!infile){cerr << "open error!" << endl;exit(1);}for(int i = 0; i < 100; ++i){s[i].read_score(infile);}infile.close();}void writefile(Student * s, int num){ofstream outfile("odered_score.dat",ios::out);if(!outfile){cerr << "open error!" << endl;exit(1);}for(int i = 0; i < 100; ++i){s[i].write_score(outfile);}outfile.close();}double highest_score(Student * s, int num, char p){double highest_score = 0;for(int i = 0; i < num; ++i){if(s[i].get_project(p) >= highest_score){highest_score = s[i].get_project(p);}else{continue;}}return highest_score;}void bubble_sort(Student * s, int num){Student t;for(int j = 0; j < num-1; j++){for(int i = 0; i < num-1-j; ++i){if(s[i].all_score() <= s[i+1].all_score()){t = s[i];s[i] = s[i+1];s[i+1] = t;}}}}int main(){Student stu[100];readfile(stu, 100);cout << "各学科、平均分及总分最高分:" << endl;cout << "c++:" << highest_score(stu, 100, 'c') << endl;cout << "Math: " << highest_score(stu, 100, 'm') << endl;cout << "English: " << highest_score(stu, 100, 'E') << endl;cout << "All score: " << highest_score(stu, 100, 'A') << endl;cout << "Average score: " << highest_score(stu, 100, 'a') << endl;bubble_sort(stu, 100);writefile(stu, 100);system("pause");return 0;}


原创粉丝点击