C++程序设计实验报告(八十四) --- 第十七周任务一

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:二进制文件读入读出

* 作 者: 刘镇
* 完成日期: 2012 年 06 月 10 日
* 版 本 号: 1.080

* 对任务及求解方法的描述部分
* 输入描述: ......

* 问题描述: ......

* 程序输出: ......

* 程序头部的注释结束
*/ 

#include<iostream>  
#include<iomanip>
#include<string>  
#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 << setprecision(1) << setiosflags(ios::fixed);
	cout << "name: " << setw(3) << this->name << '\t' << "c++: " << setw(3) <<this->score_cpp<< '\t' << "English: " << setw(3) << this->score_English << '\t' << "Math:" << setw(3) << this->score_math << '\t' << "All score: " << this->score_all;
	cout << '\t'<< "Average score : " << setw(3) << 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 << setprecision(1) << setiosflags(ios::fixed);
	out << this->name << '\t' << this->score_cpp << '\t' << this->score_math << '\t' << this->score_English << '\t' << this->score_all << '\t' << this->score_average << 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[101], my_score("刘镇", 100, 100, 100);  

	readfile(stu, 100);  

	Writefile(stu, 100);  

	Readfile(stu, 100);  

	stu[100] = my_score;  

	for(int i = 0; i < 101; ++i)  
	{  
		stu[i].display();  
	}  

	writefile(stu, 101);  

	system("pause");  
	return 0;  
}  




运行结果:

 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值