/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 《学生成绩处理》
* 作 者: 刘江波
* 完成日期: 2012 年 6 月 12 日
* 版 本 号: v.61
* 对任务及求解方法的描述部分
* 问题描述:
【任务1】学生成绩处理:保存为二进制文件
ASCII 文件score.dat 中保存的是100 名学生的姓名和C++课、高数和英语成绩。
(1)定义学生类,其中包含姓名、C++课、高数和英语成绩及总
分、均分数据成员,成员函数根据需要确定。
(2)读入学生的成绩,并求出总分,用对象数组进行存储。
(3)将所有数据保存到一个二进制文件binary_score.dat 中,最
后在文件中写入你自己的各科成绩(咱不谦虚,也求个好运,全100
分)。
(4)为验证输出文件正确,再将binary_score.dat 中的记录逐一
读出到学生对象中并输出查看。
* 程序头部的注释结束
*/
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
class Student
{
public:
Student();
Student(string name, double cpp, double math, double English);
double all_score();
double ave_score();
void read_score(ifstream &in);
void write_score(ofstream &out);
void display();
private:
string name;
double score_cpp;
double score_math;
double score_English;
double score_all;
double score_average;
};
Student::Student()
{
this->name = "0000";
this->score_cpp = 0;
this->score_math = 0;
this->score_English = 0;
}
Student::Student(string name, double cpp, double math, double English)
{
this->name = name;
this->score_cpp = cpp;
this->score_math = math;
this->score_English = English;
}
void Student::display()
{
this->all_score();
this->ave_score();
cout << setiosflags(ios::left) << setw(12) << this->name << setw(8) << this->score_cpp << setw(8) << this->score_math << setw(8) << this->score_English << setw(8) << this->score_all <<setw(8)<<this->score_average<< endl;
}
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 < num; ++i)
{
s[i].read_score(infile);
}
infile.close();
}
void Readfile(Student * s, int num)
{
ifstream infile("binary_score.dat",ios::in|ios::binary);
if(!infile)
{
cerr << "open error!" << endl;
abort();
}
for(int i = 0; i < num; ++i)
{
infile.read((char *) & s[i], sizeof(s[i]));
}
infile.close();
}
void writefile(Student * s, int num)
{
ofstream outfile("binary_score.dat",ios::out|ios::binary);
if(!outfile)
{
cerr << "open error!" << endl;
abort();
}
for(int i = 0; i < num; ++i)
{
outfile.write((char *) & s[i], sizeof(s[i]));
}
outfile.close();
}
void Writefile(Student * s, int num)
{
ofstream outfile("binary_score2.dat",ios::out);
if(!outfile)
{
cerr << "open error!" << endl;
abort();
}
for(int i = 0; i < num; ++i)
{
s[i].write_score(outfile);
}
outfile.close();
}
int main()
{
Student stu[100], stu1[101], my_score("刘江波", 100, 100, 100);
readfile(stu, 100);
writefile(stu, 100);
Readfile(stu1, 100);
stu1[100] = my_score;
cout << setiosflags(ios::left) << setw(12) << "姓名" << setw(8) << "C++" << setw(8) << "高数" << setw(8) << "英语" << setw(8) <<"总分" << setw(8) << "平均分" << endl;
for(int i = 0; i < 101; ++i)
{
stu1[i].display();
}
Writefile(stu1, 101);
system("pause");
return 0;
}