您的位置:首页 > 编程语言 > C语言/C++

用链表写职工管理系统

2017-02-12 19:45 309 查看
职工信息管理系统:

存在一个数据文件,用来存储职工各种信息:职工号,姓名,年龄,性别,

邮编,部门,工资

可以注册新职工;

允许修改职工信息

允许删除职工信息;

4,按照按照姓名和部门查询职工信息;

可以按照工资多少进行排名,

可以浏览所有职工信息;

.有一个主界面,供选择和调用上述选项。

.用C++中,文件和链表实现

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;

typedef struct node
{
int ID;
char name[20];
int age;
char sex[20];
char postcodes[20];
char department[20];
int salary;
struct node *next;   // 结点指针
}t;

typedef t *PNode;     // 重命名结点指针类型

class Opt{
public:
int Creat_list_tail(PNode h);
int Display(PNode h);
int Search(PNode h, char *p);
int Del(PNode h, char *p);
int Change(PNode h, char *p);
int Sort(PNode h);
int Quit(PNode h);
}s;

PNode head_node;
int MARK;
int RUN;

//INSERT————添加通讯录好友 尾插法
int Opt::Creat_list_tail(PNode h)
{
cout<<"#1";
if (h == NULL)
{
return -1;
}
PNode node = new t;

cout<<"输入职工号";
cin>>node->ID;
cin.ignore(100, '\n');
cout<<"输入姓名";
cin>>node->name;
cin.ignore(100, '\n');
cout<<"输入年龄";
cin>>node->age;
cin.ignore(100, '\n');
cout<<"输入性别";
cin>>node->sex;
cin.ignore(100, '\n');
cout<<"输入邮编";
cin>>node->postcodes;
cin.ignore(100, '\n');
cout<<"输入部门";
cin>>node->department;
cin.ignore(100, '\n');
cout<<"输入工资";
cin>>node->salary;
cin.ignore(100, '\n');

node->next = NULL;

PNode temp = h;
while (temp->next)
{
temp = temp->next;
}
temp->next = node;

return 0;
}
int Opt::Display(PNode h)
{
if (h == NULL)
{
return -1;
}
PNode temp = h->next;  // 链表第一个结点指针
while (temp)
{
cout<<"职工号";
cout<<temp->ID<<endl;
cout<<"姓名";
cout<<temp->name<<endl;
cout<<"年龄";
cout<<temp->age<<endl;
cout<<"性别";
cout<<temp->sex<<endl;
cout<<"邮编";
cout<<temp->postcodes<<endl;
cout<<"部门";
cout<<temp->department<<endl;
cout<<"工资";
cout<<temp->salary<<endl;
temp = temp->next;
}
return 0;
}

int Opt::Search(PNode h, char *p)
{
if (h == NULL)
{
return -1;
}
PNode temp = h->next;
int flag = 1;
while(temp)
{
if(strcmp(temp->name, p) == 0)
{
flag = 0;
cout<<"输入职工号";
cout<<temp->ID<<endl;
cout<<"输入姓名";
cout<<temp->name<<endl;
cout<<"输入年龄";
cout<<temp->age<<endl;
cout<<"输入性别";
cout<<temp->sex<<endl;
cout<<"输入邮编";
cout<<temp->postcodes<<endl;
cout<<"输入部门";
cout<<temp->department<<endl;
cout<<"输入工资";
cout<<temp->salary<<endl;
break;
}
temp = temp->next;
}
if (flag)
{
printf("\t没有此联系人!\n");
}
return 0;
}
int Opt::Del(PNode h, char *p)
{
//1搜索联系人
if (h == NULL)
{
return -1;
}
PNode temp = h->next;

int i;
int name_count = 0;
int a[20];
while(temp)
{
for(i = 0; i < 21; i++)
{
if(*(p+i) != temp->name[i])
{
break;
}
else if( (*(p+i) == '\0') && (temp->name[i] == '\0') )
{
a[name_count] = temp->ID;
name_count++;
break;
}
}
temp = temp->next;
}

//2删除联系人
PNode front = NULL;

if (name_count == 0)
{
cout<<"\t没有此联系人!"<<endl;
}
else
{
printf("\t查找到联系人%d个\n",name_count);
printf("\t选择联系人删除:\n");
i = 0;
while(i < name_count )
{
printf("\t\tID%d: %08d\n",i+1,a[i]);
i++;
}
int choose;
printf("\t输入要删除好友ID:");
cin>>choose;
cin.ignore(100, '\n');
temp = h->next;

if(temp->next == NULL)
{
free(temp);
h->next = NULL;
}
else
{
while(temp)
{
if(temp->ID == choose)
{
PNode m = front->next;
front->next = m->next;
free(m);
m=NULL;
}
front=temp;
temp = temp->next;
}
}
}

return 0;
}
int Opt::Change(PNode h, char *p)
{
//1搜索联系人
if (h == NULL)
{
return -1;
}
PNode temp = h->next;

int i;
int name_count = 0;
int a[20];
while(temp)
{
for(i = 0; i < 21; i++)
{
if(*(p+i) != temp->name[i])
{
break;
}
else if( (*(p+i) == '\0') && (temp->name[i] == '\0') )
{
a[name_count] = temp->ID;
name_count++;
break;
}
}
temp = temp->next;
}
//2修改联系人
PNode front = NULL;

if (name_count == 0)
{
cout<<"\t没有此联系人!"<<endl;
}
else
{
printf("\t查找到联系人%d个\n",name_count);
printf("\t选择联系人修改:\n");
i = 0;
while(i < name_count )
{
printf("\t\tID%d: %08d\n",i+1,a[i]);
i++;
}
int choose;
printf("\t输入要修改好友ID:");
cin>>choose;
cin.ignore(100, '\n');
temp = h->next;
while(temp)
{
if(temp->ID == choose)
{
cout<<"输入职工号";
cin>>temp->ID;
cin.ignore(100, '\n');
cout<<"输入姓名";
cin>>temp->name;
cin.ignore(100, '\n');
cout<<"输入年龄";
cin>>temp->age;
cin.ignore(100, '\n');
cout<<"输入性别";
cin>>temp->sex;
cin.ignore(100, '\n');
cout<<"输入邮编";
cin>>temp->postcodes;
cin.ignore(100, '\n');
cout<<"输入部门";
cin>>temp->department;
cin.ignore(100, '\n');
cout<<"输入工资";
cin>>temp->salary;
cin.ignore(100, '\n');
return 0;
}
front=temp;
temp = temp->next;
}
}

return 1;
}
int Opt::Sort(PNode h)
{
if (h == NULL)
{
return -1;
}
PNode temp = h->next;
int count = 0;
int maxsalary = 0;
int t = 0;
while(temp)
{
count++;
if(maxsalary < temp->salary)
{
maxsalary = temp->salary;
}
temp = temp->next;
}
cout<<"总人数为"<<count<<endl;
cout<<"最高薪为"<<maxsalary<<endl;
int i = 0, j = 0;
int flag = 0;
while(count != 0)
{
temp = h->next;
t = maxsalary;
maxsalary = 0;
while(temp)
{
//cout<<"#1";

if(temp->salary == t)//打印当前薪水的人的信息
{
cout<<"职工号";
cout<<temp->ID<<endl;
cout<<"姓名";
cout<<temp->name<<endl;
cout<<"年龄";
cout<<temp->age<<endl;
cout<<"性别";
cout<<temp->sex<<endl;
cout<<"邮编";
cout<<temp->postcodes<<endl;
cout<<"部门";
cout<<temp->department<<endl;
cout<<"工资";
cout<<temp->salary<<endl<<endl;
//system("pause");
}
else if((maxsalary < temp->salary) && (temp->salary < t))//找到下一个的人的薪水
{
maxsalary = temp->salary;
flag = 1;
}
temp = temp->next;
}
if(flag != 1)
{
count = 0;
}
flag = 0;
}

return 0;

}

