程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生
* All rights reserved.
* 文件名稱: 學生成績的讀入和排序
* 作者: 郭廣建
* 完成日期: 2012年 6月 3日
* 版本號: 1、0
*對任務及求解方法的描述部分
/*【任務】學生成績排序
文件score.dat 中保存的是名學生的姓名和C++課、高數和英語成績。
()定義學生類,其中包含姓名、C++課、高數和英語成績及總分、均分數據成員,成員函數根據
需要確定。
()讀入這名學生的成績,用對象數組進行存儲。
()求出各科和總分的最高分。
()請按總分的降序(高成績在前,低成績在後)排序
()在屏幕上顯示各科及總分的最高分,排序後的成績單(包括總分)保存到文件odered_score.dat
中。*/
#include<iostream>
#include<fstream>
#include<iomanip >
#include<string>
using namespace std;
class Student
{
public:
Student();
double Sum();
double Everage();
string get_name(){return name;}
double get_program(){return program;}
double get_math(){return math;}
double get_english(){return english;}
//private:
string name;
double program;
double math;
double english;
};
Student::Student()
{
name = '\0';
program = 0;
math = 0;
english = 0;
}
double Student::Sum()
{
return (program +math +english);
}
double Student::Everage()
{
return((program +math +english)/3);
}
int main()
{
Student stu[100];
double score_sum[100];
ifstream readFile;
ofstream writeFile;
readFile.open("score.dat", ios::in);
if(!readFile)
{
cerr << "open error!"<<endl;;
exit(1);
}
for(int i=0; i < 100; ++i)
{
//readFile >> stu[i].get_name()>> stu[i].get_program() >> stu[i].get_math() >> stu[i].get_english();
readFile >> stu[i].name >> stu[i].program >>stu[i]. math >> stu[i].english;
score_sum[i] = stu[i].Sum();
}
readFile.close();
double max;
for(int i = 1; i < 100; ++i)
{
max = score_sum[0];
if( max < score_sum[i])
{
max = score_sum[i];
}
}
cout << "總成績的最高分是:" << max <<endl;
max = stu[0].program;
for(int i=0; i < 100; ++i)
{
if(max < stu[i].program)
{
max = stu[i].program;
}
}
cout << "C++成績的最高分是:" << max <<endl;
max = stu[0].math;
for(int i=0; i < 100; ++i)
{
if(max < stu[i].math)
{
max = stu[i].math;
}
}
cout << "高數成績的最高分是:" << max <<endl;
max = stu[0].english;
for(int i=0; i < 100; ++i)
{
if(max < stu[i].english)
{
max = stu[i].english;
}
}
cout << "英語成績的最高分是:" << max <<endl;
for(int i = 0; i < 100; ++i)
{
for(int j =0; j < 100; ++j)
{
if(score_sum[j] < score_sum[j+1])
{
double temp;
string t;
t = stu[j].name;
stu[j].name = stu[j+1].name;
stu[j+1].name = t;
temp = stu[j].program;
stu[j].program = stu[j+1].program;
stu[j+1].program = temp;
temp = stu[j].math;
stu[j].math = stu[j+1].math;
stu[j+1].math = temp;
temp = stu[j].english;
stu[j].english= stu[j+1].english;
stu[j+1].english = temp;
temp = score_sum[j];
score_sum[j] = score_sum[j+1];
score_sum[j+1] = temp;
}
}
}
cout <<setw(10)<< "姓名" << " "<<"C++成績"<< " " <<"高數成績"<< " " << "英語成績" << " "<< "總分"<<endl;
for(int i=0; i < 100; ++i)
{
cout <<setw(10)<< stu[i].name << " "<< stu[i].program << " "<< stu[i].math<< " " << stu[i].english << " "<< score_sum[i] <<endl;;
}
writeFile.open("odered_score.dat", ios::out);
if(!writeFile)
{
cerr << "open error!" <<endl;
}
for(int i=0; i < 100; ++i)
writeFile <<setw(8)<< stu[i].name << " "<<stu[i].program << " "<< stu[i].math << " "<< stu[i].english << " "<<endl;
writeFile.close();
system("pause");
return 0;
}
運行結果: