您的位置:首页 > 编程语言 > C语言/C++

C++程序设计实验报告(八十)---第十六周任务二

2012-06-03 22:52 537 查看
/* (程序头部注释开始)

* 程序的版权和版本声明部分

* 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;
}


 

 

 

 

运行结果:



 

 

 

心动感言:

 

觉得这次设计挺不错的,看过另外一些解法,还是觉得类应该是具有普遍性,而不应该具有专一性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 任务 math ios system string