前几天帮了一个大一的小学妹写了一个简单的职工信息管理系统(c++写的,她们还没学到面向对象的编程)。想着大一的时候也做过类似的作业,当时真的是一个头两个大,完全不知道该怎么下手。下面我就把题目要求和代码发表在这篇博客里,希望能对初学编程的人有所帮助。
问题描述:研发一个员工工资管理程序,实现对职工工资的管理,包括工资的添加,查询(按员工号),人员的删除(按员工号删除),实发工资的汇总,修改,排序(按工号排序,实发工资排序)等操作。职工的信息包括职工号,姓名,岗位工资,薪级工资,职务津贴,公会费,保险费,水电费,应发工资,实发工资等信息。其中应发工资和实发工资应在前面六项数值的基础上求得:
应发工资=岗位工资+薪级工资+职务津贴
实发工资=应发工资-公会费-保险费-水电费
程序所显示的菜单如下:
请选择下面的一个项目:
1. 添加
2. 查询
3. 删除
4. 修改
5. 排序
6. 输出
7.退出
#include <iostream>
#include <cstring>
using namespace std;
int length=0;//代表实际的员工数目
// 员工的基本信息
struct client{
int num;//职工号
string name;//姓名
double basicWage;//岗位工资
double dutyWage;//薪级工资
double allowance;//职务津贴
double gonghui;//工会费
double baoxian;//保险费
double shuidian;//水电费
double yingfawage;//应发工资
double shifawage;//实发工资
};
//员工薪资管理系统的起始页面
void setMenu(){
cout<<"*************************职工薪资信息管理系统**********************************"<<endl;
cout<<"* *"<<endl;
cout<<"* 请选择下面的一个项目: *"<<endl;
cout<<"* *"<<endl;
cout<<"* 1 添加 *"<<endl;
cout<<"* *"<<endl;
cout<<"* 2 查询 *"<<endl;
cout<<"* *"<<endl;
cout<<"* 3 删除 *"<<endl;
cout<<"* *"<<endl;
cout<<"* 4 修改 *"<<endl;
cout<<"* *"<<endl;
cout<<"* 5 排序 *"<<endl;
cout<<"* *"<<endl;
cout<<"* 6 输出 *"<<endl;
cout<<"* *"<<endl;
cout<<"* 7 退出 *"<<endl;
cout<<"* *"<<endl;
cout<<"*******************************************************************************"<<endl;
}
//添加员工信息函数
void add(struct client* p){
cout<<"请输入要添加信息的员工数目"<<endl;
int n;cin>>n;
for(int i=0;i<n;i++){
cout<<"请输入第"<<length+1<<"位员工的基本信息"<<endl;
cout<<'\n';
cout<<"请输入第"<<length+1<<"位员工的姓名"<<endl;
string s;cin>>s;
p[length].name=s;
cout<<"请输入第"<<length+1<<"位员工的职工号"<<endl;
int a;cin>>a;
p[length].num=a;
cout<<"请输入第"<<length+1<<"位员工的岗位工资"<<endl;
double b;cin>>b;
p[length].basicWage=b;
cout<<"请输入第"<<length+1<<"位员工的薪级工资"<<endl;
double c;cin>>c;
cout<<"请输入第"<<length+1<<"位员工的职务津贴"<<endl;
double d;cin>>d;
p[length].allowance=d;
cout<<"请输入第"<<length+1<<"位员工的公会费"<<endl;
double e;cin>>e;
p[length].gonghui=e;
cout<<"请输入第"<<length+1<<"位员工的保险费"<<endl;
double f;cin>>f;
p[length].baoxian=f;
cout<<"请输入第"<<length+1<<"位员工的水电费"<<endl;
double g;cin>>g;
p[length].shuidian=g;
p[length].yingfawage=p[length].basicWage+p[length].dutyWage+p[length].allowance;
p[length].shifawage=p[length].yingfawage-p[length].gonghui-p[length].baoxian-p[length].shuidian;
length++;//注意length值必须发生变化
}
}
//输出特定员工的基本信息
void show(struct client p){
cout<<"该员工的基本信息如下:"<<endl;
cout<<"员工号:"<<p.num<<'\t'<<"姓名;"<<p.name<<'\t'<<"岗位工资:"<<p.basicWage<<'\t'<<"职务津贴:"<<p.allowance<<endl;
cout<<"工会费:"<<p.gonghui<<'\t'<<"保险费;"<<p.baoxian<<'\t'<<"水电费:"<<p.shuidian<<endl;
cout<<"应发工资:"<<p.yingfawage<<'\t'<<"实发工资"<<p.shifawage<<endl;
}
// 查找员工信息函数(按员工号进行查找)
void searClient(struct client* p){
void show(struct client j); //函数声明
cout<<"请输入带查找员工的员工号"<<endl;
int n,i;
cin>>n;
bool isfind=false;
for(i=0;i<length;i++){
if(n==p[i].num){
isfind=true;
break; //如果员工号相符,则找的到
}
}
if(isfind)show(p[i]);
else cout<<"不存在该编号的员工"<<endl;
}
//删除特定编号员工的信息(按工号删除)
void deleClient(client* p){
int i;
cout<<"请输入要删除的员工编号"<<endl;
int bianhao; cin>>bianhao;
bool isfind=false;
for(int i=0;i<length;i++){
if(bianhao==p[i].num){
p[i]=p[i+1];
isfind=true;//如果查找到该员工的编号,则用后一个员工的数据将前一个员工的数据覆盖
length--;//注意删除一个之后一定要将length的值减一
}
}
if(isfind) cout<<"删除操作成功"<<endl;
else cout<<"删除操作失败,不存在该员工的编号"<<endl;
}
int modifiedClient(client* p){
cout<<"请输入要修改员工的员工号"<<endl;
int n;cin>>n;
for(int i=0;i<length;i++){
if(n==p[i].num){
system("cls");
cout<<"*************************修改操作菜单****************************"<<'\n'<<'\n';
cout<<" 1. 修改员工姓名"<<endl;
cout<<'\n';
cout<<" 2. 修改员工员工号"<<endl;
cout<<'\n';
cout<<" 3. 修改员工岗位薪资"<<endl;
cout<<'\n';
cout<<" 4. 修改员工薪级工资"<<endl;
cout<<'\n';
cout<<" 5. 修改员工职务津贴"<<endl;
cout<<'\n';
cout<<" 6. 修改员工公会费"<<endl;
cout<<'\n';
cout<<" 7. 修改员工保险费"<<endl;
cout<<'\n';
cout<<" 8. 修改员工水电费"<<endl;
cout<<'\n';
cout<<" 0. 退出修改,返回主菜单"<<endl;
cout<<'\n'<<'\n';
cout<<"请输要进行的操作序号"<<endl;
int n;cin>>n;
switch(n){
case 0: break;
case 1:{cout<<"请输入该员工的姓名"<<endl;
string s;cin>>s;
p[i].name=s;break;
}
case 2:{cout<<"请输入该员工的员工号"<<endl;
int a;cin>>a;
p[i].num=a;break;
}
case 3:{cout<<"请输入该员工的岗位薪资"<<endl;
double b;cin>>b;
p[i].basicWage=b;break;
}
case 4:{cout<<"请输入该员工的薪级工资"<<endl;
double e;cin>>e;
p[i].dutyWage=e;break;
}
case 5:{cout<<"请输入该员工的职务津贴"<<endl;
double f;cin>>f;
p[i].allowance=f;break;
}
case 6:{cout<<"请输入该员工的工会费"<<endl;
double g;cin>>g;
p[i].gonghui=g;break;
}
case 7:{cout<<"请输入该员工的保险费"<<endl;
double h;cin>>h;
p[i].baoxian=h;break;
}
case 8:{cout<<"请输入该员工的水电费"<<endl;
double m;cin>>m;
p[i].shuidian=m;break;
}
}
}
}
}
void sort(struct client* p){
cout<<"*******************排序的种类****************"<<endl;
cout<<'\n'<<'\n';
cout<<" 1. 按员工号升序排序"<<endl;
cout<<'\n';
cout<<" 2. 按员工号降序排序"<<endl;
cout<<'\n';
cout<<" 3. 按实发工资升序排序"<<endl;
cout<<'\n';
cout<<" 4. 按实发工资降序排序"<<endl;
cout<<'\n';
cout<<"请选择所需排序的编号"<<endl;
int n;cin>>n;
switch(n){
case 1:{
for(int i=0;i<length-1;i++){
for(int j=i+1;j<length;j++){
if(p[j].num<p[i].num){
client temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
break;
}
case 2:{
for(int i=0;i<length-1;i++){
for(int j=i+1;j<length;j++){
if(p[j].num>p[i].num) {
client temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
break;
}
case 3:{
for(int i=0;i<length-1;i++){
for(int j=i+1;j<length;j++){
if(p[j].shifawage<p[i].shifawage){
client temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
break;
}
case 4:{
for(int i=0;i<length-1;i++){
for(int j=i+1;j<length;j++){
if(p[j].shifawage>p[j].shifawage){
client temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
break;
}
}
cout<<"排序操作完成"<<endl;
cout<<'\n'<<'\n';
}
void showAll(struct client* p){
void show(client p);
cout<<" 所有员工的信息如下"<<endl;
cout<<'\n';
for(int i=0;i<length;i++){
cout<<"第"<<i+1<<"位员工信息如下:"<<endl;
show(p[i]);
cout<<'\n';
}
}
//主函数
int main(){
client a[100];
client *p=a;
menu: setMenu();
int n;cin>>n;
while(1){
switch(n){
case 1:{system("cls");//清屏操作
add(p);
cout<<'\n'<<'\n';
cout<<"输入1回到主菜单,输入0退出程序"<<endl;
int j;cin>>j;
if(j==1){
system("cls");//清屏操作
goto menu;//程序返回menu接着执行
}
if(j==0) exit(0);break;
}
case 2:{
system("cls");
searClient(p);
cout<<'\n'<<'\n';
cout<<"输入1回到主菜单,输入0退出程序"<<endl;
int o;cin>>o;
if(o==1){
system("cls");
goto menu;
}
if(o==0) exit(0);break;
}
case 3:{
system("cls");
deleClient(p);
cout<<'\n'<<'\n';
cout<<"输入1回到主菜单,输入0退出程序"<<endl;
int p;cin>>p;
if(p==1){
system("cls");
goto menu;
}
if(p==0) exit(0);break;
}
case 4:{
system("cls");
modifiedClient(p);
system("cls");
goto menu;//修改操作结束直接返回主菜单
}
case 5:{
system("cls");
sort(p);
cout<<"输入1回到主菜单,输入0退出程序"<<endl;
int n;cin>>n;
if(n==1) {
system("cls");
goto menu;
}
if(n==0) exit(0);
break;
}
case 6:{
system("cls");
showAll(p);
cout<<"输入1回到主菜单,输入0退出程序"<<endl;
int n;cin>>n;
if(n==1){
system("cls");
goto menu;
}
if(n==0) exit(0);
break;
}
case 7:{
exit(0);
break;
}
}
}
return 0;
}
这个小管理系统的关键在于设置了一个全局变量length,来代表实际员工的个数,方便我们进行下面的操作(如排序和输出函数均用到length)。因此在编程的过程中要注意改变length的值。
下面添加几张程序运行的截图: