/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: score
* 作 者: 姜雅明
* 完成日期: 2012 年 06 月 05 日
* 版 本 号: 1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
class Student
{
private:
string name;
double cpp;
double math;
double english;
double all_score;
double ave_score;
public:
Student(){};
Student(string name, double cpp, double math, double english, double all_score, double ave_score);
void set_value(string name, double cpp, double math, double english);
string get_name(){return name;}
double get_cpp(){return cpp;}
double get_math(){return math;}
double get_english(){return english;}
double get_all_score(){return all_score;}
double get_ave_score(){return ave_score;}
void set_name(string name){this->name = name;}
void set_cpp(double cpp){this->cpp = cpp;}
void set_math(double math){this->math = math;}
void set_english(double english){this->english = english;}
void set_all_score(double all_score){this->all_score = all_score;}
void set_ave_score(double ave_score){this->ave_score = ave_score;}
};
Student::Student(string name, double cpp, double math, double english, double all_score, double ave_score)
{
this->name = name;
this->cpp = cpp;
this->math = math;
this->english = english;
this->all_score = all_score;
this->ave_score = ave_score;
}
void Student::set_value(std::string name, double cpp, double math, double english)
{
this->name = name;
this->cpp = cpp;
this->math = math;
this->english = english;
all_score = cpp + math + english;
ave_score = all_score / 3;
}
int main()
{
Student stu[100];
string name;
double cpp, math, english;
//读入文件
ifstream infile("score.dat",ios::in);
if(!infile)
{
cerr << "open error!\n";
exit(1);
}
for(int i = 0; i < 100; ++i)
{
infile >> name >> cpp >> math >> english;
stu[i].set_value(name, cpp, math, english);
}
infile.close();
//各科最高分
Student max_score("no", 0, 0, 0, 0, 0);
for(int i = 0; i < 100; ++i)
{
if(stu[i].get_cpp() > max_score.get_cpp()) {max_score.set_cpp(stu[i].get_cpp());}
if(stu[i].get_math() > max_score.get_math()) {max_score.set_math(stu[i].get_math());}
if(stu[i].get_english() > max_score.get_english()) {max_score.set_english(stu[i].get_english());}
if(stu[i].get_all_score() > max_score.get_all_score()) {max_score.set_all_score(stu[i].get_all_score());}
}
cout << "cpp最高分为:" << max_score.get_cpp() << endl
<< "数学最高分为:" << max_score.get_math() << endl
<< "英语最高分为:" << max_score.get_english() << endl
<< "总分最高分为:" << max_score.get_all_score() << endl;
//按总分排序
Student k;
for(int i = 0; i < 100; i++)
{
for(int j = 0; j < 100 - i; j++)
{
if(stu[j].get_all_score() < stu[j + 1].get_all_score())
{
k = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = k;
}
}
}
//输出到文件
ofstream outfile("ordered_score.txt",ios::out);
for(int i = 0; i < 100; i++)
{
outfile << stu[i].get_name() << "\t"
<< stu[i].get_cpp() << "\t"
<< stu[i].get_math() << "\t"
<< stu[i].get_english() << "\t"
<< stu[i].get_all_score() << "\t"
<< stu[i].get_ave_score() << "\t\n";
}
system("pause");
return 0;
}
开始向在类中把文件读入和写入文件的,但是没成功。看过老师的有所启发,变成这样了。