📄 struct.cpp
字号:
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>//include head file
int Initial(struct employee worker[],int x);
void Output(struct employee worker[],int m);
void Statistic(struct employee worker[],int count);
void Sort(struct employee worker[],int count);
int Console(struct employee worker[100],int temp);
int Input(struct employee worker[],int temp);
void Lookup(struct employee worker[],int temp);
int Delete(struct employee worker[],int temp);
void Modify(struct employee worker[],int temp);
int get_index(struct employee worker[],int number,int temp);
void rewrite(struct employee worker[],int index);
int Insert(struct employee worker[],int temp);
void display(struct employee worker[],int temp);
int wrong(struct employee worker[],int temp,int number);
struct employee
{
char shop[20];
int num;
char name[8];
float base_salary;
float post_salary;
float bonus;
float deal_salary;
float fund;
float tax;
float fac_salary;
}worker[100];//define struct array,allocate 100 space
int n=0;//define global variable
void main()
{
int temp;//temp stores the current total number of available data
fstream infile;
fstream outfile;
infile.open("input.txt",ios::in);
outfile.open("output.txt",ios::out);//define infile and outfile
temp=Initial(worker,n);//set the initial status and get the initial total number
Statistic(worker,temp);//compute the items
temp=Console(worker,temp);//invoke the main window and return the current total number
Output(worker,temp);//output the data to the output file
}
int Initial(struct employee worker[100],int x)//read the information from the input file
{
fstream infile;
fstream outfile;
infile.open("input.txt",ios::in);
outfile.open("output.txt",ios::out);
while(!infile.eof())
{
infile>>worker[x].shop;
infile>>worker[x].num;
infile>>worker[x].name;
infile>>worker[x].base_salary;
infile>>worker[x].post_salary;
infile>>worker[x].bonus;
x++;
}//read the date information one by one
Statistic(worker,x-1);
Sort(worker,x-1);
return x-1;
}
void Output(struct employee worker[100],int m)//output the data information to the output file
{
fstream infile;
fstream outfile;
infile.open("input.txt",ios::in);
outfile.open("output.txt",ios::out);
int i;
for(i=0;i<m;i++)
{
outfile<<setw(20)<<setiosflags(ios::left)<<worker[i].shop;
outfile<<setw(10)<<worker[i].num;
outfile<<setw(10)<<worker[i].name;
outfile<<setw(10)<<worker[i].base_salary;
outfile<<setw(10)<<worker[i].post_salary;
outfile<<setw(10)<<worker[i].bonus;
outfile<<setw(10)<<worker[i].deal_salary;
outfile<<setw(10)<<worker[i].fund;
outfile<<setw(10)<<worker[i].tax;
outfile<<setw(10)<<worker[i].fac_salary<<endl;
}
}
void Statistic(struct employee worker[100],int count)//compute the other data according to the famula
{
int i;
for(i=0;i<count;i++)
{
worker[i].deal_salary=worker[i].base_salary+worker[i].bonus+worker[i].post_salary;
worker[i].fund=worker[i].base_salary*0.1;
worker[i].tax=(worker[i].deal_salary-1000)*0.05;
worker[i].fac_salary=worker[i].deal_salary-worker[i].fund-worker[i].tax;
}
}
void Sort(struct employee worker[100],int count)
{
employee temp;
int i,j,k;
for(i=0;i<count-1;i++)
{
k=i;
for(j=i+1;j<=count-1;j++)
{
if(worker[j].fac_salary<worker[k].fac_salary)
k=j;
}//put the smallest index into k
if(k!=i)//exchange the two data
{
temp=worker[i];
worker[i]=worker[k];
worker[k]=temp;
}
}
}
int Console(struct employee worker[100],int temp)//the main window console function
{
char a;
cout<<"*******************************************************************************"<<endl;
cout<<"* WELCOME TO EMPLOYER ADMINISTRATING SYSTEM *"<<endl;
cout<<"* achieved using struct*"<<endl;
cout<<"* zhangyu 9.16.2004 *"<<endl;
cout<<"*******************************************************************************"<<endl;//output welcome information
cout<<"Input(i) Lookup(l) Delete(d) Insert(n) Modify(m) Display(p) quit(q)"<<endl;
cin>>a;//give tips and input the character to select function
if(a=='q')
cout<<"You have terminate the program!"<<"Welcome back next time!"<<endl;
else
{
switch(a)
{
case 'p':
{
cout<<"You have chosen to display records!"<<endl;
Statistic(worker,temp);
Sort(worker,temp);
display(worker,temp);
temp=Console(worker,temp);
}
break;
case 'i':
{
cout<<"You have chosen to input records!"<<endl;
temp=Input(worker,temp);
temp=Console(worker,temp);//return the current count and reinvoke the Console function
}
break;
case 'l':
{
cout<<"You have chosen to lookup records!"<<endl;
Lookup(worker,temp);
temp=Console(worker,temp);
}
break;
case 'd':
{
cout<<"You have chosen to delete records!"<<endl;
temp=Delete(worker,temp);
temp=Console(worker,temp);
}
break;
case 'n':
{
cout<<"You have chosen to insert records!"<<endl;
temp=Insert(worker,temp);
temp=Console(worker,temp);
}
break;
case 'm':
{
cout<<"You have chosen to modify records!"<<endl;
Modify(worker,temp);
temp=Console(worker,temp);
}
break;
default:
{
cout<<"you have input the wrong letter,please check and try again!"<<endl;
temp=Console(worker,temp);
}
break;
}
}
return temp;//return the current count to the main function
}
int Input(struct employee worker[100],int temp)
{
int number,i;
cout<<"How many workers's records do you want to input?"<<endl;
cin>>number;//set the total count
cout<<"You can input the new data now:"<<endl;
cout<<"(Example:soft 20040001 zhang 2000 1000 800)"<<endl;
for(i=temp;i<temp+number;i++)
{
cin>>worker[i].shop;
cin>>worker[i].num;
cin>>worker[i].name;
cin>>worker[i].base_salary;
cin>>worker[i].post_salary;
cin>>worker[i].bonus;
}
temp=temp+number;//current total is adding temp and number
Statistic(worker,temp);
Sort(worker,temp);
return temp;
}
void Lookup(struct employee worker[],int temp)
{
int number,i;
cout<<"Please input the number you want to lookup:"<<endl;
cin>>number;
if(wrong(worker,temp,number)==1)
cout<<"Sorry we don't have this employee!"<<endl;
else
{
for(i=0;i<temp;i++)
{
if(number==worker[i].num)
{
cout<<setw(10)<<setiosflags(ios::left)<<worker[i].shop;//set the field width
cout<<setw(10)<<worker[i].num;
cout<<setw(8)<<worker[i].name;
cout<<setw(6)<<worker[i].base_salary;
cout<<setw(6)<<worker[i].post_salary;
cout<<setw(6)<<worker[i].bonus;
cout<<setw(6)<<worker[i].deal_salary;
cout<<setw(6)<<worker[i].fund;
cout<<setw(6)<<worker[i].tax;
cout<<setw(6)<<worker[i].fac_salary<<endl;
break;
}
}
}
}
int Delete(struct employee worker[100],int temp)
{
int number,i,j=0;
cout<<"Please input the number of the person who is the one you want to delete!"<<endl;
cin>>number;
if(wrong(worker,temp,number)==1)
cout<<"Sorry we don't have this employee!"<<endl;
else
{
Sort(worker,temp);
if(number==worker[temp-1].num)
temp=temp-1;//if the node is the final node,change the temp directly
else//move the following data to the current data one by one
{
for(i=0;i<temp;i++)
{
if(worker[i].num==number)
{
j=i;//store the index of the node to be deleted
break;
}
}
for(i=j+1;i<temp;i++)
{
worker[i-1]=worker[i];
}//move the following data to the current data one by one
temp=temp-1;//decrease the temp
}
cout<<"number "<<number<<" worker's record has been deleted!"<<endl;
cout<<"Now the temp is:"<<temp<<endl;
}
return temp;//return the current temp
}
int get_index(struct employee worker[],int number,int temp)//get the index of the assigned number
{
int i,j;
for(i=0;i<temp;i++)
{
if(worker[i].num==number)
{
j=i;
break;
}
}
return j;
}
void Modify(struct employee worker[100],int temp)//modify the data,permit user selection
{
int number,index;
cout<<"Please input number of the worker you want to modify(Example:20040001)"<<endl;
cin>>number;
if(wrong(worker,temp,number)==1)
cout<<"Sorry we don't have this employee!"<<endl;
else
{
index=get_index(worker,number,temp);
rewrite(worker,index);//invoke the rewrite sub program
Statistic(worker,temp);
Sort(worker,temp);
}
}
void rewrite(struct employee worker[100],int index)
{
char a;
cout<<"shop(s) name(n) base_salary(b) post_salary(p) bonus(o) quit(q)"<<endl;
cout<<"Press one letter among these items to choose the corresponging function:"<<endl;
cin>>a;
if(a=='q')
cout<<"You have modified all the data successfully!"<<endl;
else
switch(a)//select respective function
{
case 's':
{
cout<<"you have chosen to modify the shop item"<<endl;
cout<<"The previous data is: "<<worker[index].shop<<endl;
cout<<"Please input the new data:"<<endl;
cin>>worker[index].shop;
cout<<"The current data is: "<<worker[index].shop<<endl;
rewrite(worker,index);
}
break;
case 'n':
{
cout<<"you have chosen to modify the name item"<<endl;
cout<<"The previous data is: "<<worker[index].name<<endl;
cout<<"Please input the new data:"<<endl;
cin>>worker[index].name;
cout<<"The current data is: "<<worker[index].name<<endl;
rewrite(worker,index);//revoke the rewrite function
}
break;
case 'b':
{
cout<<"you have chosen to modify the base_salary item"<<endl;
cout<<"The previous data is: "<<worker[index].base_salary<<endl;
cout<<"Please input the new data:"<<endl;
cin>>worker[index].base_salary;
cout<<"The current data is: "<<worker[index].base_salary<<endl;
rewrite(worker,index);
}
break;
case 'p':
{
cout<<"you have chosen to modify the post_salary item"<<endl;
cout<<"The previous data is: "<<worker[index].post_salary<<endl;
cout<<"Please input the new data:"<<endl;
cin>>worker[index].post_salary;
cout<<"The current data is: "<<worker[index].post_salary<<endl;
rewrite(worker,index);
}
break;
case 'o':
{
cout<<"you have chosen to modify the bonus item"<<endl;
cout<<"The previous data is: "<<worker[index].bonus<<endl;
cout<<"Please input the new data:"<<endl;
cin>>worker[index].bonus;
cout<<"The current data is: "<<worker[index].bonus<<endl;
rewrite(worker,index);
}
break;
default:
{
cout<<"you have input the wrong letter,please check and try again!"<<endl;
rewrite(worker,index);
}
break;//if the input letter is not included in the lettr above,give wrong message
}
}
int Insert(struct employee worker[100],int temp)
{
int i,amount;
cout<<"Please input the amount you want to insert:"<<endl;
cin>>amount;//give the amount number you want to insert
cout<<"Input data:"<<endl;
for(i=temp;i<(temp+amount);i++)//insert the data after the whole information
{
cin>>worker[i].shop;
cin>>worker[i].num;
cin>>worker[i].name;
cin>>worker[i].base_salary;
cin>>worker[i].post_salary;
cin>>worker[i].bonus;
}
temp=temp+amount;
Statistic(worker,temp);//compute the rest items
Sort(worker,temp);//sort the whole table
cout<<"The new record has been inserted successfully!"<<endl;
return temp;
}
void display(struct employee worker[100],int temp)//display the record onto the screen
{
cout<<"There are "<<temp<<"records in the list."<<endl;
int i;
for(i=0;i<temp;i++)//temp is the total amount
{
cout<<setw(15)<<setiosflags(ios::left)<<worker[i].shop;
cout<<setw(10)<<worker[i].num;
cout<<setw(8)<<worker[i].name;
cout<<setw(6)<<worker[i].base_salary;
cout<<setw(6)<<worker[i].post_salary;
cout<<setw(6)<<worker[i].bonus;
cout<<setw(6)<<worker[i].deal_salary;
cout<<setw(6)<<worker[i].fund;
cout<<setw(6)<<worker[i].tax;
cout<<setw(6)<<worker[i].fac_salary<<endl;
}
}
int wrong(struct employee worker[100],int temp,int number)
{
int i=0;
for(i=0;i<temp;i++)
{
if(worker[i].num==number)
break;
}
if(i==temp)
return 1;
else return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -