用連結串列寫職工管理系統
阿新 • • 發佈:2019-02-08
職工資訊管理系統:
存在一個數據檔案,用來儲存職工各種資訊:職工號,姓名,年齡,性別,
郵編,部門,工資可以註冊新職工;
允許修改職工資訊
允許刪除職工資訊;
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;
}