【任务1】学生成绩处理:保存为二进制文件
ASCII 文件score.dat 中保存的是100 名学生的姓名和C++课、高
数和英语成绩。
(1)定义学生类,其中包含姓名、C++课、高数和英语成绩及总
分、均分数据成员,成员函数根据需要确定。
(2)读入学生的成绩,并求出总分,用对象数组进行存储。
(3)将所有数据保存到一个二进制文件binary_score.dat 中,最
后在文件中写入你自己的各科成绩(咱不谦虚,也求个好运,全100
分)。
(4)为验证输出文件正确,再将binary_score.dat 中的记录逐一
读出到学生对象中并输出查看。
*/
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: Student.cpp
* 作 者: 计114-4刘程程
* 完成日期: 2012 年 6 月 12 日
* 版 本 号: * 对任务及求解方法的描述部分
* 程序头部的注释结束
#include <fstream>
#include <iostream>
#include "iomanip"
#include "string "
using namespace std;
class Student
{
public:
Student(){}
Student(string na,double cpp,double math,double en):name(na),cpp_score(cpp),math_score(math),en_score(en)
{total_score=cpp+math+en;aver_score=total_score/3;}
void set_name(string na);
string get_name();
void setcpp_score(double sc);
double getcpp_score();
void setmath_score(double sc);
double getmath_score();
void seten_score(double sc);
double geten_score();
void settotal_score(double sc);
double gettotal_score();
void setaver_score(double sc);
double getaver_score();
void show_score();//输出成绩
private:
string name;
double cpp_score;
double math_score;
double en_score;
double total_score;
double aver_score;
};
void Student::set_name(string na)
{
name=na;
}
string Student::get_name()
{
return name;
}
void Student::setcpp_score(double sc)
{
cpp_score = sc;
}
double Student::getcpp_score()
{
return cpp_score;
}
void Student::setmath_score(double sc)
{
math_score = sc;
}
double Student::getmath_score()
{
return math_score;
}
void Student::seten_score(double sc)
{
en_score = sc;
}
double Student::geten_score()
{
return en_score;
}
void Student::settotal_score(double sc)
{
total_score = sc;
}
double Student::gettotal_score()
{
return total_score;
}
void Student::setaver_score(double sc)
{
aver_score = sc;
}
double Student::getaver_score()
{
return aver_score;
}
//定义void show_score函数
void Student::show_score()
{
cout<<setw(6)<<setiosflags(ios::left)<<name<<""<<cpp_score<<'\t'<<math_score<<'\t'<<en_score<<'\t'<<total_score<<'\t'<<aver_score<<'\t'<<endl;
}
void cin_score(int num,Student st[]);//从文件得到成绩
void write_score(int num,Student st[]);//储存成绩
void write_Myscore();//写入我的成绩
void read_score(int num,Student st[]);//读取存储的成绩
int main()
{
Student st[101];
//从文件得到学生成绩并作相应的处理
cin_score(100,st);
//保存成绩
write_score(100,st);
//写入我的成绩
write_Myscore();
//读取处理后的成绩
read_score(101,st);
//显示处理后的成绩
for(int i=0;i<101;i++)
{
st[i].show_score();
}
system("pause");
return 0;
}
//定义从文件输入函数
void cin_score(int num,Student st[])
{
string name;
double cpp_score;
double math_score;
double en_score;
ifstream infile("score.dat",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<num;i++)
{
infile>>name>>cpp_score>>math_score>>en_score;//读入
st[i].set_name(name);
st[i].setcpp_score(cpp_score);
st[i].setmath_score(math_score);
st[i].seten_score(en_score);
st[i].settotal_score(cpp_score+math_score+en_score);
st[i].setaver_score((cpp_score+math_score+en_score)/3);
}
infile.close();
}
//定义写出数据函数
void write_score(int num,Student st[])
{
fstream outfile("binary_score.dat",ios::out|ios::binary);
if(!outfile)
{
cerr<<"open error"<<endl;
exit(1);
}
else
{
for(int i=0;i<num;i++)
{
outfile.write((char*)&st[i],sizeof(st[i]));
}
outfile.close();
cout<<"成绩已保存!"<<endl;
}
}
//定义读取函数
void read_score(int num,Student st[])
{
fstream infile("binary_score.dat",ios::in|ios::binary);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
else
{
for(int i=0;i<num;i++)
{
infile.read((char*)&st[i],sizeof(st[i]));
}
infile.close();
cout<<"成绩已读取!"<<endl;
}
}
void write_Myscore()
{
fstream writems("binary_score.dat",ios::out|ios::app|ios::binary);//定义写入文件的方式
if (!writems)
{
cerr<<"open error!"<<endl;
exit(1);
}
else
{
Student st("刘程程",100,100,100);
writems.write((char*)&st,sizeof(st));
cout<<"个人成绩已写入!"<<endl;
}
}