项目要求
以菜单方式工作管理每个职工的个人信息。
(1)总共有职工若干名,每个职工是一个记录,包括的信息有:职工号、姓名、性别、出生年月、学历、职务、部门、工资、住址、电话等等(可以根据自己的需求添加),并且可以对职工信息进行录入、删除、修改、浏览等操作。
(2)可以按姓名进行查询。
(3)可以修改职工的信息。
(4)用一个文档来存储职工的信息,并实现数据的读取。
单个职工的头文件:staff.h
#ifndef STAFF_H_INCLUDED #define STAFF_H_INCLUDED //结构体创建 structstaff { charID[10]; charname[10]; charsex[10]; intpay; intreward; intfactpay; }; //自定义结构体 typedefstructstaff staff; //单个职工信息创建 staff Createstaff(); //单个职工信息输出 voidDisplaystaff(staff staff); //修改职工信息 voidupdatestaff(staff *Staff); #endif // STAFF_H_INCLUDED
单个职工的cpp文件:staff.cpp
#include#include #include "staff.h" staff Createstaff() { staff staff; printf("-----------ID----------- "); scanf("%s", staff.ID); printf("-----------name----------- "); scanf("%s", staff.name); printf("-----------sex----------- "); scanf("%s", staff.sex); printf("-----------pay----------- "); scanf("%d", &staff.pay); printf("-----------reward----------- "); scanf("%d", &staff.reward); staff.factpay = staff.pay + staff.reward; printf(" "); returnstaff; } voidDisplaystaff(staff staff) { 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(" "); } voidupdatestaff(staff *Staff) { printf("-----请显示要修改的数据-------- "); Displaystaff(*Staff); printf("-------请输入要修改的数据---------"); printf("-----------pay----------- "); scanf("%d", &Staff->pay); printf("-----------reward----------- "); scanf("%d", &Staff->reward); Staff->factpay = Staff->pay + Staff->reward; printf(" "); }
2.链表的创建
链表的头文件:linklist.h
#ifndef LINKLIST_H_INCLUDED #define LINKLIST_H_INCLUDED #include "staff.h" //链表结点创建 structNode { structstaff Staff; structNode *next; }; //自定义结点 typedefstructNode node; typedefstructNode *linklist; //创建链表 node *Createlinklist(); //输出链表中的数据 voidDisplaylinklist(node *head); //按职工号查找职工 node *searchnode(node *head, charID[]); //按姓名查找职工 voidsearchnodebyname(node *head, charname[]); //删除职工 voiddelenode(linklist head, charID[]); //插入职工 voidinsertnode(linklist head, staff Staff); //链表销毁 voiddistroylinklist(linklist head); #endif // LINKLIST_H_INCLUDED
链表创建的源程序
linklist.cpp #include#include #include #include "staff.h" #include "linklist.h" node *Createlinklist() { node *head, *p; head = (node *)malloc(sizeof(node)); head->next = NULL; staff a[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) { linklist p; p = head->next; while(p!=NULL) { Displaystaff(p->Staff); p = p->next; } } node *searchnode(node *head, charID[]) { linklist p; p = head; while(p!=NULL&&strcmp(p->next->Staff.ID, ID)!=0) { p = p->next; } returnp->next; } voidsearchnodebyname(node *head, charname[]) { linklist p; p = head; while((p!=NULL)&&(strcmp((p->next)->Staff.name, name)!=0)) { p = p->next; } printf("-----´ËÈËΪ--------- "); printf("%s", p->next->Staff.name); printf(" "); } voiddelenode(linklist head, charID[]) { linklist p; 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("=====NO FOUND======== "); } } voidinsertnode(linklist head, staff Staff) { linklist p; p = (node *)malloc(sizeof(node)); p->Staff = Staff; p->next = head->next; head->next = p; } voiddistroylinklist(linklist head) { linklist p; p = head; while(p!=NULL) { p = p->next; free(p); } }
3.文件存盘
file.h
#ifndef FILE_H_INCLUDED #define FILE_H_INCLUDED #include "linklist.h" #include "staff.h" //职工信息存盘 voidsaveinformation(linklist head ); //职工信息加载 voidloadinformation(linklist head ); #endif // FILE_H_INCLUDED
file.cpp
#include#include #include #include "file.h" #include "linklist.h" #include "staff.h" voidsaveinformation(linklist h ) { FILE*fp; linklist p; if( (fp = fopen("stu.txt","w") ) == NULL) { printf("Failure to open stu.txt! "); exit(0); } for( p = h->next; p; p=p->next ) { fwrite( &(p->Staff), sizeof(node), 1, fp); } fclose(fp); } voidloadinformation( linklist h ) { FILE*fp; staff nodeBuffer; if((fp = fopen("stu.txt","r")) == NULL) { printf(" 数据文件丢失或为首次运行, 将加载测试数据 "); return; } while( fread(&nodeBuffer, sizeof(node), 1, fp)!=0 ) { insertnode(h, nodeBuffer); } }
4.主函数:mainmeun.cpp
#include#include #include "linklist.h" #include "staff.h" #include "file.h" voidmainmeun(linklist head); voidsearchmenu(linklist head); int main(void) { linklist head=NULL; //int n; //printf("------请输入你要存的数据---------- "); //scanf("%d", &n); head = Createlinklist(); system("cls"); //Displaylinklist(head); mainmeun(head); printf(" "); //loadinformation(head); //saveinformation(head); return0; } voidmainmeun(linklist head) { linklist p; charID[10]; //char name[10]; staff Staff; intselection; intflag = 1; do { printf("=================职工管理系统=================== "); printf("==========1.链表输出=====2.数据查询===== "); printf("=======3.数据删除===4.数据修改=====5.添加数据====== "); printf("=======6.链表销毁===7.信息存盘=====8.放弃存盘===== "); printf("================================================== "); printf("======请选择功能(1~8):"); scanf("%d", &selection); switch(selection) { case1: Displaylinklist(head); break; case2: searchmenu(head); break; case3: printf("=========请输入工号========== "); scanf("%s", ID); delenode(head, ID); break; case4: printf("=========请输入工号========== "); 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(linklist head) { linklist p; intflag = 1; charID[10]; charname[10]; do { printf("=========查找菜单=========== "); printf("===1.ID======2.name====3.退出==== "); printf("================================= "); intselection; printf("==请选择功能(1~3):"); scanf("%d", &selection); switch(selection) { case1: printf("=====请输入ID======= "); scanf("%s", ID); p = searchnode(head, ID); Displaystaff(p->Staff); break; case2: printf("=====请输入name====== "); scanf("%s", name); searchnodebyname(head, name); break; case3: flag = 0; break; } system("pause"); system("cls"); }while(flag == 1); }
审核编辑:汤梓红
评论
查看更多