📄 cpp2.cpp
字号:
#include<string>
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<fstream>
#include<istream>
#define NULL 0
using namespace std;
class worker{ //定义教职工类
public:
worker();
~worker();
string getnum() //获取编号
{return num;}
string getname() //获取姓名
{return name;}
string getsex() //获取性别
{return sex;}
string getage() //获取年龄
{return age;}
string gettype() //获取教职工分类
{return type;}
string getpost() //获取职务
{return post;}
string gettitle() //获取职称
{return title;}
string getparty() //获取政治面貌
{return party;}
string getdegree() //获取学历
{return degree;}
void setnum(string a) //设定编号
{num=a;}
void setname(string a) //设定姓名
{ name=a;}
void setsex(char t) //设定性别
{
if(t=='A'||t=='a')
sex="男";
if(t=='B'||t=='b')
sex="女";
if(t!='a'&&t!='A'&&t!='b'&&t!='B')
cout<<" 输入错误!请正确输入!";
}
void setage(string b) //设定年龄
{ age=b;}
void settype(char t) //设定教职工分类
{ if(t=='A'||t=='a')
type="行政人员";
if(t=='B'||t=='b')
type="教师";
if(t=='C'||t=='c')
type="一般员工";
if(t=='D'||t=='d')
type="退休人员";
if(t=='E'||t=='e')
type="返聘人员";
if(t=='F'||t=='f')
type="临时工";
if(t!='a'&&t!='A'&&t!='b'&&t!='B'&&t!='c'&&t!='C'&&t!='d'&&t!='D'&&t!='e'&&t!='E'&&t!='f'&&t!='F')
cout<<" 输入错误!请正确输入!";
}
void setpost(char t) //设定职务
{
if(t=='A'||t=='a')
post="无";
if(t=='B'||t=='b')
post="科级";
if(t=='C'||t=='c')
post="处级";
if(t=='D'||t=='d')
post="地级";
if(t!='a'&&t!='A'&&t!='b'&&t!='B'&&t!='c'&&t!='C'&&t!='d'&&t!='D')
cout<<" 输入错误!请正确输入!";
}
void settitle(char t) //设定职称
{
if(t=='A'||t=='a')
title="无";
if(t=='B'||t=='b')
title="初级";
if(t=='C'||t=='c')
title="中级";
if(t=='D'||t=='d')
title="高级";
if(t!='a'&&t!='A'&&t!='b'&&t!='B'&&t!='c'&&t!='C'&&t!='d'&&t!='D')
cout<<" 输入错误!请正确输入!";
}
void setparty(char t) //设定政治面貌
{
if(t=='A'||t=='a')
party="群众";
if(t=='B'||t=='b')
party="中共党员";
if(t=='C'||t=='c')
party="民主党派";
if(t!='a'&&t!='A'&&t!='b'&&t!='B'&&t!='c'&&t!='C')
cout<<" 输入错误!请正确输入!";
}
void setdegree(char t) //设定学历
{
if(t=='A'||t=='a')
degree="中专";
if(t=='B'||t=='b')
degree="大专";
if(t=='C'||t=='c')
degree="大学";
if(t=='D'||t=='d')
degree="硕士";
if(t=='E'||t=='e')
degree="博士";
if(t!='a'&&t!='A'&&t!='b'&&t!='B'&&t!='c'&&t!='C'&&t!='d'&&t!='D'&&t!='e'&&t!='E')
cout<<" 输入错误!请正确输入!";
}
worker *next;
friend void inputf(worker *head,ifstream inf);
private:
string num;
string name;
string sex;
string age;
string type;
string post;
string title;
string party;
string degree;
};
worker::worker(){ //定义构造函数
num="1001";
name="李军";
sex="男";
age="20";
type="行政人员";
post="科级";
title="中级";
party="中共党员";
degree="大学";
next=NULL;
}
worker::~worker(){};
void inputf(worker *head,ifstream inf){
inf>>head->num>>head->name>>head->sex>>head->age>>head->type>>head->post>>head->title>>head->party>>head->degree;
}
void input(worker *wor){ //输入教职工信息
string a;
char t;
string b;
cout<<" 输入编号:";
cin>>a;
wor->setnum(a);
cout<<endl;
cout<<" 输入姓名:";
cin>>a;
wor->setname(a);
cout<<endl;
cout<<" 输入性别(A.男 B.女):";
cin>>t;
wor->setsex(t);
cout<<endl;
cout<<" 输入年龄(数值):";
cin>>b;
wor->setage(b);
cout<<endl;
cout<<" 输入职工类别(A.行政人员 B.教师 C.一般员工 D.退休人员 E.返聘人员 F.临时工):";
cin>>t;
wor->settype(t);
cout<<endl;
cout<<" 输入职务(A .无 B.科级 C.处级 D.地级):";
cin>>t;
wor->setpost(t);
cout<<endl;
cout<<" 输入职称(A .无 B.低级 C.中级 D.高级):";
cin>>t;
wor->settitle(t);
cout<<endl;
cout<<" 输入政治面貌(A.群众 B.中共党员 C.民主党派):";
cin>>t;
wor->setparty(t);
cout<<endl;
cout<<" 输入学历(A.中专 B.大专 C.大学 D.硕士 E.博士):";
cin>>t;
wor->setdegree(t);
cout<<endl;
}
void output(worker *head,ofstream ouf){
worker *p;
p=head;
ouf<<setw(10)<<p->getnum()<<setw(10)<<p->getname()<<setw(10)<<p->getsex()<<setw(10)<<p->getage()<<setw(10)<<p->gettype()<<setw(10)<<p->getpost()<<setw(10)<<p->gettitle()<<setw(10)<<p->getparty()<<setw(10)<<p->getdegree();
}
void output(worker *wor){ //输出教职工信息
cout<<" 编号: ";
cout<<wor->getnum();
cout<<endl;
cout<<" 姓名: ";
cout<<wor->getname();
cout<<endl;
cout<<" 性别: ";
cout<<wor->getsex();
cout<<endl;
cout<<" 年龄: ";
cout<<wor->getage();
cout<<endl;
cout<<" 职工分类: ";
cout<<wor->gettype();
cout<<endl;
cout<<" 职务: ";
cout<<wor->getpost();
cout<<endl;
cout<<" 职称: ";
cout<<wor->gettitle();
cout<<endl;
cout<<" 政治面貌: ";
cout<<wor->getparty();
cout<<endl;
cout<<" 学历: ";
cout<<wor->getdegree();
cout<<endl;
}
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -