数据结构课上完了,我们需要自己选一个课程设计,我们小组选了如下的人事管理系统
这道题目中,我多加了一个板块,是系统板块,大致是原本我们只需要设计一个人事管理系统,但我觉得系统应该多个,所以我在外层加了一个系统菜单,其次这道题目比较麻烦的有两点,一点是员工的信息太多了,敲的手都酸死,第二点是按薪水进行排序,我用的直接快速排序,时间复杂度为0n(2)。其次我还学到一个新的知识点:为什么不能给结构体指针中的string变量用“=”赋值(更新),也不能输出,但能通过编译??这个问题我在题目中遇到一模一样的,答案是:这里得用new,因为里面有string需要初始化malloc只是申请内存,是无法调用string类型里的初始化函数没有经过初始化的string,在程序执行到这里时出错:node->name =”a”;,所以,在代码中我放弃了malloc,而直接用new,关于new也是新学到的知识点,这里有必要写个笔记。
new关键字
C++中new运算符用于动态分配和撤销内存的运算符。
1、开辟单变量地址空间
new int; //开辟一个存放数组的存储空间,返回一个指向该存储空间的地址.int *a = new int 即为将一个int类型的地址赋值给整型指针a. 2)int *a = new int(5) 作用同上,但是同时将整数赋值为5。
2、开辟数组空间
要访问new所开辟的结构体空间,无法直接通过变量名进行,只能通过赋值的指针进行访问。用new可以动态开辟,撤销地址空间。在编程序时,若用完一个变量,下次需要再用,可以在每次开始使用时开辟一个空间,在用完后撤销它。
接下来把我写的代码贴出来,如果有写的不好的地方,欢迎大家指正。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
#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; } |