C++ //习题13.5 编程序实现以下功能:(1) 按职工号由小到大的顺序将5个员工的数据(包括号码、姓名、年龄、工资)输出到磁盘文件中保存。(2) 从键盘输入两个员工的数据(职工号大于已有的职工号

2024-01-03 04:13:14

C++程序设计 (第三版) 谭浩强 习题13.5

习题13.5 编程序实现以下功能:

(1) 按职工号由小到大的顺序将5个员工的数据(包括号码、姓名、年龄、工资)输出到磁盘文件中保存。

(2) 从键盘输入两个员工的数据(职工号大于已有的职工号),增加到文件的末尾。

(3) 输出文件中全部职工的数据。

(4) 从键盘输入一个号码,从文件中查找有无此职工号,如有则显示此职工是第几个职工,以及此职工的全部数据。如没有,就输出“无此人”。可以反复多次查询,如果输入查找的职工号为0,就结束查询。

IDE工具:VS2010
Note: 使用不同的IDE工具可能有部分差异。

?

代码块:
说明:文件employee.dat在程序源文件同目录下。
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <limits>
using namespace std;

const int N = 7;

typedef struct Employee{
	char name[20];
	int num;
	int age;
	double wage;
}Employee;

void initialEmp(Employee **emp, int n){
	*emp = new Employee[n];
}

void freeEmp(Employee **emp){
	delete[] (*emp);
}

void inputEmp(Employee *emp, int n){
	cout<<"Enter "<<n<<" Employee Info:"<<endl;
	for(int i = 0; i < n; i++){
		cout<<"Enter No."<<i + 1<<" Employee Number(100 ~ 999): ";
		cin>>emp[i].num;
		while(emp[i].num < 100 || emp[i].num > 999){
			cout<<"Number Error! Retry!\nEnter No."<<i + 1<<" Employee Number(100 ~ 999): ";
			cin>>emp[i].num;
		}

		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cout<<"Enter No."<<i + 1<<" Employee Name: ";
		gets(emp[i].name);

		cout<<"Enter No."<<i + 1<<" Employee Age(18 ~ 60): ";
		cin>>emp[i].age;
		while(emp[i].age < 18 || emp[i].age > 60){
			cout<<"Age Error! Retry!\nEnter No."<<i + 1<<" Employee Age(18 ~ 60): ";
			cin>>emp[i].age;
		}

		cout<<"Enter No."<<i + 1<<" Employee Wage: ";
		cin>>emp[i].wage;
		while(emp[i].wage < 0){
			cout<<"Wage cannot be less than 0! Retry!\nEnter No."<<i + 1<<" Employee Wage: ";
			cin>>emp[i].wage;
		}
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cout<<endl;
	}
}

void inputFile(char *name, Employee *emp, int n){
	Employee temp;
	for(int i = 0; i < n; i++){
		for(int j = i + 1; j < n; j++){
			if(emp[i].num > emp[j].num){
				temp = emp[i];
				emp[i] = emp[j];
				emp[j] = temp;
			}
		}
	}

	ofstream outfile(name, ios::out | ios::binary);
	if(!outfile){
		cerr<<"Open File "<<name<<" Error!"<<endl;
		system("pause");
		exit(1);
	}

	for(int i = 0; i < n; i++){
		outfile.write(reinterpret_cast<char*>(&emp[i]), sizeof(Employee));
	}

	outfile.close();
}

void outputFile(char *name, Employee *emp, int n){
	ifstream infile(name, ios::in | ios::binary);
	if(!infile){
		cerr<<"Open File "<<name<<" Error!"<<endl;
		system("pause");
		exit(1);
	}

	for(int i = 0; i < n; i++){
		infile.read(reinterpret_cast<char*>(&emp[i]), sizeof(Employee));
	}

	cout<<"Employee Info:"<<endl;
	for(int i = 0; i < n; i++){
		cout<<setiosflags(ios::left | ios::fixed)<<setprecision(2);
		cout<<"Number: "<<setw(3)<<emp[i].num<<" Name: "<<setw(10)<<emp[i].name
			<<" Age: "<<setw(2)<<emp[i].age<<" Wage: "<<emp[i].wage<<endl;
	}
	cout<<endl;

	infile.close();
}

void insertEmp(char *name, Employee *emp, int n, int insert){
	Employee *newEmp = new Employee[insert];
	cout<<"Enter "<<insert<<" Insert Employee Info:"<<endl;
	for(int i = 0; i < insert; i++){
		cout<<"Enter No."<<i + 1<<" Insert Employee Number(100 ~ 999): ";
		cin>>newEmp[i].num;
		while((newEmp[i].num <= emp[n-1].num) || (newEmp[i].num < 100 || newEmp[i].num > 999)){
			if(newEmp[i].num <= emp[n-1].num){
				cout<<"Number cannot be less than the last employee number! Retry!\nEnter No."<<i + 1<<" Insert Employee Number(100 ~ 999): ";
				cin>>newEmp[i].num;
			}
			if(newEmp[i].num < 100 || newEmp[i].num > 999){
				cout<<"Number Error! Retry!\nEnter No."<<i + 1<<" Insert Employee Number(100 ~ 999): ";
				cin>>newEmp[i].num;
			}
		}

		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cout<<"Enter No."<<i + 1<<" Insert Employee Name: ";
		gets(newEmp[i].name);

		cout<<"Enter No."<<i + 1<<" Insert Employee Age(18 ~ 60): ";
		cin>>newEmp[i].age;
		while(newEmp[i].age < 18 || newEmp[i].age > 60){
			cout<<"Age Error! Retry!\nEnter No."<<i + 1<<" Insert Employee Age(18 ~ 60): ";
			cin>>newEmp[i].age;
		}

		cout<<"Enter No."<<i + 1<<" Insert Employee Wage: ";
		cin>>newEmp[i].wage;
		while(newEmp[i].wage < 0){
			cout<<"Wage cannot be less than 0! Retry!\nEnter No."<<i + 1<<" Insert Employee Wage: ";
			cin>>newEmp[i].wage;
		}
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cout<<endl;
	}

	fstream iofile(name, ios::in | ios::out | ios::binary);
	if(!iofile){
		cerr<<"Open File "<<name<<" Error!"<<endl;
		system("pause");
		exit(1);
	}
	
	iofile.seekg(n * sizeof(emp[0]), ios::beg);
	for(int i = 0; i < insert; i++){
		iofile.write(reinterpret_cast<char*>(&newEmp[i]), sizeof(Employee));
	}

	delete[] newEmp;
	iofile.close();
}

void search(char *name, Employee *emp, int n){
	int searchNum;
	int sign = 0;
	cout<<"Enter Search Number(100 ~ 999): ";
	cin>>searchNum;
	while(searchNum != 0){
		for(int i = 0; i < n; i++){
			if(searchNum == emp[i].num){
				sign = 1;
				cout<<"The number is No."<<i + 1<<" Employee."<<endl;
				cout<<setiosflags(ios::left | ios::fixed)<<setprecision(2);
				cout<<"Number: "<<setw(3)<<emp[i].num<<" Name: "<<setw(10)<<emp[i].name
					<<" Age: "<<setw(2)<<emp[i].age<<" Wage: "<<emp[i].wage<<endl<<endl;
				break;
			}
		}
		if(sign == 0){
			cout<<"No Match!"<<endl<<endl;
		}
		sign = 0;
		cout<<"Enter Search Number(100 ~ 999): ";
		cin>>searchNum;
	}
	cout<<"The End!"<<endl<<endl;
}

int main(){
	Employee *emp = NULL;

	initialEmp(&emp, N);
	inputEmp(emp, N-2);
	inputFile("employee.dat", emp, N-2);
	outputFile("employee.dat", emp, N-2);
	insertEmp("employee.dat", emp, N-2, 2);
	outputFile("employee.dat", emp, N);
	search("employee.dat", emp, N);
	freeEmp(&emp);
	
	system("pause");
	return 0;
}
结果显示如下:

在这里插入图片描述

文章来源:https://blog.csdn.net/navicheung/article/details/135352155
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。