做了一个企业员工信息管理,跟学生信息管理类似,用的单链表实现的,话不多说直接放源码
放两张图片看看效果
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<string>
using namespace std;
typedef struct per {
char xm[30]; //姓名
char ph[20]; //手机号
char xh[20]; //工号
char sex[10]; //性别
int age; //年龄
char bm[30]; //部门
struct per *next;
}stu;
typedef struct per *sink;
void caidan(); //菜单打印
void income(sink head); //初始化员工信息
void insert(sink head, int m); //插入员工信息
void del(sink head, int m); //删除员工信息
void fin(sink head, char s[20]); //查询员工信息
void xiugai(sink head, char s[20]); //修改员工信息
void inout(sink head); //输出所有员工信息
void caidan() {
cout<<"**********************************************************************" << endl;
cout<<endl;
cout<< "* 欢迎使用企业员工管理系统 *" << endl;
cout<< endl;
cout<< "**********************************************************************" << endl;
cout<< endl;
cout<< "*============================主菜单==================================*" << endl;
cout<< endl;
cout<< "******* 1.初始化员工信息 2.添加员工信息 *******" << endl;
cout<< endl;
cout<< "******* 3.删除员工信息 4.查询员工信息 *******" << endl;
cout<< endl;
cout<< "******* 5.修改员工信息 6.输出所有员工信息 *******" << endl;
cout<< endl;
cout<< "******* 按其他键退出........ *******" << endl;
cout<<endl;
}
void income(sink head) { //用来初始化员工信息
system("cls");
sink p,r=head;
int n;
cout<<"员工信息人数:"<<endl;
cin>>n;
cout<< endl;
cout<<"员工信息:"<<endl;
cout<<"工号 姓名 性别 年龄 手机号 部门" << endl;
for (int i=1;i<=n;i++) {
p = (sink)malloc(sizeof(stu));
p->next=NULL;
cin>>p->xh;
cin>>p->xm;
cin>>p->sex;
cin>>p->age;
cin>>p->ph;
cin>>p->bm;
r->next=p;
r=p;
}
r->next=NULL;
cout<<"初始化完成!"<<endl;
system("pause");
}
void insert(sink head,int m) { //插入员工信息(尾插法)
sink p,pre=head;
int j=0;
while(pre&&j<m-1){
pre=pre->next;
j++;
}
if (!pre||m<1){
cout<<"没有这个位置!"<<endl;
cout<<endl;
return ;
}
p=(sink)malloc(sizeof(stu));
cout<<"输入要插入的信息:"<<endl;
cout<<"工号:"<<endl;
cin>>p->xh;
cout<<endl;
cout<<"姓名:"<<endl;
cin>>p->xm;
cout<<endl;
cout<<"性别:"<<endl;
cin>>p->sex;
cout<<endl;
cout<<"年龄:"<<endl;
cin>>p->age;
cout<<endl;
cout<<"手机号:"<<endl;
cin>>p->ph;
cout<<endl;
cout<<"部门:"<<endl;
cin>>p->bm;
cout<<endl;
p->next=pre->next;
pre->next=p;
cout<<"插入成功!"<<endl;
system("pause");
}
void del(sink head,char s[20]) { //删除员工信息
sink p,pre=head,op;
p=pre;
while(p->next!=NULL){
op=p;
p=p->next;
if(strcmp(p->xh,s)==0)
break;
}
op->next=p->next;
cout<<"删除成功!"<<endl;
system("pause");
}
void fin(sink head,char s[20]) { //查找员工信息
sink pre=head,p;
p=(sink)malloc(sizeof(stu));
p=pre;
int f=0;
while (p->next!=NULL) {
p = p->next;
if (strcmp(p->xm,s)==0){
cout<<"查询的信息:"<<endl; f=1;
cout<<p->xh<<" "<<p->xm<<" "<<p->sex<<" "<<p->age<<" "<<p->ph<<" "<<p->bm<<endl;
cout<<endl;
}
}
if(f) cout<<"查询成功!"<<endl;
else cout<<"没有此人!"<<endl;
system("pause");
}
void xiugai(sink head, char s[20]) { //修改员工信息
sink pre = head, p,op,po;
p=(sink)malloc(sizeof(stu));
op=(sink)malloc(sizeof(stu));
p=pre;po=NULL;
cout<<" **********要修改员工的哪个信息:**********"<<endl;
cout<<"**** 1.姓名 2.性别 ****"<<endl;
cout<<endl;
cout<<"**** 3.年龄 4.手机号 ****"<<endl;
cout<<endl;
cout<<"**** 5.部门 按任意键退出........ ****"<<endl;
cout<<endl;
int t; cin>>t;
while (p->next!=NULL) {
p = p->next;
if (strcmp(p->xh,s)==0) {
strcpy(op->xh,p->xh);strcpy(op->xm,p->xm);
strcpy(op->sex,p->sex);op->age=p->age;
strcpy(op->ph,p->ph);strcpy(op->bm,p->bm);
switch(t){
case 1:{
cout<<"输入要修改的姓名:"<<endl;
cin>>p->xm;break;
}
case 2:{
cout<<"输入要修改的性别:"<<endl;
cin>>p->sex;break;
}
case 3:{
cout<<"输入要修改的年龄:"<<endl;
cin>>p->age;break;
}
case 4:{
cout<<"输入要修改的手机号:"<<endl;
cin>>p->ph;break;
}
case 5:{
cout<<"输入要修改的部门:"<<endl;
cin>>p->bm;break;
}
default:return ;
}po=p;
}
}
cout<<"原信息:"<<endl;
cout<<op->xh<<" "<<op->xm<<" "<<op->sex<<" "<<op->age<<" "<<op->ph<<" "<<op->bm<<endl;
cout<<"修改后的信息:"<<endl;
cout<<po->xh<<" "<<po->xm<<" "<<po->sex<<" "<<po->age<<" "<<po->ph<<" "<<po->bm<<endl;
cout<<"修改成功!"<<endl;
system("pause");
}
void inout(sink head) { //输出所有员工信息
system("cls");
sink p=head;
cout<<"所有员工信息:"<<endl<<endl;
cout<<" ---------------------------------------------------------"<<endl;
cout<<" | 工号 | 姓名 | 性别 | 年龄 | 手机号 | 部门 |"<<endl;
cout<<" ---------------------------------------------------------"<<endl;
while (p->next!=NULL) {
p=p->next;
printf(" | %6s | %6s | %4s | %4d | %10s | %8s |\n",p->xh,p->xm,p->sex,p->age,p->ph,p->bm);
cout<<" ---------------------------------------------------------"<<endl;
}
system("pause");
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int flag=0;
sink head;
head=(sink)malloc(sizeof(stu));
int f=0;
while(1){
system("cls");
caidan();
if(!f) cout<<"请先初始化员工信息,再进行操作"<<endl;
if(flag){
cout<<"程序结束!";break;
}
int t; cin>>t;
switch(t){
case 1:{
f=1;
income(head);
break;
}
case 2:{
system("cls");
int m;
cout <<"要插入在第几个位置:"<<endl;
cin >>m;
cout <<endl;
insert(head, m);
break;
}
case 3:{
system("cls");
char s[20];
cout <<"要删除的员工的工号:"<<endl;
cin >>s;
cout <<endl;
del(head, s);
break;
}
case 4: {
system("cls");
char s[20];
cout <<"所要查询的员工的姓名:"<<endl;
cin >>s;
fin(head,s);
break;
}
case 5:{
system("cls");
char s[20];
cout <<"所要修改的员工的工号:"<<endl;
cin >>s; cout<<endl;
xiugai(head, s);
break;
}
case 6:{
inout(head);
break;
}
default :flag=1;
}
}
return 0;
}