exam4.cpp

来自「C++语言程序设计题典」· C++ 代码 · 共 64 行

CPP
64
字号
#include <fstream.h>
#include <iostream.h>
#include <iomanip.h>
class Stud
{
	int no;
	char name[10];
	int score;
public:
	void getdata()
	{
		cout << "(学号 姓名 成绩):";
		cin >> no >> name >> score;
	}
	void disp()
	{
		cout << setw(6) << no << setw(10) << name << setw(6) << score << endl;
	}
};
void func1()
{
	ofstream output("stud.dat");
	Stud s;
	int n;
	cout << "输入数据" << endl;
	cout << "  学生人数:";
	cin >> n;
	for (int i=0;i<n;i++)
	{
		cout << "  第" << i+1 << "个学生";
		s.getdata();
		output.write((char *)&s,sizeof(s));
	};
	output.close();
}
void func2()
{
	ifstream input("stud.dat");
	Stud s;
	cout << "输出数据" << endl;
	cout << "    学号   姓名    成绩" << endl;
    input.read((char *)&s,sizeof(s));
	while (input)
	{
		s.disp();
		input.read((char *)&s,sizeof(s));
	};
	input.close();
}
void main()
{
	int sel;
	do
	{  
		cout << "选择(1:输入数据 2:输出数据 其他退出):";
	    cin >> sel;
		switch(sel)
		{
		case 1:func1();break;
		case 2:func2();break;
		}
	} while (sel==1 || sel==2);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?