这几天把c++基础学的差不多了,于是找了一个小项目来锻炼一下自己,做一个职工管理系统,可以实现增加、查询、删除、修改、排序、查看等功能。因为我是用链表做的,所以在排序的时候,遇到了一个小问题,就是循环链表的排序不知道如何做,查了一下资料,最后上网找到了一个解决的办法,感觉链表的插入、删除都比数组简单,但是说到排序,不得不承认链表比数组复杂的不是一点半点,一个链表的冒泡排序看了很久才看懂,说来惭愧啊。代码如下:
#ifndef _CONTROL_H_
#define _CONTROL_H_
#include <iostream>
#include <string>
#include<windows.h>
using namespace std;
typedef struct node
{
string num; //职工号
string name; //名字
int age; //年龄
string x; //性别 f为女 m为男
string yobi; //邮编
string bum; //部门
int salary; //工资
struct node *next;
}Node;
typedef struct node * PNode;
class Control
{
public:
void jiemian(); //界面显示
void zhuce(PNode head); //注册职工
void xiugai(PNode head); //修改职工信息
void shanchu(PNode head); //删除职工信息
void chaxun(PNode head); //查询职工信息
void paiming(PNode head); //根据薪资排名
void pint(PNode head); //打印所有信息
};
#endif
#include "control.h"
void Control::jiemian()
{
system("cls");
cout<<"\n\n\n";
cout<<"\t\t\t**************************************\n";
cout<<"\t\t\t* 欢迎来到职工管理系统 *\n";
cout<<"\t\t\t* 1 注册新职工 *\n";
cout<<"\t\t\t* 2 修改职工信息 *\n";
cout<<"\t\t\t* 3 删除职工信息 *\n";
cout<<"\t\t\t* 4 查询职工信息 *\n";
cout<<"\t\t\t* 5 薪资排名顺序 *\n";
cout<<"\t\t\t* 6 浏览所有信息 *\n";
cout<<"\t\t\t**************************************\n";
cout<<"\n\n\t\t请输入您的选择:";
}
void Control::zhuce(PNode head)
{
PNode p = new Node;
system("cls");
cout<<"\n\n\n";
cout<<"\t\t\t请输入职工号:";
cin>>p->num;
cout<<"\n\t\t\t请输入名字:";
cin>>p->name;
cout<<"\n\t\t\t请输入年龄:";
cin>>p->age;
cout<<"\n\t\t\t请输入性别:";
cin>>p->x;
cout<<"\n\t\t\t请输入邮编:";
cin>>p->yobi;
cout<<"\n\t\t\t请输入部门:";
cin>>p->bum;
cout<<"\n\t\t\t请输入工资:";
cin>>p->salary;
p->next = head->next;
head->next = p;
}
void Control::xiugai(PNode head)
{
string str1;
int flag = 0;
system("cls");
cout<<"\n\n\n";
cout<<"\t\t\t请输入要修改人的职工号:";
cin>>str1;
PNode temp = head->next;
while(temp != head)
{
if(temp->num == str1)
{
flag = 1;
cout<<"\n\t\t\t请输入修改后的名字:";
cin>>temp->name;
cout<<"\n\t\t\t请输入修改后的年龄:";
cin>>temp->age;
cout<<"\n\t\t\t请输入修改后的性别:";
cin>>temp->x;
cout<<"\n\t\t\t请输入修改后的邮编:";
cin>>temp->yobi;
cout<<"\n\t\t\t请输入修改后的部门:";
cin>>temp->bum;
cout<<"\n\t\t\t请输入修改后的薪资:";
cin>>temp->salary;
break;
}
temp = temp->next;
}
if (flag)
{
Sleep(2);
system("cls");
cout<<"\n\n\n\n\n\n\t\t\t\t修改成功!"<<endl;
}
else
{
Sleep(2);
system("cls");
cout<<"\n\n\n\n\n\n\t\t\t\t查无此人!"<<endl;
}
}
void Control::shanchu(PNode head)
{
system("cls");
string str1;
cout<<"\n\n\n";
cout<<"\t\t\t请输入要修改人的职工号:";
cin>>str1;
PNode temp = head;
int flag = 0;
while(temp->next != head)
{
if(temp->next->num == str1)
{
flag = 1;
PNode p = temp->next;
temp->next = p->next;
delete p;
break;
}
temp = temp->next;
}
if(flag)
{
Sleep(2);
system("cls");
cout<<"\n\n\n\n\n\n\t\t\t\t删除成功!"<<endl;
}
else
{
Sleep(2);
system("cls");
cout<<"\n\n\n\n\n\n\t\t\t\t查无此人!"<<endl;
}
}
void Control::chaxun(PNode head)
{
system("cls");
string str1, str2;
cout<<"\n\n\n";
cout<<"\t\t\t请输入要查询的职工号:";
cin>>str1;
cout<<"\n\n\t\t\t请输入要查询的名字:";
cin>>str2;
int flag = 0;
PNode temp = head->next;
while (temp != head)
{
if(temp->num == str1 && temp->name == str2)
{
flag = 1;
cout<<"\n\n年龄:"<<temp->age<<" 性别:"<<temp->x<<" 邮编:"<<temp->yobi<<" 部门:"
<<temp->bum<<" 薪资:"<<temp->salary<<endl;
break;
}
temp = temp->next;
}
if (flag == 0)
{
Sleep(2);
system("cls");
cout<<"\n\n\n\n\n\n\t\t\t\t查无此人!"<<endl;
}
}
void Control::paiming(PNode head)
{
PNode temp, max, p, m;
int i = 0;
p = head;
m = max = head->next;
while (p->next != head) //大循环,控制循环次数//
{
temp = head->next; //temp为下面找最大值的前节点所用,赋初值//
for (m = p->next; m != head; m = m->next) //小循环,依次找出当前最大值//
{
if (max->salary > m->salary)
{
max = m;
}
}
while(temp->next != max)
{
temp = temp->next;
}
temp->next = max->next;
max->next = head->next;
head->next = max;
if(i == 0)
{
p = max;
}
max = p->next;
i++;
}
pint(head);
}
void Control::pint(PNode head)
{
PNode temp = head->next;
system("cls");
cout<<"\n\n\n";
int flag = 0;
while (temp != head)
{
flag = 1;
cout<<"职工号:"<<temp->num<<" 名字:"<<temp->name<<" 年龄:"<<temp->age<<" 性别:"<<temp->x
<<" 邮编:"<<temp->yobi<<" 部门:"<<temp->bum<<" 薪资:"<<temp->salary<<endl;
temp = temp->next;
}
if(flag == 0)
{
Sleep(2);
system("cls");
cout<<"\n\n\n\n\n\n\t\t\t\t系统中暂时无人!"<<endl;
}
}
#include "control.h"
static PNode head; //定义头结点为全局变量
int main()
{
Control con;
head = new Node;
head->next = head;
char ecf; //选项
char sel[10];
while(1)
{
con.jiemian();
cin>>ecf;
switch (ecf)
{
case '1': //注册新职工
{
con.zhuce(head);
break;
}
case '2': //修改职工信息
{
con.xiugai(head);
cout<<"\n\n\t\t输入任意键返回:";
cin>>sel;
break;
}
case '3': //删除职工信息
{
con.shanchu(head);
cout<<"\n\n\t\t输入任意键返回:";
cin>>sel;
break;
}
case '4': //查询职工信息
{
con.chaxun(head);
cout<<"\n\n\t\t输入任意键返回:";
cin>>sel;
break;
}
case '5': //薪资排名顺序
{
con.paiming(head);
cout<<"\n\n\t\t输入任意键返回:";
cin>>sel;
break;
}
case '6': //浏览所有信息
{
con.pint(head);
cout<<"\n\n\t\t输入任意键返回:";
cin>>sel;
break;
}
default:
{
system("cls");
cout<<"\n\n\n\n\n请输入1--6!"<<endl;
cout<<"\n\n\t\t输入任意键返回:";
cin>>sel;
break;
}
}
}
return 0;
}