/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 姜雅明
* 完成日期: 2012 年 06 月 11 日
* 版 本 号: 1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
string name;
double cpp;
double math;
double english;
double all;
double ave;
public:
Student(){};
Student(string name, double cpp, double math, double english);
string get_name() {return name;}
double get_cpp() {return cpp;}
double get_math() {return math;}
double get_english() {return english;}
double get_all() {return all;}
double get_ave() {return ave;}
void set_date(string name, double cpp, double math, double english);
};
Student::Student(std::string name, double cpp, double math, double english)
{
this->name = name;
this->cpp = cpp;
this->math = math;
this->english = english;
all = cpp + math + english;
ave = all / 3;
}
void Student::set_date(std::string name, double cpp, double math, double english)
{
this->name = name;
this->cpp = cpp;
this->math = math;
this->english = english;
all = cpp + math + english;
ave = all / 3;
}
void main()
{
Student stu[101];
string name;
double cpp, math, english;
fstream infile("score.dat", ios::in);
if(!infile)
{
cerr << "open error!" << endl;
exit(1);
}
for(int i = 0; i < 101; ++i)
{
infile >> name >> cpp >> math >> english;
stu[i].set_date(name, cpp, math, english);
}
infile.close();
ofstream outfile("binary_score.dat", ios::out|ios::binary);
if(!outfile)
{
cerr << "open error!" << endl;
exit(1);
}
for(int i = 0; i < 101; ++i)
{
Student m("姜雅明", 100, 100, 100);
stu[100] = m;
outfile.write((char*)&stu[i], sizeof(stu[i]));
}
outfile.close();
ifstream ifile("binary_score.dat", ios::binary);
if(!ifile)
{
cerr << "open error!" << endl;
exit(1);
}
for(int i = 0; i < 101; ++i)
{
ifile.read((char*)&stu[i], sizeof(stu[i]));
}
ifile.close();
for(int i = 0; i < 101; ++i)
{
cout << "Name:" << stu[i].get_name() << '\t'
<< "Cpp score:" << stu[i].get_cpp() << '\t'
<< "Math score:" << stu[i].get_math() << '\t'
<< "English score:" << stu[i].get_english() << '\t'
<< "All score:" << stu[i].get_all() << '\t'
<< "Average score:" << stu[i].get_ave() << endl;
}
system("pause");
}
在 infile>>stu[i].get_cpp() 处卡住了,总是有个错误·········只好改用此方法。
本想用 ios::app 增加自己的成绩的,但是发现还是这样吧········