📄 worker.cpp
字号:
#include<string>
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<fstream>
#include<istream>
#include"worker.h"
#define NULL 0
using namespace std;
worker * loadfile(){ //从本地载入教职工数据
worker *head;
worker *p1;
ifstream inf("file.dat",ios::binary); //打开文件,定义文件输出流
if(!inf){
cerr<<"can not open file!"<<endl;
head=NULL;
return head;
}
inf.seekg(0,ios::beg); //指针指向文件头
//head=new worker;
inf.read((char*)head,sizeof(worker));
inputf(head,inf);
head->next=NULL;
while(!inf.eof()){ //使用链表
p1=new worker; //一个一个读入教职工数据
inf.read((char*)p1,sizeof(worker));
//inputf(p1,inf);
inf.tellg();
p1->next=head;
head=p1;
}
inf.close(); //关闭输出流
cout<<" 已经成功载入数据."<<endl;
cout<<endl;
return head;
}
worker* creat(){ //定义函数从0开始创建数据
worker *head=NULL;
worker *p1;
char c;
head=new worker;
cout<<" 开始输入"<<endl<<endl;
int n=0;
while(c!='n'&&c!='N') //判断是否继续输入
{
n=n+1;
if(n==1){
input(head);
head->next=NULL;}
else{
p1=new worker;
cout<<" 开始输入"<<endl<<endl;
input(p1);
p1->next=head;
head=p1;
}
cout<<" 是否继续(按n退出):";
cin>>c;}
return head;
}
void print(worker *head){ //定义函数打印全部数据
if(head==NULL)
cout<<" 没有资料!"<<endl;
else{
worker *p1;
p1=head;
while(p1!=NULL){
output(p1);
p1=p1->next;
cout<<" ********************************************"<<endl;
}
}
}
worker* add(worker *head){ //定义函数添加教职工信息
worker *p1;
p1=new worker;
cout<<" 请输入新教职工信息:"<<endl;;
input(p1);
p1->next=head;
head=p1;
return head;
}
worker* del(worker *head){ //定义函数删除教职工信息
worker *p1,*p2;
if(head==NULL)
cout<<" 没有资料!"<<endl;
else{
string number;
cout<<" 请输入要删除的人的编号:";
cin>>number;
cout<<endl;
p1=head;
while(p1->next!=NULL&&p1->getnum()!=number)
{p2=p1;p1=p1->next;}
if(p1->getnum()==number)
{if(p1==head) head=p1->next;
else p2->next=p1->next;
cout<<" 此人已成功删除!"<<endl;
}
else
cout<<" 对不起,找不到你要删除的人."<<endl;
}
return head;
}
worker* updata(worker *head){ //定义函数更新教职工信息
worker *p1;
if(head==NULL)
cout<<" 没有资料!"<<endl;
else{
string number;
p1=head;
cout<<" 请输入你想修改的人的编号: ";
cin>>number;
cout<<endl;
p1=head;
while(p1->next!=NULL&&p1->getnum()!=number)
{p1=p1->next;}
if(p1->getnum()==number){
input(p1);
cout<<" 此人已成功更改!"<<endl;}
else
cout<<" 对不起,找不到你要更改的人."<<endl;
}
return head;
}
void seek(worker *head){ //定义函数查询教职工信息
worker *p1;
if(head==NULL)
cout<<" 没有资料!"<<endl;
else{
string number;
p1=head;
cout<<" 请输入你想查询的人的编号: ";
cin>>number;
cout<<endl;
while(p1->next!=NULL&&p1->getnum()!=number)
{p1=p1->next;}
if(p1->getnum()==number)
output(p1);
else
cout<<" 对不起,找不到你要查询的人."<<endl;
}
}
void count(worker*head,char x){
worker*p=head;
int c=0;
if(x=='A'||x=='a'){
while(p!=NULL){
if(p->gettype()!="退休人员"){ //查询在职人员人数
c++;
p=p->next;
}
}
}
if(x=='B'||x=='b'){
while(p!=NULL){
if(p->getsex()=="女"){ //查询女职工人数
c++;
p=p->next;}
}
}
if(x=='C'||x=='c'){
while(p!=NULL){
if(p->gettitle()=="高级"){ //查询高职称人数
c++;
p=p->next;}
}
}
if(x=='D'||x=='d'){ //查询高学历人数
while(p!=NULL){
if(p->getdegree()=="硕士"||p->getdegree()=="博士"){
c++;
p=p->next;}
}
}
if(x=='E'||x=='e'){ //查询中共党员人数
while(p!=NULL){
if(p->gettype()=="中共党员"){
c++;
p=p->next;}
if(x!='a'&&x!='A'&&x!='b'&&x!='B'&&x!='c'&&x!='C'&&x!='d'&&x!='D'&&x!='e'&&x!='E')
cout<<" 输入错误!请正确输入!";
}
}
cout<<" +++++++++人数为+++++++++:"<<c<<endl;
}
void exit_system(worker *head ,char y){ //退出并且询问是否保存
worker *p=head;
if(y=='Y'||y=='y'){
ofstream ouf("file.dat",ios::out);
if(!ouf){
cout<<"can not open file!"<<endl;
return;}
while(p!=NULL){
ouf.write((char*)p,sizeof(worker));
//output(p,ouf);
ouf.tellp();
p=p->next;
}
ouf.close();
cout<<" 数据成功保存!";
exit(-1);
}
if(y=='N'||y=='n'){
exit(-1);}
}
void mainmenu(){ //定义菜单函数
cout<<" *******************************************************"<<endl;
cout<<" *******************************************************"<<endl;
cout<<" ** **"<<endl;
cout<<" ** 按(1):添加教职工信息; **"<<endl;
cout<<" ** **"<<endl;
cout<<" ** 按(2):删除教职工资料; **"<<endl;
cout<<" ** **"<<endl;
cout<<" ** 按(3):修改教职工资料; **"<<endl;
cout<<" ** **"<<endl;
cout<<" ** 按(4):查询教职工资料; **"<<endl;
cout<<" ** **"<<endl;
cout<<" ** 按(5):统计教职工资料; **"<<endl;
cout<<" ** **"<<endl;
cout<<" ** 按(6):输出教职工资料; **"<<endl;
cout<<" ** **"<<endl;
cout<<" ** 按(7):安全退出; **"<<endl;
cout<<" ** **"<<endl;
cout<<" *******************************************************"<<endl;
cout<<" *******************************************************"<<endl;
}
int main(){ //主函数
worker *head;
cout<<endl<<endl<<endl<<endl<<endl;
cout<<" ";
for(int j=0;j<=71;j++){cout<<char(4);} //欢迎界面
cout<<endl;
cout<<" "<<char(4)<<" ########## ## ### ###### "<<char(4)<<endl;
for(int i=0;i<=50000000;i++){} //延迟循环
cout<<" "<<char(4)<<" ##### ## ## ## #### ## ## ## "<<char(4)<<endl;
for( i=0;i<=50000000;i++){}
cout<<" "<<char(4)<<" ## ## ## ## ## ## ## "<<char(4)<<endl;
for( i=0;i<=50000000;i++){}
cout<<" "<<char(4)<<" ## #### ## ## # ## ### "<<char(4)<<endl;
for( i=0;i<=50000000;i++){}
cout<<" "<<char(4)<<" ## ## ## ## ### #### ## "<<char(4)<<endl;
for( i=0;i<=50000000;i++){}
cout<<" "<<char(4)<<" ## #### ## ## ## "<<char(4)<<endl;
for( i=0;i<=50000000;i++){}
cout<<" "<<char(4)<<" ### ### ################## "<<char(4)<<endl;
cout<<" ";
for( j=0;j<=71;j++){cout<<char(4);}
for( i=0;i<=50000000;i++){}
cout<<endl<<endl<<endl; //选择载入数据还是创建数据
cout<<" ***************欢迎来到高校人事管理系统**************** "<<endl;
cout<<" ** ** "<<endl;
cout<<" ** 按(0):从本地载入数据; ** "<<endl;
cout<<" ** ** "<<endl;
cout<<" ** 按(1):开始创建数据; ** "<<endl;
cout<<" ** ** "<<endl;
cout<<" ******************************************************* "<<endl;
int b;
cout<<" 请选择进入操作:";
cin>>b;
cout<<endl;
switch(b) {
case 0:
head=loadfile();
mainmenu();
cout<<" 请选择进入下一步操作:";
break;
case 1:
head=creat();
mainmenu();
cout<<" 请选择进入下一步操作:";
break;
default:
cout<<" !!!!!输入错误!!!!!请重新选择:";
}
int a;
cin>>a;
cout<<endl;
do{
switch(a) {
case 1:
head=add(head);
mainmenu();
cout<<" 请选择进入下一步操作:";
break;
case 2:
head=del(head);
mainmenu();
cout<<" 请选择进入下一步操作:";
break;
case 3:
head=updata(head);
mainmenu();
cout<<" 请选择进入下一步操作:";
break;
case 4:
seek(head);
mainmenu();
cout<<" 请选择进入下一步操作:";
break;
case 5:
char x;
cout<<" 请输入你想统计的类型:"<<endl;
cout<<endl;
cout<<" **********************************************"<<endl;
cout<<" * *"<<endl;
cout<<" * 按(A):统计在职人员数目; *"<<endl;
cout<<" * 按(B):统计女教职工数目; *"<<endl;
cout<<" * 按(C):统计高职称人数目; *"<<endl;
cout<<" * 按(D):统计高学历的数目; *"<<endl;
cout<<" * 按(E):统计中共党员数目; *"<<endl;
cout<<" * *"<<endl;
cout<<" **********************************************"<<endl<<endl;
cout<<" 请选择:";
cin>>x;
cout<<endl;
count(head,x);
mainmenu();
cout<<" 请选择进入下一步操作:";
break;
case 6:
print(head);
cout<<endl<<endl<<endl;
mainmenu();
cout<<" 请选择进入下一步操作:";
break;
case 7:
char z;
cout<<" *************是否保存数据?(Y/N)***************"<<endl;
cout<<endl;
cout<<" ";
cin>>z;
exit_system(head ,z);
cout<<endl;
break;
default:
cout<<" !!!!!输入错误!!!!!请重新选择:";
}
cin>>a;
cout<<endl;
}while(a<=8&&a>=0);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -