以前先了几天,现在发现只需要半天就能写完,还不错,尽管很简单,
#include <iostream>
#include<cstdio>
#include<math.h>
#include<cstring>
#include<algorithm>
using namespace std;
void menu_main()
{
printf("-----------------主页面-------------------\n");
printf(" ←←←←←←←←←←←←←←←←←←←←\n");
printf("↓ 1.新建 2.查找 ↑\n");
printf("↓ 3.插入 4.删除 ↑\n");
printf("↓ 5.显示 6.排序 ↑\n");
printf("↓ 7.工资 0.退出 ↑\n");
printf(" →→→→→→→→→→→→→→→→→→→→\n");
}
void menu_search()
{
printf(" ---------------查找--------------\n");
printf(" ←←←←←←←←←←←←←←←←\n");
printf("↓ ↑\n");
printf("↓ 1.姓名查找 ↑\n");
printf("↓ 2.职工号查找 ↑\n");
printf("↓ ↑\n");
printf(" →→→→→→→→→→→→→→→→\n");
}
void menu_sort()
{
printf(" ---------------排序--------------\n");
printf(" ←←←←←←←←←←←←←←←←\n");
printf("↓ ↑\n");
printf("↓ 1.工资排序 ↑\n");
printf("↓ 2.职工号排序 ↑\n");
printf("↓ ↑\n");
printf(" →→→→→→→→→→→→→→→→\n");
}
typedef struct workers
{
char name[15];//姓名
char department[18];//单位
char gender;//性别
unsigned int age;//年龄
unsigned long telephone;//电话
unsigned long wage;//工资
unsigned long num;//职工号
struct workers *next;
} workers,*Workers;
Workers create()
{
//创建员工信息
int count=1;
Workers h,p,q;
h=(Workers)malloc(sizeof(workers));
h->next=NULL;
p=h;
q=(Workers)malloc(sizeof(workers));
while(1)
{
printf("请输入第%d个员工的信息\n",count);
count++;
printf("员工的职工号(输入信息以0结尾):");
cin>>q->num;
if(q->num==0)
break;
printf("员工的姓名:");
cin>>q->name;
printf("员工的性别(f/m):");
cin>>q->gender;
printf("员工的单位:");
cin>>q->department;
printf("员工的电话号码:");
cin>>q->telephone;
printf("员工的工资:");
cin>>q->wage;
p->next=q;
p=q;
q=(Workers)malloc(sizeof(workers));
}
free(q);
p->next=NULL;
return h;
}
void show_inf(Workers h)
{
//显示员工信息
Workers p=h->next;
printf("------------------------------------------------------------------\n");
printf("职工号\t姓名\t性别\t单位\t\t电话\t\t工资\n");
printf("------------------------------------------------------------------\n");
while(p!=NULL)
{
printf("%lu\t%s\t%c\t%s\t\t%lu\t\t%lu\n",p->num,p->name,p->gender,p->department,p->telephone,p->wage);
p=p->next;
}
printf("------------------------------------------------------------------\n");
}
void insert_inf(Workers h)
{
//插入
int count=1;
Workers q,p=h;
while(p->next!=NULL)
{
p=p->next;
}
q=(Workers)malloc(sizeof(Workers));
while(1)
{
printf("请输入插入的第%d个员工的信息\n",count);
count++;
printf("员工的职工号(输入信息以0结尾):");
cin>>q->num;
if(q->num==0)
break;
printf("员工的姓名:");
cin>>q->name;
printf("员工的性别(f/m):");
cin>>q->gender;
printf("员工的单位:");
cin>>q->department;
printf("员工的电话号码:");
cin>>q->telephone;
printf("员工的工资:");
cin>>q->wage;
p->next=q;
p=q;
q=(Workers)malloc(sizeof(workers));
}
free(q);
p->next=NULL;
}
void search_num(Workers h)
{
//按职工的职工号查询
Workers p,q;
p=h->next;
unsigned long num;
printf("请输入要查找的员工的职工号:");
cin>>num;
while(p!=NULL)
{
if(p->num==num)
{
printf("查找成功:\n");
printf("------------------------------------------------------------------\n");
printf("职工号\t姓名\t性别\t单位\t\t电话\t\t工资\n");
printf("------------------------------------------------------------------\n");
printf("%lu\t%s\t%c\t%s\t\t%lu\t\t%lu\n",p->num,p->name,p->gender,p->department,p->telephone,p->wage);
printf("------------------------------------------------------------------\n");
break;
}
p=p->next;
}
if(p==NULL)
printf("查找失败:\n");
}
void search_name(Workers h)
{
//按姓名查找
Workers p,q;
p=h->next;
char name[15];
printf("请输入要查找的员工的姓名:");
cin>>name;
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
printf("查找成功:\n");
printf("------------------------------------------------------------------\n");
printf("职工号\t姓名\t性别\t单位\t\t电话\t\t工资\n");
printf("------------------------------------------------------------------\n");
printf("%lu\t%s\t%c\t%s\t\t%lu\t\t%lu\n",p->num,p->name,p->gender,p->department,p->telephone,p->wage);
printf("------------------------------------------------------------------\n");
break;
}
p=p->next;
}
if(p==NULL)
printf("查找失败:\n");
}
void updata_inf(Workers h)
{
//更新员工信息
unsigned long num;
int flag;
printf("请输入需要更新的员工的职工号:");
cin>>num;
Workers p=h->next;
while(p!=NULL)
{
if(p->num==num)
{
printf("查询到此人,该人原信息是:\n");
printf("------------------------------------------------------------------\n");
printf("职工号\t姓名\t性别\t单位\t\t电话\t\t工资\n");
printf("------------------------------------------------------------------\n");
printf("%lu\t%s\t%c\t%s\t\t%lu\t\t%lu\n",p->num,p->name,p->gender,p->department,p->telephone,p->wage);
printf("------------------------------------------------------------------\n");
printf("是否更新?(1代表是,2代表否)\n");
cin>>flag;
if(flag)
{
printf("请更新该员工的信息");
printf("员工的职工号:");
cin>>p->num;
printf("员工的姓名:");
cin>>p->name;
printf("员工的性别(f/m):");
cin>>p->gender;
printf("员工的单位:");
cin>>p->department;
printf("员工的电话号码:");
cin>>p->telephone;
printf("员工的工资:");
cin>>p->wage;
printf("修改后的信息:\n");
printf("------------------------------------------------------------------\n");
printf("职工号\t姓名\t性别\t单位\t\t电话\t\t工资\n");
printf("------------------------------------------------------------------\n");
printf("%lu\t%s\t%c\t%s\t\t%lu\t\t%lu\n",p->num,p->name,p->gender,p->department,p->telephone,p->wage);
printf("------------------------------------------------------------------\n");
}
break;
}
p=p->next;
}
if(p==NULL)
printf("查无此人,修改失败\n");
}
void delete_inf(Workers h)
{
//删除信息
Workers p,q;
int flag;
q=h;
p=h->next;
unsigned long num;
printf("输入需要删除的员工的职工号:");
cin>>num;
while(p!=NULL)
{
if(p->num==num)
{
printf("查找成功,该人信息如下:\n");
printf("------------------------------------------------------------------\n");
printf("职工号\t姓名\t性别\t单位\t\t电话\t\t工资\n");
printf("------------------------------------------------------------------\n");
printf("%lu\t%s\t%c\t%s\t\t%lu\t\t%lu\n",p->num,p->name,p->gender,p->department,p->telephone,p->wage);
printf("------------------------------------------------------------------\n");
printf("是否删除?(1代表是,2代表否)\n");
cin>>flag;
if(flag)
{
q->next=p->next;
free(p);
}
break;
}
p=p->next;
q=q->next;
}
if(p==NULL)
printf("查无此人,删除失败\n");
}
void swap_inf(Workers a,Workers b)
{
//交换两个节点的信息
unsigned long x;
x=a->num;
a->num=b->num;
b->num=x;
char y[15];
strcpy(y,a->name);
strcpy(a->name,b->name);
strcpy(b->name,y);
char z;
z=a->gender;
a->gender=b->gender;
b->gender=z;
char d[18];
strcpy(d,a->department);
strcpy(a->department,b->department);
strcpy(b->department,d);
unsigned long t;
t=a->telephone;
a->telephone=b->telephone;
b->telephone=t;
unsigned long w;
t=a->wage;
a->wage=b->wage;
b->wage=t;
}
void sort_num(Workers h)
{
//按职工号排序
Workers p,q,r,t;
r=h;
q=h->next;
p=h->next->next;
int count=0;
while(p!=NULL)
{
count++;
p=p->next;
}
for(int i=1; i<count; i++)
{
r=h;
q=h->next;
p=h->next->next;
for(int j=1; j<=count-i; j++)
{
if(q->num>p->num)
{
swap_inf(p,q);
}
r=r->next;
p=p->next;
q=q->next;
}
}
}
void sort_wage(Workers h)
{
//按工资排序
Workers p,q,r,t;
r=h;
q=h->next;
p=h->next->next;
int count=0;
while(p!=NULL)
{
count++;
p=p->next;
}
for(int i=1; i<count; i++)
{
r=h;
q=h->next;
p=h->next->next;
for(int j=1; j<=count-i; j++)
{
if(q->wage>p->wage)
{
swap_inf(p,q);
}
r=r->next;
p=p->next;
q=q->next;
}
}
}
void show_wage(Workers h)
{
Workers p,q;
double ave;
unsigned long ans=0;
int count=0;
p=h->next;
printf("---------------------------------------------------\n");
printf("姓名\t\t\t\t工资\n");
printf("---------------------------------------------------\n");
while(p!=NULL)
{
printf("%s\t\t\t\t%lu\n",p->name,p->wage);
ans+=p->wage;
count++;
p=p->next;
}
ave=ans*1.0/count;
printf("---------------------------------------------------\n");
printf(" 总计:\n");
printf(" 人数:%d 平均工资:%.2lf\n",count,ave);
}
int main()
{
Workers h;
int n;
int n_main;
int n_search;
int n_sort;
menu_main();
while(~scanf("%d",&n_main)&&n_main)
{
switch(n_main)
{
case 1:
h=create();
printf("→→→→→→→→创建成功→→→→→→→\n");
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
cout<<endl;
cout<<endl;
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
printf("按任意键返回主页……………………\n");
getchar();
getchar();
cout<<endl;
menu_main();
break;
case 2:
menu_search();
cin>>n_search;
switch(n_search)
{
case 1:
search_name(h);
printf("→→→→→→→→查找成功→→→→→→→\n");
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
cout<<endl;
cout<<endl;
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
printf("按任意键返回主页……………………\n");
getchar();
getchar();
cout<<endl;
menu_main();
break;
case 2:
search_num(h);
printf("→→→→→→→→查找成功→→→→→→→\n");
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
cout<<endl;
cout<<endl;
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
printf("按任意键返回主页……………………\n");
getchar();
getchar();
cout<<endl;
menu_main();
break;
}
break;
case 3:
insert_inf(h);
printf("→→→→→→→→插入成功→→→→→→→\n");
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
cout<<endl;
cout<<endl;
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
printf("按任意键返回主页……………………\n");
getchar();
getchar();
cout<<endl;
menu_main();
break;
case 4:
delete_inf(h);
menu_main();
break;
case 5:
show_inf(h);
printf("→→→→→→→→显示完毕→→→→→→→\n");
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
cout<<endl;
cout<<endl;
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
printf("按任意键返回主页……………………\n");
getchar();
getchar();
cout<<endl;
menu_main();
break;
case 6:
menu_sort();
cin>>n_sort;
switch(n_sort)
{
case 1:
sort_wage(h);
printf("→→→→→→→→→→→排序成功,结果如下\n");
show_inf(h);
printf("→→→→→→→→排序完成→→→→→→→\n");
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
cout<<endl;
cout<<endl;
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
printf("按任意键返回主页……………………\n");
getchar();
getchar();
cout<<endl;
menu_main();
break;
case 2:
sort_num(h);
printf("→→→→→→→→→→→排序成功,结果如下\n");
show_inf(h);
printf("→→→→→→→→排序完成→→→→→→→\n");
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
cout<<endl;
cout<<endl;
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
printf("按任意键返回主页……………………\n");
getchar();
getchar();
cout<<endl;
menu_main();
break;
}
break;
case 7:
show_wage(h);
printf("→→→→→→→→查询完成→→→→→→→\n");
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
cout<<endl;
cout<<endl;
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
printf("按任意键返回主页……………………\n");
getchar();
getchar();
cout<<endl;
menu_main();
break;
}
}
}