您的位置:首页 > 其它

职工管理系统(文件保存)

2017-02-06 16:09 274 查看
  今天,做了一个通讯录的文件保存,然后拿了这个职工管理系统再熟悉一下,感觉和通讯录差不多,唯一有点不同的就是c++和Linux c对文件操作的不同了。感觉文件操作掌握的还行了,除了C语言自带的文件操作(还没有练习)。

#ifndef _CONTROL_H_
#define _CONTROL_H_
#include <iostream>
#include <string>
#include <fstream>
#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(); //注册职工
void xiugai(); //修改职工信息
void shanchu(); //删除职工信息
void chaxun(); //查询职工信息
void paiming(); //根据薪资排名
void pint(); //打印所有信息
};

#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 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-&g
4000
t;yobi;
cout<<"\n\t\t\t请输入部门:";
cin>>p->bum;
cout<<"\n\t\t\t请输入工资:";
cin>>p->salary;

ofstream out;
out.open("职工档案.txt", ios::out | ios::app);
if(!out)
{
cerr<<"open error!"<<endl;
return;
}
out.write((char*)p, sizeof(Node));

out.close();
}

void Control::xiugai()
{
string str1;
int flag = 0;

system("cls");
cout<<"\n\n\n";
cout<<"\t\t\t请输入要修改人的职工号:";
cin>>str1;

ifstream in;
in.open("职工档案.txt", ios::in | ios::_Nocreate);
if(!in)
{
cerr<<"open error!"<<endl;
return;
}

PNode h = new Node;
PNode p = new Node;

if(in.read((char*) p, sizeof(Node)))
{
h->next = p;
while(1)
{
PNode t = new Node;

if (in.read((char*) t, sizeof(Node)))
{
p->next = t;
p = p->next;
continue;
}
else
{
p->next = h;
break;
}
}
}
else
{
cout<<"\n\n\n\n\n\n\t\t\t\t系统中暂时无人!"<<endl;
in.close();
return;
}
in.close();

PNode temp = h->next;
while(temp != h)
{
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;
return;
}

remove("职工档案.txt");
ofstream out;
out.open("职工档案.txt", ios::out | ios::app);
if(!out)
{
cerr<<"open error"<<endl;
return;
}

temp = h->next;
while (temp != h)
{
out.write((char*)temp, sizeof(Node));
temp = temp->next;
}
out.close();
}

void Control::shanchu()
{
system("cls");
string str1;
int flag = 0;
cout<<"\n\n\n";
cout<<"\t\t\t请输入要删除人的职工号:";
cin>>str1;

ifstream in;
in.open("职工档案.txt", ios::in | ios::_Nocreate);
if(!in)
{
cerr<<"open error!"<<endl;
return;
}

PNode h = new Node;
PNode p = new Node;

if(in.read((char*) p, sizeof(Node)))
{
h->next = p;
while(1)
{
PNode t = new Node;

if (in.read((char*) t, sizeof(Node)))
{
p->next = t;
p = p->next;
continue;
}
else
{
p->next = h;
break;
}
}
}
else
{
cout<<"\n\n\n\n\n\n\t\t\t\t系统中暂时无人!"<<endl;
in.close();
return;
}
in.close();

PNode temp = h;
if(temp->next->next == h)           //一个结点的表
{
if(temp->next->num == str1)
{
PNode tmp = temp->next;
temp->next = h;
delete tmp;

remove("职工档案.txt");
ofstream outfile;
outfile.open("职工档案.txt", ios::out);
outfile.close();

system("cls");
cout<<"\n\n\n\n\n\n\t\t\t\t删除成功!"<<endl;
return;
}
else
{
system("cls");
cout<<"\n\n\n\n\n\n\t\t\t\t查无此人!"<<endl;
return;
}
}

while (temp->next != h)
{
if (temp->next->num == str1)
{
flag = 1;
PNode p = temp->next;
temp->next = p->next;
free(p);
break;
}

temp = temp->next;
}

if(flag)
{
system("cls");
cout<<"\n\n\n\n\n\n\t\t\t\t删除成功!"<<endl;
}
else
{
system("cls");
cout<<"\n\n\n\n\n\n\t\t\t\t查无此人!"<<endl;
return;
}

remove("职工档案.txt");
ofstream out;
out.open("职工档案.txt", ios::out | ios::app);
if(!out)
{
cerr<<"open error"<<endl;
return;
}

temp = h->next;
while (temp != h)
{
out.write((char*)temp, sizeof(Node));
temp = temp->next;
}
out.close();
}