int Opt::Quit(PNode h)
{
PNode temp = h->next;
int count = 0;
fstream outfile("stud.txt",ios::out);
if(!outfile)
{
cerr<<"open error!"<<endl;
abort( );
}
while(temp)
{
outfile.write((char *)temp,sizeof(t));
temp = temp->next;
}
outfile.close( );
RUN = 0;
return 0;
}
void System_init()
{
RUN = 1;
MARK = 0;
system("color A4");
head_node = new t;
head_node->next = NULL;   // 空链表

fstream infile("stud.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
abort( );
}
PNode temp = new t;
if(infile.read((char *)temp,sizeof(t)))
{
head_node->next = temp;
}
else
{
return;
}
PNode end = head_node->next;
while(1)
{
PNode temp = new t;
if(infile.read((char *)temp,sizeof(t)))
{
end->next = temp;
end = end->next;
end->next = NULL;
}
else
{
free(temp);
infile.close( );
break;
}
}
}

void welcome()
{

}
void Menu(int menu)
{
switch (menu)
{
case 0://Main menu
{
cout<<"Main menu"<<endl;
cout<<"1-------------------------注册新职工"<<endl;
cout<<"2-------------------------修改职工信息"<<endl;
cout<<"3-------------------------删除职工信息"<<endl;
cout<<"4-------------------------按照按照姓名和部门查询职工信息"<<endl;
cout<<"5-------------------------按照工资多少进行排名"<<endl;
cout<<"6-------------------------浏览所有职工信息"<<endl;
cout<<"9-------------------------退出程序"<<endl;
break;
}
case 1://Register~
{
system("cls");
cout<<"注册新职工"<<endl;
break;
}
case 2://Change
{
system("cls");
cout<<"修改职工信息"<<endl;
break;
}
case 3://Delete~
{
system("cls");
cout<<"删除职工信息"<<endl;
break;
}
case 4://Search~
{
system("cls");
cout<<"按照按照姓名和部门查询职工信息"<<endl;
break;
}
case 5://Sort_salary
{
system("cls");
cout<<"按照工资多少进行排名"<<endl;
break;
}
case 6://Display~
{
system("cls");
cout<<"浏览所有职工信息"<<endl;
break;
}
case 9://quit~
{
system("cls");
cout<<"退出程序"<<endl;
break;
}
}
}
int Choose()
{
cout<<"请输入选择";
char choose[10];
cin>>choose;
cin.ignore(100, '\n');
//fflush(stdin);
return (int)*choose - 48;
}
void Task(int choose)
{
cout<<"choose = "<<choose<<endl;
Menu(choose);
switch(choose)
{
case 0:
{
break;
}
case 1:
{
if(s.Creat_list_tail(head_node))
cout<<"ERROR1"<<endl;
system("pause");
system("cls");
Menu(0);
break;
}
case 2:
{
char name[20];
cout<<"输入姓名";
cin>>name;
cin.ignore(100, '\n');
if(s.Change(head_node,name))
cout<<"不存在该员工信息"<<endl;
system("pause");
system("cls");
Menu(0);
break;
}
case 3:
{
char name[20];
cout<<"输入姓名";
cin>>name;
cin.ignore(100, '\n');
if(s.Del(head_node,name))
cout<<"ERROR3"<<endl;
system("pause");
system("cls");
Menu(0);
break;
}
case 4:
{
char name[20];
cout<<"输入姓名";
cin>>name;
cin.ignore(100, '\n');
if(s.Search(head_node,name))
cout<<"ERROR4"<<endl;
system("pause");
system("cls");
Menu(0);
break;
}
case 5:
{
if(s.Sort(head_node))
cout<<"ERROR5"<<endl;
system("pause");
system("cls");
Menu(0);
break;
}
case 6:
{
if(s.Display(head_node))
cout<<"ERROR6"<<endl;
system("pause");
system("cls");
Menu(0);
break;
}
case 9:
{
if(s.Quit(head_node))
cout<<"ERROR9"<<endl;
break;
}
}

}
int main()
{
System_init();
Menu(0);
while (RUN)
{
Task(Choose());

//system ("pause");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言