南邮c++实验四 文件中选课和选课人数问题 代码

南邮c++实验 文件中选课和选课人数问题 代码

#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Course
{
	string name;
	int number;
public:
	Course(string na = "", int num = 0);
	friend ostream& operator<<(ostream& out, const Course& p);
	friend istream& operator>>(istream& in, Course& p);
};
ostream& operator<<(ostream& out, const Course& p)
{
	out << p.name << setw(10) << p.number << endl;
	return out;
}
istream& operator >>(istream& in, Course& p)
{
	in >> p.name >> p.number;
	return in;
}
Course::Course(string na, int num)
{
	name = na;
	number = num;
}
int main()
{
	ofstream out("d:\\course.txt");
	if (!out)
	{
		cout << "course.txt cannot be openned";
		return 0;
	}
	Course course[3] = { Course("高级语言程序设计",3018),
						Course("面向对象程序设计及c++",487),
						Course("程序设计(实践)",2046)
	};
	for (int j = 0; j <= 2; j++)
	{
		out << course[j];
	}
	out.close();
	ifstream in("d:\\course.txt");
	if (!in)
	{
		cout << "course.txt cannot be openned" << endl;
		return 0;
	}
	int i = 0;
	while (i < 3)
	{
		in >> course[i];
		cout << course[i];
		i++;
	}
	in.close();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值