// 闲来无事,写几行代码,温习一下单链表,功能不全,可补充。
/////////////////////////////////////////////////// /*Person Information Management System-PIMY *功能菜单///////////////////////////////////////// *1. 添加////////////////////////////////////////// *2. 删除////////////////////////////////////////// * 2.1按名字删除////////////////////////////////// * 2.2按id删除//////////////////////////////////// *3. 查找////////////////////////////////////////// * 3.1所有人////////////////////////////////////// * 3.2按名字查找////////////////////////////////// * 3.3按id查找//////////////////////////////////// *4. 退出////////////////////////////////////////// *////////////////////////////////////////////////// //************************************************* /////////////////////////////////////////////////// ////////////////Author: Mr Chen//////////////////// /////////////////////////////////////////////////// #include <stdio.h> #include <string.h> #include <stdlib.h> /////////////////////////////////////////////////// //the struct of person typedef struct person { char name[50]; int age; char id[20]; person *next; } node,personList; //Menu():design of the PIMY menu void mainMenu() { printf("\n"); printf("\t\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n"); printf("\t\t┃ 人事信息管理系统 ┃\n"); printf("\t\t┃﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌┃\n"); printf("\t\t┃ ◇Menu◇ ┃\n"); printf("\t\t┃ 1........录入 2........查找 ┃\n"); printf("\t\t┃ 3........删除 4........退出 ┃\n"); printf("\t\t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n"); } //getCommand():接受用户操作指令 void searchMenu() { printf("\n"); printf("\t\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n"); printf("\t\t┃ 人事信息管理系统 ┃\n"); printf("\t\t┃﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌┃\n"); printf("\t\t┃ ◇Search-Menu◇ ┃\n"); printf("\t\t┃ 1........全部 2........名字 ┃\n"); printf("\t\t┃ 3........ID 4........退出 ┃\n"); printf("\t\t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n"); } void delMenu() { printf("\n"); printf("\t\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n"); printf("\t\t┃ 人事信息管理系统 ┃\n"); printf("\t\t┃﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌┃\n"); printf("\t\t┃ ◇Delete-Menu◇ ┃\n"); printf("\t\t┃ 1........全部 2........名字 ┃\n"); printf("\t\t┃ 3........ID 4........退出 ┃\n"); printf("\t\t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n"); } int getCommand() { int c; printf("输入你的操作指令:"); scanf("%d",&c); getchar(); return c; } //create() node *create() { node *head; head = (node *)malloc(sizeof(node)); head->next = NULL; return head; } //addPerson() node *addPerson(node *head) { node *p; int i = 0; int num; char buf[256]; printf("需要录入的人数:"); scanf("%d",&num); getchar(); if(num<1) { printf("输入错误,人数必须大于0\n"); } else{ while(num-->0) { p = (node *)malloc(sizeof(node)); printf("请出入第%d个人的信息:\n",(i++)+1); gets(buf); if(sscanf(buf,"%s%d%s",&p->name,&p->age,&p->id) !=3) { printf("There is a mistake\n"); } else { p->next = head->next; head->next = p; } } printf("录入完毕!\n"); } return head; } //showAllPerson():show all persons` information void showAllPerson(node *head) { node *p; printf("姓名\t年龄\tID\n"); p = head->next; while(p!=NULL) { printf("%s\t%d\t%s\n",p->name,p->age,p->id); p = p->next; } } //searchName():根据名字查找个人信息 void searchName(node *head) { node *p; char name[50]; int i = 0; printf("请输入名字:"); scanf("%s",&name); printf("姓名\t年龄\tID\n"); p = head->next; while(p!=NULL) { if(strcmp(p->name,name)==0) { printf("%s\t%d\t%s\n",p->name,p->age,p->id); // p = p->next; i++; } p = p->next; } if(i==0) { printf("查无此人!\n"); } } // void searchId(node *head) { node *p; char id[20]; int i = 0; printf("请输入ID:"); scanf("%s",&id); printf("姓名\t年龄\tID\n"); p = head->next; while(p!=NULL) { if(strcmp(p->id,id)==0) { printf("%s\t%d\t%s\n",p->name,p->age,p->id); i++; } p = p->next; } if(i==0) { printf("查无此人!\n"); } } //search():一级菜单查找 void search(node *head) { if(head->next ==NULL) { printf("系统信息为空,查找失败!\n"); } else { searchMenu(); int c = getCommand(); switch(c) { case 1: showAllPerson(head); break; case 2: searchName(head); break; case 3: searchId(head); break; default: printf("输入有误!\n"); break; } } } //deleteName():按名字删除 node *deleteName(node *head) { node *p,*q; char name[50]; p = head; q = head->next; printf("请输入名字:"); scanf("%s",&name); while(q&&(strcmp(q->name,name)!=0)) { p = q; q = q->next; } if(q) { if(p==head) { head->next = q->next; } else { p->next = q->next; } // delete q; free(q); printf("删除完毕!\n"); } else { printf("系统中没有此人!\n"); } return head; } //deleteId():按ID删除 node *deleteId(node *head) { node *p,*q; char id[20]; p = head; q = head->next; printf("请输入ID:"); scanf("%s",&id); while(q&&(strcmp(q->id,id)!=0)) { p = q; q = q->next; } if(q) { if(p==head) { head->next = q->next; } else { p->next = q->next; } // delete q; free(q); printf("删除完毕!\n"); } else { printf("系统中没有此人!\n"); } return head; } node *delAllPerson(node *head) { node *p,*q; p = head->next; while(p!=NULL) { q = p; p = p->next; free(q); } head->next = NULL; printf("删除完毕!\n"); return head; } //delete():一级菜单 node *delPerson(node *head) { if(head->next==NULL) { printf("系统信息为空,删除失败!\n"); } else { delMenu(); int c = getCommand(); switch(c) { case 1: head = delAllPerson(head); break; case 2: head = deleteName(head); break; case 3: head = deleteId(head); break; default: printf("输入有误!\n"); break; } } return head; } ////////////////////////////////////////////////// // int main() { int c; node *head = create(); while(1) { mainMenu(); c = getCommand(); switch(c) { case 1: head = addPerson(head); break; case 2: search(head); break; case 3: head = delPerson(head); break; case 4: printf("你已退出人事信息管理系统!\n"); exit(0); default: printf("输入错误!\n"); break; } } return 0; }
运行结果:
时间: 2024-10-26 01:10:46