数据结构课上完了,我们需要自己选一个课程设计,我们小组选了如下的人事管理系统
这道题目中,我多加了一个板块,是系统板块,大致是原本我们只需要设计一个人事管理系统,但我觉得系统应该多个,所以我在外层加了一个系统菜单,其次这道题目比较麻烦的有两点,一点是员工的信息太多了,敲的手都酸死,第二点是按薪水进行排序,我用的直接快速排序,时间复杂度为0n(2)。其次我还学到一个新的知识点:为什么不能给结构体指针中的string变量用“=”赋值(更新),也不能输出,但能通过编译??这个问题我在题目中遇到一模一样的,答案是:这里得用new,因为里面有string需要初始化malloc只是申请内存,是无法调用string类型里的初始化函数没有经过初始化的string,在程序执行到这里时出错:node->name =”a”;,所以,在代码中我放弃了malloc,而直接用new,关于new也是新学到的知识点,这里有必要写个笔记。
C++中new运算符用于动态分配和撤销内存的运算符。
1、开辟单变量地址空间
new int; //开辟一个存放数组的存储空间,返回一个指向该存储空间的地址.int *a = new int 即为将一个int类型的地址赋值给整型指针a. 2)int *a = new int(5) 作用同上,但是同时将整数赋值为5。
2、开辟数组空间
要访问new所开辟的结构体空间,无法直接通过变量名进行,只能通过赋值的指针进行访问。用new可以动态开辟,撤销地址空间。在编程序时,若用完一个变量,下次需要再用,可以在每次开始使用时开辟一个空间,在用完后撤销它。
接下来把我写的代码贴出来,如果有写的不好的地方,欢迎大家指正。
#include<iostream>
#include<string>
#include<malloc.h>
#include<vector>
#include<sstream>
#define maxsize 100
using namespace std;
//管理系统采用指针结构,目的是为了便于摧毁
int NumberSystem=0;//系统的数量,初始为0
int IndexSystem=0;//系统的编号,初始为0
int nber=0;//某个系统中成员的最大容量
bool enter=true;//程序进入退出总开关
typedef struct{
string name;//姓名
string sex;//性别
string workid;//工作证号
string idcard;//身份证号
string birthday;//生日日期
string adress;//家庭住址
string telephone;//电话号码
int salary;//薪水
string duty;//职务
}elemtype;//成员信息结构体
typedef struct{
elemtype number[maxsize];
string name;//系统的名字
int idenx;//系统的编号
int length;//成员人数
}nsystem;//人事管理系统结构体
vector<nsystem*> st;//系统管理栈
void initsystem (nsystem *&s,string NameSystem){
//这里解释一下为什么不用malloc,malloc只是申请内存,是无法调用string类型里的
//初始化函数,没有经过初始化的string类型(系统的名字),是无法被赋值的
s=new nsystem;//用new运算开辟一个存放nsystem型的数据空间
s->name=NameSystem;
s->length=0;
IndexSystem++;
s->idenx=IndexSystem;
}
void destorysystem(nsystem*&s){
delete s;//撤销该空间
}
void hr(){
cout<<"---------------------------------------------"<<endl;
}
void clear(){
// cout<<"输入yes清屏,输入其他值不清屏"<<endl;
// string a;
// cin>>a;
// if(a=="yes"||a=="y")
system("pause");
system("cls");//清屏
}
bool isNum(string str){
stringstream sin(str);
double d;
char c;
if(!(sin>>d))
return false;
if(sin>>c)
return false;
return true;
}
void menu2(){
hr();
clear();
cout<<"\t1.添加员工信息"<<endl;
cout<<"\t2.删除员工信息"<<endl;
cout<<"\t3.显示所有员工信息"<<endl;
cout<<"\t4.查找某个员工信息"<<endl;
cout<<"\t5.修改某个员工信息"<<endl;
cout<<"\t6.将员工按薪水排序"<<endl;
cout<<"\t7.显示该系统的名字"<<endl;
cout<<"\t8.回到主菜单"<<endl;
hr();
}
void menu1(){
hr();
clear();
cout<<"请选择选项操作:"<<endl;
cout<<"\t1.添加系统"<<endl;
cout<<"\t2.删除系统"<<endl;
cout<<"\t3.进入系统"<<endl;
cout<<"\t4.查看所有系统"<<endl;
cout<<"\t5.退出程序"<<endl;
hr();
}
//查看所有系统函数
void looksnsystem(){
nsystem*s1=st.front();
nsystem*s2=st.back();
if(NumberSystem==1){
hr();
cout<<"你有以下系统:"<<endl;
cout<<"\t系统编号:"<<s1->idenx<<"\t系统名称:"<<s1->name<<"\t成员人数:"<<s1->length<<"人"<<endl;
hr();
}
else{
hr();
cout<<"你有以下系统:"<<endl;
cout<<"\t系统编号:"<<s1->idenx<<"\t系统名称:"<<s1->name<<"\t成员人数:"<<s1->length<<"人"<<endl;
cout<<"\t系统编号:"<<s2->idenx<<"\t系统名称:"<<s2->name<<"\t成员人数:"<<s2->length<<"人"<<endl;
hr();
}
}
//添加系统函数
void insertnsystem(){
if(NumberSystem<2){
cout<<"请输入系统的名字"<<endl;
hr();
string NameSystem;//系统的名字
cin>>NameSystem;//读入系统名
nsystem *s1;//声明管理系统s1
initsystem(s1,NameSystem);//初始化s1系统
st.push_back(s1);//
NumberSystem++;
cout<<"创建成功!"<<endl;
hr();
}
else{
cout<<"由于服务器空间有限,目前只能创建两个系统"<<endl;
cout<<"若要开通高级服务,请付费8888元,如有疑问,请咨询客服。"<<endl;
hr();
}
}
//删除系统函数
void deletensystem(){
looksnsystem();
cout<<"请按编号删除不需要的系统"<<endl;
cout<<"请输入系统编号"<<endl;
hr();
int choose;
cin>>choose;
if(choose==1){
nsystem*s=st.back();
st.clear();
st.push_back(s);
cout<<"删除成功"<<endl;
NumberSystem--;
}
else if(choose==2){
nsystem*s=st.front();
st.clear();
st.push_back(s);
cout<<"删除成功"<<endl;
NumberSystem--;
}
else{
cout<<"你的输入有误!"<<endl;
hr();
}
}
//进入该系统函数
nsystem* enternsystem(){
looksnsystem();
cout<<"请按编号删除进入想要进的系统"<<endl;
cout<<"请输入系统编号"<<endl;
hr();
int choose;
cin>>choose;
nsystem*s;
if(choose==1){
s=st.front();
cout<<"你已经成功进入'"<<s->name<<"'系统"<<endl;
hr();
cout<<"接下来,为你调出系统内管理菜单,请稍等..."<<endl;
hr();
return s;
}
if(choose==2){
s=st.back();
cout<<"你已经成功进入'"<<s->name<<"'系统"<<endl;
hr();
cout<<"接下来,为你调出系统内管理菜单,请稍等..."<<endl;
hr();
return s;
}
}
//判断身份证号函数
bool isworkid(){
}
//判断工作证号函数
bool isidcard(){
}
//判断生日日期函数
bool isbirthday(string birthday){
}
//判断电话号码函数
bool istelephone(){
}
//字符串拼接函数(将s2拼接到s1后面)
//string mystr(string s1,string s2){
// int len=s1.length();
// int i,j;
// for(i=len,j=0;s2[j]!='\0';i++,j++){
// s1[i]=s2[j];
// }
// s1[i]="\0";
// return s1;
//}
//判断员工性别函数
bool issex(string sex){
if(sex=="男"||sex=="女")
return true;
else
return false;
}
//添加员工函数
void insertber(nsystem *s){
string name,sex,workid,idcard,birthday,adress,telephone,duty;
int salary;
string year1,month1,day1;
int year,month,day;
cout<<"请依次输入员工的姓名"<<endl;
cin>>name;
s->number[nber].name=name;
cout<<"请输入员工的性别"<<endl;
while(cin>>sex){
if(issex(sex)){
break;
}
else{
cout<<"性别输入有误,请重新输入"<<endl;
//continue;
}
}
s->number[nber].sex=sex;
cout<<"请输入员工的工作证号(七位)"<<endl;
while(cin>>workid){
if(!isNum(workid)){
cout<<"工作证号由数字组成,你的输入有误"<<endl;
continue;
}
if(workid.length()==7){
s->number[nber].workid=workid;
break;
}
else{
cout<<"你输入的员工工作证号有误,请重新输入"<<endl;
continue;
}}
cout<<"请输入员工的身份证号(18位)"<<endl;
while(cin>>idcard){
if(idcard.length()==18){
s->number[nber].idcard=idcard;
break;
}
else{
cout<<"你输入的身份证号有误,请重新输入"<<endl;
continue;
}}
cout<<"请输入员工的生日日期(请按格式输入xx年xx月xx日)"<<endl;
cin>>birthday;
s->number[nber].birthday=birthday;
//修改后与的生日(未完成)
// cout<<"请输入员工的出生年份"<<endl;
// while(cin>>year){
// if(year>2010||year<1900){
// cout<<"年份输入有误,请重新输入"<<endl;
// continue;
// }else break;
// }
// cout<<"请输入员工的出生月份"<<endl;
// while(cin>>month){
// if(month<=0||month>=13){
// cout<<" 月份输入有误,请重新输入"<<endl;
// continue;
// }else break;
// }
// cout<<"请输入员工的出生日"<<endl;
// while(cin>>day){
// if(day<=0||day>=31){
// cout<<"日输入有误,请重新输入"<<endl;
// continue;
// }else break;
// }
// year1=""+year;month1=""+month;day1=""+day;
// birthday=year1+"年"+month1+"月"+day1+"day";
// s->number[nber].birthday=birthday;
// cout<<"请输入员工的家庭住址"<<endl;
// cin>>adress;
// s->number[nber].adress=adress;
cout<<"请输入员工的电话号码(11位)"<<endl;
while(cin>>telephone){
if(!isNum(telephone)){
cout<<"你输入的电话号码有误"<<endl;
continue;
}
if(telephone.length()==11){
s->number[nber].telephone=telephone;
break;
}
else{
cout<<"你输入的电话号码有误,请重新输入"<<endl;
continue;
}
}
cout<<"请输入员工的职务"<<endl;
cin>>duty;
s->number[nber].duty=duty;
cout<<"请输入员工的薪水(请输入数字,以元为单位)"<<endl;
cin>>salary;
s->number[nber].salary=salary;
cout<<"添加完成!"<<endl;
hr();
nber++;
}
//删除员工函数
bool deleteber(nsystem *s){
cout<<"请输入要删除的员工的姓名"<<endl;
string name;
cin>>name;
bool bl=0;
int i;
for( i=0;i<nber;i++){
if(s->number[i].name==name){
bl=1;
break;
}
}
if(bl){
for(int j=i;j<=nber;j++)
s->number[j]=s->number[j+1];
nber--;
return true;
}
else{
cout<<"未找到该员工"<<endl;
hr();
return false;
}
}
//显示员工函数
void dispber(nsystem *s){
for(int i=0;i<nber;i++){
hr();
cout<<"员工"<<i+1<<"的信息:"<<endl;
cout<<"\t姓名:"<<s->number[i].name<<endl;
cout<<"\t性别:"<<s->number[i].sex<<endl;
cout<<"\t工作证号:"<<s->number[i].workid<<endl;
cout<<"\t身份证号:"<<s->number[i].idcard<<endl;
cout<<"\t生日日期:"<<s->number[i].birthday<<endl;
cout<<"\t家庭住址:"<<s->number[i].adress<<endl;
cout<<"\t电话号码:"<<s->number[i].telephone<<endl;
cout<<"\t薪水:"<<s->number[i].salary<<"元"<<endl;
cout<<"\t职务:"<<s->number[i].duty<<endl;
hr();
}
}
//查找员工函数
bool locatber(nsystem*s){
cout<<"请输入要查找的员工的姓名"<<endl;
string name;
cin>>name;
int i;
bool bl=0;
for( i=0;i<nber;i++){
if(s->number[i].name==name){
bl=1;
break;
}
}
if(bl){
hr();
cout<<"员工"<<i+1<<"的信息:"<<endl;
cout<<"\t姓名:"<<s->number[i].name<<endl;
cout<<"\t性别:"<<s->number[i].sex<<endl;
cout<<"\t工作证号:"<<s->number[i].workid<<endl;
cout<<"\t身份证号:"<<s->number[i].idcard<<endl;
cout<<"\t生日日期:"<<s->number[i].birthday<<endl;
cout<<"\t家庭住址:"<<s->number[i].adress<<endl;
cout<<"\t电话号码:"<<s->number[i].telephone<<endl;
cout<<"\t薪水:"<<s->number[i].salary<<"元"<<endl;
cout<<"\t职务:"<<s->number[i].duty<<endl;
hr();
}
else{
cout<<"未找到该员工"<<endl;
hr();
return false;
}
}
//修改员工函数
bool reber(nsystem*s){
cout<<"请输入要修改的员工的姓名"<<endl;
string name;
cin>>name;
int i;
bool bl=0;
for( i=0;i<nber;i++){
if(s->number[i].name==name){
bl=1;
break;
}
}
if(bl){
hr();
cout<<"员工"<<i+1<<"的信息:"<<endl;
cout<<"\t姓名:"<<s->number[i].name<<endl;
cout<<"\t性别:"<<s->number[i].sex<<endl;
cout<<"\t工作证号:"<<s->number[i].workid<<endl;
cout<<"\t身份证号:"<<s->number[i].idcard<<endl;
cout<<"\t生日日期:"<<s->number[i].birthday<<endl;
cout<<"\t家庭住址:"<<s->number[i].adress<<endl;
cout<<"\t电话号码:"<<s->number[i].telephone<<endl;
cout<<"\t薪水:"<<s->number[i].salary<<"元"<<endl;
cout<<"\t职务:"<<s->number[i].duty<<endl;
hr();
cout<<"请输入你要修改该员工的值,输入该员工的信息(一次只能修改一个信息)"<<endl;
hr();
string tmp;
string a;
cin>>a;
if(a=="姓名"){
cout<<"请输入修改后的名字"<<endl;
cin>>tmp;
s->number[i].name=tmp;
}
else if(a=="性别"){
cout<<"请输入修改后的性别"<<endl;
cin>>tmp;
s->number[i].sex=tmp;
}
else if(a=="工作证号"){
cout<<"请输入修改后的工作证号"<<endl;
cin>>tmp;
s->number[i].workid=tmp;
}
else if(a=="身份证号"){
cout<<"请输入修改后的身份证号"<<endl;
cin>>tmp;
s->number[i].idcard=tmp;
}
else if(a=="生日日期"){
cout<<"请输入修改后的生日日期"<<endl;
cin>>tmp;
s->number[i].birthday=tmp;
}
else if(a=="家庭住址"){
cout<<"请输入修改后的家庭住址"<<endl;
cin>>tmp;
s->number[i].adress=tmp;
}
else if(a=="电话号码"){
cout<<"请输入修改后的电话号码"<<endl;
cin>>tmp;
s->number[i].telephone=tmp;
}
else if(a=="职务"){
cout<<"请输入修改后的职务"<<endl;
cin>>tmp;
s->number[i].duty=tmp;
}
cout<<"修改成功"<<endl;
hr();
}
else{
cout<<"未找到该员工"<<endl;
hr();
return false;
}
}
//排序函数
void sortber(nsystem*s){
int i,j,k;
int a=0;//总的工资
int choosex;
cout<<"请选择操作\t1.排序功能\t2.平均工资"<<endl;
while(cin>>choosex){
if(choosex==1){
elemtype tmp;
cout<<"请选择降序or升序,降序选1,升序选2"<<endl;
hr();
int choose;
cin>>choose;
if(choose==2){
for(int i=0;i<nber;i++){
k=i;
for(j=i+1;j<nber;j++)
if(s->number[j].salary<s->number[k].salary)
k=j;
//交换
if(k!=i){
tmp=s->number[i];
s->number[i]=s->number[k];
s->number[k]=tmp;
}
}
cout<<"排序成功"<<endl;
hr();
}
else if(choose==1){
for(int i=0;i<nber;i++){
k=i;
for(j=i+1;j<nber;j++)
if(s->number[j].salary>s->number[k].salary)
k=j;
//交换
if(k!=i){
tmp=s->number[i];
s->number[i]=s->number[k];
s->number[k]=tmp;
}
}
cout<<"排序成功"<<endl;
hr();
}
else
cout<<"输入有误"<<endl;
hr();
}
else if(choosex==2){
for(int i=0;i<nber;i++){
a+=s->number[i].salary;
}
cout<<"平均工资: "<<a/nber<<endl;
}
else{
cout<<"你输入的有误,请重新输入"<<endl;
continue; }
}
}
//系统开始函数
nsystem* welcome(){
cout<<"欢迎进入人事管理系统"<<endl;
if(NumberSystem==0){
cout<<"本程序发现你还没有系统,首先你需要创建一个系统,请为你的系统去取一个动听的名字"<<endl;
hr();
insertnsystem();
nsystem*s=st.back();
cout<<"恭喜你成功创建了一个'"<<s->name<<"'系统接下来为你显示系统菜单"<<endl;
hr();
}
int choosensystem;
nsystem*s;
menu1();
while(cin>>choosensystem){
if(choosensystem==1){
insertnsystem();
menu1();
}
if(choosensystem==2){
deletensystem();
menu1();
}
if(choosensystem==3){
s=enternsystem();
return s;
}
if(choosensystem==4){
looksnsystem();
menu1();
}
if(choosensystem==5){
enter=false;
break;
}
}
}
//某个系统的函数
void welcome2(nsystem*s){
menu2();
bool bl;
int choose;
cin>>choose;
while(1){
if(choose==1){
insertber(s);
menu2();
cin>>choose;
continue;
}
else if(choose==2){
bl=deleteber(s);
if(bl)
cout<<"删除成功!"<<endl;
else
cout<<"删除失败!"<<endl;
hr();
menu2();
cin>>choose;
continue;
}
else if(choose==3){
dispber(s);
menu2();
cin>>choose;
continue;
}
else if(choose==4){
bl=locatber(s);
if(bl)
cout<<"查找成功!"<<endl;
else
cout<<"查找失败!"<<endl;
hr();
menu2();
cin>>choose;
continue;
}
else if(choose==5){
bl=reber(s);
if(bl)
cout<<"修改成功!"<<endl;
else
cout<<"修改失败!"<<endl;
hr();
menu2();
cin>>choose;
continue;
}
else if(choose==6){
sortber(s);
menu2();
cin>>choose;
continue;
} else if(choose==8){
break;
}
else if(choose==7){
cout<<"本系统的名字是:"<<s->name<<endl;
hr();
menu2();
cin>>choose;
continue;
}
}
}
int main(){
system("color 3E");
cout<<"*******************************"<<endl;
cout<<"** ╭╮ ╭╮ ***"<<endl;
cout<<"** ││欢迎进入人事系统││ ***"<<endl;
cout<<"**╭┴┴———————┴┴╮***"<<endl;
cout<<"**│ ● ● │***"<<endl;
cout<<"**│○ ╰┬┬┬╯ ○│***"<<endl;
cout<<"**│ ╰—╯ │***"<<endl;
cout<<"**╰——┬O———O┬——╯***"<<endl;
cout<<"** ╭╮ ╭╮ ***"<<endl;
cout<<"** ╰┴————┴╯ ***"<<endl;
while(enter){
nsystem*s=welcome();//由系统向量中得到一个系统s进行员工管理操作
if(enter)
welcome2(s);
}
cout<<"欢迎下次光临本人事管理系统"<<endl;
}
废江博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 转载请注明原文链接:人事管理系统(数据结构课程设计)