C++ //연습 13.5 다음 함수를 구현하는 프로그램: (1) 사원 5명의 데이터(번호, 이름, 나이, 급여 포함)를 사원 번호가 작은 순서대로 디스크 파일에 출력합니다. (2) 두 명의 사원의 데이터를 키보드로 입력합니다. (사원번호가 기존 사원번호보다 큽니다.)

C++ 프로그래밍(제3판) Tan Haoqiang 연습 13.5

연습 13.5 다음 기능을 구현하는 프로그램을 작성하세요:

(1) 사원 5명의 데이터(번호, 이름, 나이, 급여 포함)를 사원번호 오름차순으로 디스크 파일에 출력하고 저장한다.

(2) 두 명의 사원의 데이터를 키보드로 입력(사원번호가 기존 사원번호보다 큰 경우)하여 파일 끝에 추가합니다.

(3) 파일에 있는 모든 직원의 데이터를 출력합니다.

(4) 키보드로 숫자를 입력하고 파일을 검색하여 해당 사원번호가 있는지 확인하면 해당 사원이 누구인지, 해당 사원의 모든 데이터가 표시됩니다. 그렇지 않은 경우 "해당 사람 없음"을 출력합니다. 질의는 여러 번 반복될 수 있으며, 입력한 사원번호가 0이면 질의가 종료됩니다.

IDE 도구: VS2010
참고: 다른 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;
}
결과는 아래와 같습니다:

여기에 이미지 설명을 삽입하세요.

추천

출처blog.csdn.net/navicheung/article/details/135352155