(程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作者:吴瑕
* 完成日期: 2012年 06月 05日
* 版本号:
运行结果:
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作者:吴瑕
* 完成日期: 2012年 06月 05日
* 版本号:
*对任务及求解方法的描述部分:
文件score.dat 中保存的是100 名学生的姓名和C++课、高数和英语成绩。
(1)定义学生类,其中包含姓名、C++课、高数和英语成绩及总分、均分数据成员,成员函数根据
需要确定。
(2)读入这名学生的成绩,用对象数组进行存储。
(3)求出各科和总分的最高分。
(4)请按总分的降序(高成绩在前,低成绩在后)排序
(5)在屏幕上显示各科及总分的最高分,排序后的成绩单(包括总分)保存到文件odered_score.dat
中。
#include<fstream>
#include<iostream>
#include <string>
using namespace std;
class student
{
private:
string name;
double c_goal;
double m_goal;
double e_goal;
double zong_goal;
double ping_goal;
public:
double zong();
double ping();
friend void input_student(student stu[]);
friend void output_student(student stu[]);
friend void output_max(student stu[]);
friend void paixu(student stu[]);
};
int main()
{
student stu[100];
input_student(stu);
output_max(stu);
paixu(stu);
output_student(stu);
cout<<endl;
system("PAUSE");
return 0;
}
void input_student(student stu[])
{
string name;
int i;
double c_goal=0;
double m_goal=0;
double e_goal=0;
ifstream infile("score.dat",ios::in);
if (!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(i=0;i<100;i++)
{
infile>>stu[i].name;
infile>>stu[i].c_goal;
infile>>stu[i].m_goal;
infile>>stu[i].e_goal;
}
infile.close();
cout<<endl;
}
void output_student(student stu[])
{
string name;
int i;
double c_goal=0;
double m_goal=0;
double e_goal=0;
ofstream outfile;
outfile.open("odered_score.dat",ios::out);
if (!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(i=0;i<100;i++)
{
outfile<<stu[i].name;
outfile<<stu[i].c_goal;
outfile<<stu[i].m_goal;
outfile<<stu[i].e_goal;
}
cout<<"排序结果请查看odered_score.dat文件";
/*for(i=0;i<100;i++)
{
cout<<"名字 "<<stu[i].name<<" ";
cout<<"c++ 成绩"<<stu[i].c_goal<<" ";
cout<<"高数 成绩"<<stu[i].m_goal<<" ";
cout<<"英语 成绩"<<stu[i].e_goal<<" ";
cout<<endl;
}*/
outfile.close();
cout<<endl;
}
double student::zong()
{
(this->zong_goal)=((this->c_goal)+(this->m_goal)+(this->e_goal));
return (this->zong_goal);
}
double student::ping()
{
this->ping_goal=(this->zong_goal/3);
return this->ping_goal=ping_goal;
}
void output_max(student stu[])
{
double max1=stu[0].c_goal,max2=stu[0].c_goal,max3=stu[0].c_goal,max4=stu[0].zong();
int t1=0,t2=0,t3=0,t4=0;
double y;
cout<<"c++最高分"<<endl;
for(int i=0;i<100;i++)
{
if(stu[i].c_goal>max1)
{
max1=(stu[i].c_goal);
t1=i;
}
}
cout<<max1<<endl;
cout<<"高数最高分"<<endl;
for(int i=0;i<100;i++)
{
if(stu[i].m_goal>max3)
{
max3=(stu[i].m_goal);
t3=i;
}
}
cout<<max3<<endl;
cout<<"英语最高分"<<endl;
for(int i=0;i<100;i++)
{
if(stu[i].e_goal>max2)
{
max2=(stu[i].e_goal);
t2=i;
}
}
cout<<max2<<endl;
cout<<"总分最高分"<<endl;
for(int i=0;i<100;i++)
{
y=stu[i].zong();
if(y>max4)
{
max4=y;
t4=i;
}
}
cout<<max4<<endl;
}
void paixu(student stu[])
{
int i,j;
student t;
for(i=0;i<100;i++)
{
stu[i].zong();
}
for(i=0;i<99;i++)
{
for(j=0;j<99-i;j++)
{
if(stu[j+1].zong_goal>stu[j].zong_goal)
{
t=stu[j];
stu[j]=stu[j+1];
stu[j+1]=t;
}
}
}
}
运行结果:
上机感言:
好不容易啊做这个!本来看着不难,原来做下来有好多要注意的细节,细心至上啊!!以后莫要眼高手低。