C++实现简单职工管理系统
本文实例为大家分享了C++职工管理系统实例代码,供大家参考,具体内容如下
1.单个职工的头文件
staff.h
#ifndefSTAFF_H_INCLUDED #defineSTAFF_H_INCLUDED //结构体创建 structstaff { charID[10]; charname[10]; charsex[10]; intpay; intreward; intfactpay; }; //自定义结构体 typedefstructstaffstaff; //单个职工信息创建 staffCreatestaff(); //单个职工信息输出 voidDisplaystaff(staffstaff); //修改职工信息 voidupdatestaff(staff*Staff); #endif//STAFF_H_INCLUDED 单个职工的cpp文件 staff.cpp #include<stdio.h> #include<stdlib.h> #include"staff.h" staffCreatestaff() { staffstaff; printf("-----------ID-----------\n"); scanf("%s",staff.ID); printf("-----------name-----------\n"); scanf("%s",staff.name); printf("-----------sex-----------\n"); scanf("%s",staff.sex); printf("-----------pay-----------\n"); scanf("%d",&staff.pay); printf("-----------reward-----------\n"); scanf("%d",&staff.reward); staff.factpay=staff.pay+staff.reward; printf("\n"); returnstaff; } voidDisplaystaff(staffstaff) { printf("%10s",staff.ID); printf("%10s",staff.name); printf("%10s",staff.sex); printf("%10d",staff.pay); printf("%10d",staff.reward); printf("%10d",staff.factpay); printf("\n"); } voidupdatestaff(staff*Staff) { printf("-----请显示要修改的数据--------\n"); Displaystaff(*Staff); printf("-------请输入要修改的数据---------"); printf("-----------pay-----------\n"); scanf("%d",&Staff->pay); printf("-----------reward-----------\n"); scanf("%d",&Staff->reward); Staff->factpay=Staff->pay+Staff->reward; printf("\n"); }
2.链表的创建
链表的头文件
linklist.h
#ifndefLINKLIST_H_INCLUDED #defineLINKLIST_H_INCLUDED #include"staff.h" //链表结点创建 structNode { structstaffStaff; structNode*next; }; //自定义结点 typedefstructNodenode; typedefstructNode*linklist; //创建链表 node*Createlinklist(); //输出链表中的数据 voidDisplaylinklist(node*head); //按职工号查找职工 node*searchnode(node*head,charID[]); //按姓名查找职工 voidsearchnodebyname(node*head,charname[]); //删除职工 voiddelenode(linklisthead,charID[]); //插入职工 voidinsertnode(linklisthead,staffStaff); //链表销毁 voiddistroylinklist(linklisthead); #endif//LINKLIST_H_INCLUDED
链表创建的源程序
linklist.cpp
#include<stdio.h> #include<stdlib.h> #include<string.h> #include"staff.h" #include"linklist.h" node*Createlinklist() { node*head,*p; head=(node*)malloc(sizeof(node)); head->next=NULL; staffa[100]={{"11111","mmm","f",12000,2000,14000}, {"22222","aaa","m",13000,3000,16000}, {"33333","sss","f",15000,3000,18000}, {"44444","fff","m",17000,8000,25000}, {"55555","ggg","f",20000,5000,25000}}; for(inti=0;i<5;i++) { p=(node*)malloc(sizeof(node)); p->Staff=a[i]; p->next=head->next; head->next=p; } returnhead; } voidDisplaylinklist(node*head) { linklistp; p=head->next; while(p!=NULL) { Displaystaff(p->Staff); p=p->next; } } node*searchnode(node*head,charID[]) { linklistp; p=head; while(p!=NULL&&strcmp(p->next->Staff.ID,ID)!=0) { p=p->next; } returnp->next; } voidsearchnodebyname(node*head,charname[]) { linklistp; p=head; while((p!=NULL)&&(strcmp((p->next)->Staff.name,name)!=0)) { p=p->next; } printf("-----´ËÈËΪ---------\n"); printf("%s",p->next->Staff.name); printf("\n"); } voiddelenode(linklisthead,charID[]) { linklistp; p=head; while(p->next&&(strcmp(p->next->Staff.ID,ID)!=0)) { p=p->next; } if(p->next) { p->next=p->next->next; } else { printf("=====NOFOUND========\n"); } } voidinsertnode(linklisthead,staffStaff) { linklistp; p=(node*)malloc(sizeof(node)); p->Staff=Staff; p->next=head->next; head->next=p; } voiddistroylinklist(linklisthead) { linklistp; p=head; while(p!=NULL) { p=p->next; free(p); } }
3.文件存盘
file.h
#ifndefFILE_H_INCLUDED #defineFILE_H_INCLUDED #include"linklist.h" #include"staff.h" //职工信息存盘 voidsaveinformation(linklisthead); //职工信息加载 voidloadinformation(linklisthead); #endif//FILE_H_INCLUDED file.cpp #include<stdio.h> #include<string.h> #include<stdlib.h> #include"file.h" #include"linklist.h" #include"staff.h" voidsaveinformation(linklisth) { FILE*fp; linklistp; if((fp=fopen("stu.txt","w"))==NULL) { printf("Failuretoopenstu.txt!\n"); exit(0); } for(p=h->next;p;p=p->next) { fwrite(&(p->Staff),sizeof(node),1,fp); } fclose(fp); } voidloadinformation(linklisth) { FILE*fp; staffnodeBuffer; if((fp=fopen("stu.txt","r"))==NULL) { printf("\n\t数据文件丢失或为首次运行,将加载测试数据\n"); return; } while(fread(&nodeBuffer,sizeof(node),1,fp)!=0) { insertnode(h,nodeBuffer); } }
4.主函数
mainmeun.cpp
#include<stdio.h> #include<stdlib.h> #include"linklist.h" #include"staff.h" #include"file.h" voidmainmeun(linklisthead); voidsearchmenu(linklisthead); intmain(void) { linklisthead=NULL; //intn; //printf("------请输入你要存的数据----------\n"); //scanf("%d",&n); head=Createlinklist(); system("cls"); //Displaylinklist(head); mainmeun(head); printf("\n\n"); //loadinformation(head); //saveinformation(head); return0; } voidmainmeun(linklisthead) { linklistp; charID[10]; //charname[10]; staffStaff; intselection; intflag=1; do { printf("=================职工管理系统===================\n"); printf("==========1.链表输出=====2.数据查询=====\n"); printf("=======3.数据删除===4.数据修改=====5.添加数据======\n"); printf("=======6.链表销毁===7.信息存盘=====8.放弃存盘=====\n"); printf("==================================================\n"); printf("======请选择功能(1~8):"); scanf("%d",&selection); switch(selection) { case1: Displaylinklist(head); break; case2: searchmenu(head); break; case3: printf("=========请输入工号==========\n"); scanf("%s",ID); delenode(head,ID); break; case4: printf("=========请输入工号==========\n"); scanf("%s",ID); p=searchnode(head,ID); updatestaff(&(p->Staff)); break; case5: printf("========添加数据========="); Staff=Createstaff(); insertnode(head,Staff); break; case6: distroylinklist(head); break; case7: loadinformation(head); saveinformation(head); break; case8: flag=0; break; } }while(flag==1); printf("========BYE=====BYE======"); } voidsearchmenu(linklisthead) { linklistp; intflag=1; charID[10]; charname[10]; do { printf("=========查找菜单===========\n"); printf("===1.ID======2.name====3.退出====\n"); printf("=================================\n"); intselection; printf("==请选择功能(1~3):"); scanf("%d",&selection); switch(selection) { case1: printf("=====请输入ID=======\n"); scanf("%s",ID); p=searchnode(head,ID); Displaystaff(p->Staff); break; case2: printf("=====请输入name======\n"); scanf("%s",name); searchnodebyname(head,name); break; case3: flag=0; break; } system("pause"); system("cls"); }while(flag==1); }
推荐几篇文章:
C++实现简单的图书管理系统
C++实现简单的职工信息管理系统
C++基础学生管理系统
更多学习资料请关注专题《管理系统开发》。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。