void Control::chaxun()
{
system("cls");
string str1, str2;
int flag = 0;
cout<<"\n\n\n";
cout<<"\t\t\t请输入要查询的职工号:";
cin>>str1;
cout<<"\n\n\t\t\t请输入要查询的名字:";
cin>>str2;

ifstream in;
in.open("职工档案.txt", ios::in | ios::_Nocreate);
if(!in)
{
cerr<<"open error!"<<endl;
return;
}

PNode h = new Node;
PNode p = new Node;

if(in.read((char*) p, sizeof(Node)))
{
h->next = p;
while(1)
{
PNode t = new Node;

if (in.read((char*) t, sizeof(Node)))
{
p->next = t;
p = p->next;
continue;
}
else
{
p->next = h;
break;
}
}
}
else
{
cout<<"\n\n\n\n\n\n\t\t\t\t系统中暂时无人!"<<endl;
in.close();
return;
}
in.close();

PNode temp = h->next;
while (temp != h)
{
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()
{
ifstream in;
in.open("职工档案.txt", ios::in | ios::_Nocreate);
if(!in)
{
cerr<<"open error!"<<endl;
return;
}

PNode h = new Node;
PNode p = new Node;

if(in.read((char*) p, sizeof(Node)))
{
h->next = p;
while(1)
{
PNode t = new Node;

if (in.read((char*) t, sizeof(Node)))
{
p->next = t;
p = p->next;
continue;
}
else
{
p->next = h;
break;
}
}
}
else
{
cout<<"\n\n\n\n\n\n\t\t\t\t系统中暂时无人!"<<endl;
in.close();
return;
}
in.close();

PNode temp, max, n, m;
int i = 0;
n = h;
m = max = h->next;
while (n->next != h)			//大循环,控制循环次数//
{
temp = h->next;			//temp为下面找最大值的前节点所用,赋初值//
for (m = n->next; m != h; m = m->next)	//小循环,依次找出当前最大值//
{
if (max->salary > m->salary)
{
max = m;
}
}
while(temp->next != max)
{
temp = temp->next;
}
temp->next = max->next;
max->next = h->next;
h->next = max;
if(i == 0)
{
n = max;
}
max = n->next;
i++;
}

remove("职工档案.txt");
ofstream out;
out.open("职工档案.txt", ios::out | ios::app);
if(!out)
{
cerr<<"open error"<<endl;
return;
}

temp = h->next;
while (temp != h)
{
out.write((char*)temp, sizeof(Node));
temp = temp->next;
}
out.close();

pint();
}

void Control::pint()
{
system("cls");
cout<<"\n\n\n";
ifstream in;
in.open("职工档案.txt", ios::in | ios::_Nocreate);
if(!in)
{
cerr<<"open error!"<<endl;
return;
}

PNode h = new Node;
PNode p = new Node;

if(in.read((char*) p, sizeof(Node)))
{
h->next = p;
while(1)
{
PNode t = new Node;

if (in.read((char*) t, sizeof(Node)))
{
p->next = t;
p = p->next;
continue;
}
else
{
p->next = h;
break;
}
}
}
else
{
cout<<"\n\n\n\n\n\n\t\t\t\t系统中暂时无人!"<<endl;
in.close();
return;
}

PNode temp = h->next;
while (temp != h)
{
cout<<"职工号:"<<temp->num<<" 名字:"<<temp->name<<" 年龄:"<<temp->age<<" 性别:"<<temp->x
<<" 邮编:"<<temp->yobi<<" 部门:"<<temp->bum<<" 薪资:"<<temp->salary<<endl;
temp = temp->next;
}
in.close();
}

#include "control.h"

int main()
{
Control con;

char ecf;   //选项
char sel[10];

while(1)
{
con.jiemian();
cin>>ecf;

switch (ecf)
{
case '1':				//注册新职工
{
con.zhuce();
break;
}
case '2':				//修改职工信息
{
con.xiugai();
cout<<"\n\n\t\t输入任意键返回:";
cin>>sel;
break;
}
case '3':				//删除职工信息
{
con.shanchu();
cout<<"\n\n\t\t输入任意键返回:";
cin>>sel;
break;
}
case '4':				//查询职工信息
{
con.chaxun();
cout<<"\n\n\t\t输入任意键返回:";
cin>>sel;
break;
}
case '5':				//薪资排名顺序
{
con.paiming();
cout<<"\n\n\t\t输入任意键返回:";
cin>>sel;
break;
}
case '6':				//浏览所有信息
{
con.pint();
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: