目标:熟悉输入与输出流的定义及操作方式。
上机任务:
A、P268 第1题,第2题,第3题,第4题,第5题,第6题。
B、设计一个工资管理系统,计算某高校教师、行政员工、临时工、双肩挑员工的工资数额。要求有简单的界面管理。
实验步骤:
第一步:问题分析和功能定义。分析工资系统的数据组成和工资的计算方法。
第二步:设计类。
工资管理系统程序设计图:
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
const char N=20;
class Staff_Teacher;
void WriteFile(int num,char name[],char sex,double salary);
void InterFace();
void Select();
class Person
{
public:
Person(){num=-1;strcpy(name,"未知");sex='0';salary=-1;}
Person(int a,char const b[],char c):num(a),sex(c){strcpy(name,b);}
virtual void output() =0;
virtual void pay() =0;
virtual void input() =0;
protected:
int num;
char name[N];
char sex;
double salary;
};
class Temporary_Workes:public Person
{
public:
Temporary_Workes(){hourlypay=-1;workinghours=-1;}
Temporary_Workes(int a,char const b[],char c,double d, double e):Person(a,b,c),hourlypay(d),workinghours(e){}
void output();
void pay();
void input();
protected:
double hourlypay;//时薪
double workinghours;//工作时长
};
void Temporary_Workes::output()
{
cout<<"\t工号\t姓名\t性别\t收入"<<endl;
cout<<"\t"<<num<<"\t"<<name<<"\t"<<sex<<"\t"<<salary<<endl;
WriteFile(num,name,sex,salary);
}
void Temporary_Workes::input()
{
cout<<"职工号:"<<endl;
cin>>num;
cout<<"姓名:"<<endl;
cin>>name;
cout<<"性别:"<<endl;
cin>>sex;
cout<<"时薪:"<<endl;
cin>>hourlypay;
cout<<"工作时长:"<<endl;
cin>>workinghours;
pay();
}
void Temporary_Workes::pay()
{
salary=workinghours*hourlypay;
}
class Teacher:virtual public Person
{
public:
Teacher(){teaching_hours=-1;strcpy(title,"不知道");}
Teacher(int a,char const b[],char c,double d, char const e[]):Person(a,b,c),teaching_hours(d){strcpy(title ,e);}
void output();
void input();
void pay();
protected:
double teaching_hours;//授课时长
char title[N];//职称
};
void Teacher::output()
{
cout<<"\t工号\t姓名\t性别\t收入"<<endl;
cout<<"\t"<<num<<"\t"<<name<<"\t"<<sex<<"\t"<<salary<<endl;
WriteFile(num,name,sex,salary);
}
void Teacher::input()
{
cout<<"职工号:"<<endl;
cin>>num;
cout<<"姓名:"<<endl;
cin>>name;
cout<<"性别:"<<endl;
cin>>sex;
cout<<"授课时长:"<<endl;
cin>>teaching_hours;
int n;
cout<<"1---------教授\n2---------副教授\n3---------讲师\n4---------助教\n请选择职称序号:";
cin>>n;
switch(n)
{
case 1:{strcpy(title,"教授");break;}
case 2:{strcpy(title,"副教授");break;}
case 3:{strcpy(title,"讲师");break;}
case 4:{strcpy(title,"助教");break;}
default: cout<<"!!输入错误!!"<<endl;system("pause");exit(0);
}
pay();
}
void Teacher::pay()
{
if(strcmp(title,"教授")==0)
{
salary=teaching_hours*80+6000;
}
else if(strcmp(title,"副教授")==0)
{
salary=teaching_hours*70+5000;
}
else if(strcmp(title,"讲师")==0)
{
salary=teaching_hours*60+4000;
}
else
{
salary=teaching_hours*50+3000;//可怜的助教的工资
}
}
class Staff:virtual public Person
{
public:
Staff(){strcpy(position,"不知道");}
Staff(int a,char const b[],char c, char const d[]):Person(a,b,c){strcpy(position ,d);}
void output();
void input();
void pay();
protected:
char position[N];//职位
};
void Staff::output()
{
cout<<"\t工号\t姓名\t性别\t收入"<<endl;
cout<<"\t"<<num<<"\t"<<name<<"\t"<<sex<<"\t"<<salary<<endl;
WriteFile(num,name,sex,salary);
}
void Staff::input()
{
cout<<"职工号:"<<endl;
cin>>num;
cout<<"姓名:"<<endl;
cin>>name;
cout<<"性别:"<<endl;
cin>>sex;
cout<<"1---------院级员工\n2---------处级员工\n3---------科级员工\n4---------一般员工\n请选择任职序号:";
int n;
cin>>n;
switch(n)
{
case 1:{strcpy(position,"院级员工");break;}
case 2:{strcpy(position,"处级员工");break;}
case 3:{strcpy(position,"科级员工");break;}
case 4:{strcpy(position,"一般员工");break;}
default: {cout<<"!!输入错误!!"<<endl;system("pause");exit(0);}
}
pay();
}
void Staff::pay()
{
if(strcmp(position,"院级员工")==0)
{
salary=6000;
}
else if(strcmp(position,"处级员工")==0)
{
salary=5000;
}
else if(strcmp(position,"科级员工")==0)
{
salary=4000;
}
else
{
salary=3000;//可怜的一般员工
}
}
class Staff_Teacher:virtual public Teacher,virtual public Staff
{
public:
Staff_Teacher(){}
Staff_Teacher(int a,char const b[],char c, char const d[],double e, char const f[]):Person(a,b,c),Teacher(a,b,c,e,f),Staff(a,b,c,d){}
void output();
void input();
void pay();
};
void Staff_Teacher::output()
{
cout<<"\t工号\t姓名\t性别\t收入"<<endl;
cout<<"\t"<<num<<"\t"<<name<<"\t"<<sex<<"\t"<<salary<<endl;
WriteFile(num,name,sex,salary);
}
void Staff_Teacher::input()
{
cout<<"职工号:"<<endl;
cin>>num;
cout<<"姓名:"<<endl;
cin>>name;
cout<<"性别:"<<endl;
cin>>sex;
cout<<"授课时长:"<<endl;
cin>>teaching_hours;
int n;
cout<<"1---------教授\n2---------副教授\n3---------讲师\n4---------助教\n请选择职称序号:";
cin>>n;
switch(n)
{
case 1:{strcpy(title,"教授");break;}
case 2:{strcpy(title,"副教授");break;}
case 3:{strcpy(title,"讲师");break;}
case 4:{strcpy(title,"助教");break;}
default: {cout<<"!!输入错误!!"<<endl;system("pause");exit(0);}
}
cout<<"1---------院级员工\n2---------处级员工\n3---------科级员工\n4---------一般员工\n请选择任职序号:";
cin>>n;
switch(n)
{
case 1:{strcpy(position,"院级员工");break;}
case 2:{strcpy(position,"处级员工");break;}
case 3:{strcpy(position,"科级员工");break;}
case 4:{strcpy(position,"一般员工");break;}
default: {cout<<"!!输入错误!!"<<endl;system("pause");exit(0);}
}
pay();
}
void Staff_Teacher::pay()
{
Staff::pay();
if(strcmp(title,"教授")==0)
{
salary=teaching_hours*80+6000+salary*0.5;
}
else if(strcmp(title,"副教授")==0)
{
salary=teaching_hours*70+5000+salary*0.5;
}
else if(strcmp(title,"讲师")==0)
{
salary=teaching_hours*60+4000+salary*0.5;
}
else
{
salary=teaching_hours*50+3000+salary*0.5;//可怜的助教的工资
}
}
int main()
{
ofstream outfile("教职工数据表.docx",ios::app);
outfile<<"**********以下为工资管理系统每一次的运行的运行数据**********"<<endl;
outfile<<"\t工号\t姓名\t性别\t收入"<<endl;
InterFace();//显示选择的界面
Select();//进行选择
outfile.close();
system("pause");
return 0;
}
void InterFace()
{
cout<<"***************欢迎使用工资管理系统***************"<<endl;
cout<<"***\t\t1.教师 \t\t***"<<endl;
cout<<"***\t\t2.一般职工 \t\t***"<<endl;
cout<<"***\t\t3.临时工 \t\t***"<<endl;
cout<<"***\t\t4.双肩挑教师\t\t***"<<endl;
cout<<"***\t\t0.退出 \t\t***"<<endl;
cout<<"请选择:"<<endl;
}
void Select()
{
int n;
while(true)
{
cin>>n;
switch(n)
{
case 1:
{
Teacher t;
t.input();
t.output();
break;
}
case 2:
{
Staff s;
s.input();
s.output();
break;
}
case 3:
{
Temporary_Workes tw;
tw.input();
tw.output();
break;
}
case 4:
{
Staff_Teacher st;
st.input();
st.output();
break;
}
case 0:{break;}
default:{cerr<<"!!输入错误!!"<<endl;system("pause");exit(0);}
}
if(n==0)
{break;}
cout<<"还请老哥继续上述选择咯"<<endl;
}
}
void WriteFile(int num,char name[],char sex,double salary)
{
ofstream outfile("教职工数据表.docx",ios::app);
if(!outfile)
{
cerr<<"open error!!"<<endl;
exit(1);
}
outfile<<"\t"<<num<<"\t"<<name<<"\t"<<sex<<"\t\t"<<salary<<endl;
outfile.close();
}
该程序同时运行的结果储存在文件中
萌新一枚,还请多多点赞收藏,谢谢大佬们。