#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
struct Build{
int Number,Phone,Age,Wage;
char Name[20],Depart[20],Gender[2];
struct Build *next;
};
typedef struct Build Bstaff;
Bstaff *Staff=(Bstaff *)malloc(sizeof(Bstaff));
void output(Bstaff *h){
Bstaff *p;
p=h->next;
printf("目前储存有以下员工信息!\n==================\n");
while(1){
printf("Name: %s\n--------\n",p->Name);
if(p->next==NULL){break;}
p=p->next;
}
printf("OK!\n==================\n");
}
void oput(Bstaff *p){
printf("===================================\n");
printf("该员工信息为:\n");
printf("Name: %s\n",p->Name);printf("Age: %d\n",p->Age);printf("Gender: %s\n",p->Gender);
printf("Wage: %d\n",p->Wage);printf("Number: %d\n",p->Number);printf("Phone: %d\n",p->Phone);
printf("Department: %s\n",p->Depart);
printf("===================================\n");
}
Bstaff *Add(int n,Bstaff *h){
if(h==NULL){printf("空间已满!");return NULL;}
Bstaff *p,*q;
q=h;
printf("请录入信息:\n");
while(n--){
p=(Bstaff *)malloc(sizeof(Bstaff));
if(p==NULL){printf("Room is not enough!");return NULL;}
printf("请输入姓名:");scanf("%s",p->Name);
printf("Age:");scanf("%d",&p->Age);
printf("Wage:");scanf("%d",&p->Wage);
printf("Number:");scanf("%d",&p->Number);
printf("Phone:");scanf("%d",&p->Phone);
printf("Gender:");scanf("%s",p->Gender);
printf("Department:");scanf("%s",p->Depart);
q->next=p;
q=p;
}
printf("-----------------------------------\nSuccess!\n");
printf("-----------------------------------\n");
return q;
}
void C1(){
int n;
printf("Please Input The Number of Add People: ");scanf("%d",&n);
Bstaff *h=(Bstaff *)malloc(sizeof(Bstaff));
Bstaff *tmp=Add(n,h);
if(tmp==NULL){printf("Room Error!\n----------------\n");return ;}
tmp->next=Staff->next;
Staff->next=h->next;
free(h);
}
void Delete(int c,Bstaff *h){
int cc;char ss[20];
if(h==NULL){printf("Error!Header is NULL!\n");return ;}
Bstaff *p,*q;
p=h->next;q=h;
if(c<5&&c>0){ printf("请输入删除人员的关键信息:");scanf("%d",&cc);}
else if(c>4&&c<8){ printf("请输入删除人员的关键信息:");scanf("%s",ss);}
else {printf("指令错误!请重新输入!\n");return ;}
while(p!=NULL)
{
if(c==1){if(p->Age==cc){q->next=p->next;free(p);}else q=p;}
if(c==2){if(p->Wage==cc){q->next=p->next;free(p);}else q=p;}
if(c==3){if(p->Number==cc){q->next=p->next;free(p);}else q=p;}
if(c==4){if(p->Phone==cc){q->next=p->next;free(p);}else q=p;}
if(c==5){if(strcmp(p->Name,ss)==0){q->next=p->next;free(p);}else q=p;}
if(c==6){if(strcmp(p->Gender,ss)==0){q->next=p->next;free(p);}else q=p;}
if(c==7){if(strcmp(p->Depart,ss)==0){q->next=p->next;free(p);}else q=p;}
p=p->next;
}
printf("------------------\nOver!\n-----------------\n");
}
void C2(){
printf("请输入删除的关键信息!\n");
printf("1: Age 2:Wage\n");printf("3: Number 4:Phone\n");
printf("5: Name 6:Gender\n");printf("7: Deparment\n");
int c;scanf("%d",&c);
Delete(c,Staff);
}
void change(int c,Bstaff *p){
int cc;char ss[20];
if(c<5&&c>0){ printf("请输入修改后的关键信息:");scanf("%d",&cc);}
else if(c>4&&c<8){ printf("请输入修改后的关键信息:");scanf("%s",ss);}
else {printf("指令错误!请重新输入!\n");return ;}
if(c==1){p->Age=cc;oput(p);}
if(c==2){p->Wage=cc;oput(p);}
if(c==3){p->Number=cc;oput(p);}
if(c==4){p->Phone=cc;oput(p);}
if(c==5){strcpy(p->Name,ss);oput(p);}
if(c==6){strcpy(p->Gender,ss);oput(p);}
if(c==7){strcpy(p->Depart,ss);oput(p);}
printf("------------------\nOver!\n-----------------\n");
}
void C3_1(Bstaff *p){
printf("请输入要修改的关键信息!\n");
printf("1: Age 2:Wage\n");printf("3: Number 4:Phone\n");
printf("5: Name 6:Gender\n");printf("7: Deparment\n");
int c;scanf("%d",&c);
change(c,p);
}
void Update(int c,Bstaff *h){
int cc;char ss[20];
if(h==NULL){printf("Error!Header is NULL!\n");return ;}
Bstaff *p,*q;
p=h->next;q=h;
if(c<5&&c>0){ printf("请输入插找更新人员的关键信息:");scanf("%d",&cc);}
else if(c>4&&c<8){ printf("请输入插找更新人员的关键信息:");scanf("%s",ss);}
else {printf("指令错误!请重新输入!\n");return ;}
while(p!=NULL)
{
if(c==1){if(p->Age==cc){oput(p);C3_1(p); }else q=p;}
if(c==2){if(p->Wage==cc){oput(p);C3_1(p);}else q=p;}
if(c==3){if(p->Number==cc){oput(p);C3_1(p);}else q=p;}
if(c==4){if(p->Phone==cc){oput(p);C3_1(p);}else q=p;}
if(c==5){if(strcmp(p->Name,ss)==0){oput(p);C3_1(p);}else q=p;}
if(c==6){if(strcmp(p->Gender,ss)==0){oput(p);C3_1(p);}else q=p;}
if(c==7){if(strcmp(p->Depart,ss)==0){oput(p);C3_1(p);}else q=p;}
p=p->next;
}
}
void C3(){
printf("请选择查找修改人员的关键信息!\n");
printf("1: Age 2:Wage\n");printf("3: Number 4:Phone\n");
printf("5: Name 6:Gender\n");printf("7: Deparment\n");
int c;scanf("%d",&c);
Update(c,Staff);
}
void Search(int c,Bstaff *h){
int cc;char ss[20];
if(h==NULL){printf("Error!Header is NULL!\n");return ;}
Bstaff *p,*q;
p=h->next;q=h;
if(c<5&&c>0){ printf("请输入查找人员的关键信息:");scanf("%d",&cc);}
else if(c>4&&c<8){ printf("请输入查找人员的关键信息:");scanf("%s",ss);}
else {printf("指令错误!请重新输入!\n");return ;}
while(p!=NULL)
{
if(c==1){if(p->Age==cc){oput(p);}else q=p;}
if(c==2){if(p->Wage==cc){oput(p);}else q=p;}
if(c==3){if(p->Number==cc){oput(p);}else q=p;}
if(c==4){if(p->Phone==cc){oput(p);}else q=p;}
if(c==5){if(strcmp(p->Name,ss)==0){oput(p);}else q=p;}
if(c==6){if(strcmp(p->Gender,ss)==0){oput(p);}else q=p;}
if(c==7){if(strcmp(p->Depart,ss)==0){oput(p);}else q=p;}
p=p->next;
}
printf("------------------\nSearch Over!\n-----------------\n");
}
void C4(){
printf("请选择查找人员的关键信息!\n");
printf("1: Age 2:Wage\n");printf("3: Number 4:Phone\n");
printf("5: Name 6:Gender\n");printf("7: Deparment\n");
int c;scanf("%d",&c);
Search(c,Staff);
}
void Trans(Bstaff *a,Bstaff *b){
a->Age=b->Age;
a->Wage=b->Wage;a->Number=b->Number;a->Phone=b->Phone;
strcpy(a->Name,b->Name);strcpy(a->Gender,b->Gender);strcpy(a->Depart,b->Depart);
}
void Swap(Bstaff *a,Bstaff *b){
Bstaff *tmp=(Bstaff*)malloc(sizeof(Bstaff));
if(tmp==NULL){printf("Error!\n");return ;}
Trans(tmp,a);Trans(a,b);Trans(b,tmp);
}
void SelectSort1(Bstaff *h){
if(h==NULL){printf("Linklist is NULL!Error!\n");return ;}
Bstaff *p,*q,*Min;
for(p=h->next;p!=NULL;p=p->next)
{
Min=p;
for(q=p->next;q!=NULL;q=q->next)
{
if(q->Age<Min->Age)
Min=q;
}
Swap(Min,p);
}
}
void SelectSort2(Bstaff *h){
if(h==NULL){printf("Linklist is NULL!Error!\n");return ;}
Bstaff *p,*q,*Min;
for(p=h->next;p!=NULL;p=p->next)
{
Min=p;
for(q=p->next;q!=NULL;q=q->next)
{
if(q->Wage<Min->Wage)
Min=q;
}
Swap(Min,p);
}
}
void SelectSort3(Bstaff *h){
if(h==NULL){printf("Linklist is NULL!Error!\n");return ;}
Bstaff *p,*q,*Min;
for(p=h->next;p!=NULL;p=p->next)
{
Min=p;
for(q=p->next;q!=NULL;q=q->next)
{
if(q->Number<Min->Number)
Min=q;
}
Swap(Min,p);
}
}
void SelectSort4(Bstaff *h){
if(h==NULL){printf("Linklist is NULL!Error!\n");return ;}
Bstaff *p,*q,*Min;
for(p=h->next;p!=NULL;p=p->next)
{
Min=p;
for(q=p->next;q!=NULL;q=q->next)
{
if(q->Phone<Min->Phone)
Min=q;
}
Swap(Min,p);
}
}
void SelectSort5(Bstaff *h){
if(h==NULL){printf("Linklist is NULL!Error!\n");return ;}
Bstaff *p,*q,*Min;
for(p=h->next;p!=NULL;p=p->next)
{
Min=p;
for(q=p->next;q!=NULL;q=q->next)
{
if(strcmp(q->Name,Min->Name)<0)
Min=q;
}
Swap(Min,p);
}
}
void SelectSort6(Bstaff *h){
if(h==NULL){printf("Linklist is NULL!Error!\n");return ;}
Bstaff *p,*q,*Min;
for(p=h->next;p!=NULL;p=p->next)
{
Min=p;
for(q=p->next;q!=NULL;q=q->next)
{
if(strcmp(q->Gender,Min->Gender)<0)
Min=q;
}
Swap(Min,p);
}
}
void SelectSort7(Bstaff *h){
if(h==NULL){printf("Linklist is NULL!Error!\n");return ;}
Bstaff *p,*q,*Min;
for(p=h->next;p!=NULL;p=p->next)
{
Min=p;
for(q=p->next;q!=NULL;q=q->next)
{
if(strcmp(q->Depart,Min->Depart)<0)
Min=q;
}
Swap(Min,p);
}
}
void Sort(int c,Bstaff *h){
if(h==NULL){printf("Error!Header is NULL!\n");return ;}
if(c>7)printf("输入出错!\n");
if(c==1){SelectSort1(h);}
if(c==2){SelectSort2(h);}
if(c==3){SelectSort3(h);}
if(c==4){SelectSort4(h);}
if(c==5){SelectSort5(h);}
if(c==6){SelectSort6(h);}
if(c==7){SelectSort7(h);}
output(Staff);
}
void C5(){
printf("请选择排序的关键信息!\n");
printf("1: Age 2:Wage\n");printf("3: Number 4:Phone\n");
printf("5: Name 6:Gender\n");printf("7: Deparment\n");
int c;scanf("%d",&c);
Sort(c,Staff);
}
int main()
{
int c;
while(1){
printf("==============================\n请输入操作指令:\n");
printf("1:添加员工 2:删除员工\n");
printf("3:修改员工信息 4: 查询员工信息\n");
printf("5:对员工信息进行排序!\n6:退出!\n==============================\n");
scanf("%d",&c);
system("CLS");
switch(c)
{
case 1:C1();break;
case 2:C2();break;
case 3:C3();break;
case 4:C4();break;
case 5:C5();break;
case 6:break;
}
system("pause");
output(Staff);
if(c==6)break;
}
printf("================\nSee You Again!\n================\n");
return 0;
}
C(单链表)实现的员工管理系统
最新推荐文章于 2023-01-08 18:36:12 发布