?
#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;
}