您的位置:首页 > 移动开发 > IOS开发

第十六周实验报告2

2012-06-06 00:05 288 查看
/*(程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作者:    朱亚楠
* 完成日期: 2012年 06月 06日
* 版本号:
*对任务及求解方法的描述部分:
文件score.dat 中保存的是100 名学生的姓名和C++课、高数和英语成绩。
(1)定义学生类,其中包含姓名、C++课、高数和英语成绩及总分、均分数据成员,成员函数根据
需要确定。
(2)读入这名学生的成绩,用对象数组进行存储。
(3)求出各科和总分的最高分。
(4)请按总分的降序(高成绩在前,低成绩在后)排序
(5)在屏幕上显示各科及总分的最高分,排序后的成绩单(包括总分)保存到文件odered_score.dat
中。
*/

#include<iostream>
#include <string>
using namespace std;
#include<fstream>
class Student
{
public:
double get_total();
double get_average();
void get_name(string name);
void get_c_score(double c_score);
void get_m_score(double m_score);
void get_e_score(double e_score);
friend void input(Student *stu) ;
friend void output(Student *stu);
friend void output_max(Student *stu);
friend void paixu(Student *stu);
private:
string name;
double c_score;
double m_score;
double e_score;
double total;
double average;
};

double Student::get_total()
{
(this->total) = (this->c_score + this->e_score + this->m_score);
return (this->total);
}

double Student::get_average()
{
(this->average) = (this->c_score + this->e_score + this->m_score)/3;
return (this->average);
}
void Student::get_name(string name)
{
this->name = name;
}
void Student::get_c_score(double c_score)
{
this->c_score = c_score;
}
void Student::get_m_score(double m_score)
{
this->m_score = m_score;
}
void Student::get_e_score(double e_score)
{
this->e_score = e_score;
}
void input(Student stu[])
{
string name;
int i;
double c_score;
double m_score;
double e_score;
ifstream inFile("score.dat",ios::in);
if(!inFile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for( i=0;i<100;++i)
{
inFile>>name;
stu[i].get_name( name);

inFile>>c_score;
stu[i].get_c_score( c_score);
inFile>>e_score;
stu[i].get_e_score( e_score);

inFile>>m_score;
stu[i].get_m_score( m_score);
}
inFile.close();
}

void output(Student *stu)
{
ofstream writeFile("odered_score.dat",ios::out);
if(!writeFile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<100;++i)
{
writeFile<<stu[i].name;
cout<<stu[i].name<<" ";
writeFile<<stu[i].c_score;
cout<<stu[i].c_score<<" ";
writeFile<<stu[i].e_score;
cout<<stu[i].e_score<<" ";
writeFile<<stu[i].m_score;
cout<<stu[i].m_score<<" ";
writeFile<<stu[i].average;
cout<<stu[i].average<<" ";
writeFile<<stu[i].total;
cout<<stu[i].total<<" ";
cout<<endl;
}
writeFile.close();

}
void output_max(Student *stu)
{
double max1,max2,max3,max4;
max1=stu[0].c_score;
max2=stu[0].m_score;
max3=stu[0].e_score;
max4=stu[0].get_total();
for(int i=0;i<100;++i)
{
if(stu[i].c_score>max1)
{
max1=stu[i].c_score;
}

if(stu[i].m_score>max2)
{
max2=stu[i].m_score;
}

if(stu[i].e_score>max3)
{
max3=stu[i].e_score;
}

if(stu[i].get_total()>max4)
{
max4=stu[i].get_total();
}

}
cout<<max1<<endl;
cout<<max2<<endl;
cout<<max3<<endl;
cout<<max4<<endl;

}

void paixu(Student *stu)
{

Student t;
for( int i=0;i<100;++i)
{
stu[i].get_total();
stu[i].get_average();
}
for(int i=0;i<99;++i)
for(int j=0;j<99-i;++j)
{
if(stu[i].total<stu[i+1].total)
{
t=stu[i];
stu[i]=stu[i+1];
stu[i+1]=t;
}
}
}
int main()
{
Student stu[100];
input(stu);
output_max(stu);
paixu(stu);
output(stu);
cout<<endl;
system("PAUSE");
return 0;

}







上机感想:

这些这是看起来难,做起来更难啊。。。

不过这次的内容很符合实际,读取和保存,再接再励。细节决定成败啊。


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  output string input c++ c ios