17周任务一 score 二进制文件

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* 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    增加自己的成绩的,但是发现还是这样吧········



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值