📄 employee.cpp
字号:
#include<iostream>
#include<fstream>
#include<conio.h>
#include<iomanip>
#include<windows.h>
#include<string>
#include<sstream>
#include<cassert>
#include"employee.h"
using namespace std;
void inline clear()
{
system("PAUSE");
system("cls");
cout<<" ***********************"<<endl;
cout<<" * *"<<endl;
cout<<" * —— 超市 *"<<endl;
cout<<" * *"<<endl;
cout<<" * "<<__DATE__<<" *"<<endl;
cout<<" * "<<__TIME__<<" *"<<endl;
cout<<" ***********************"<<endl;
}
Employee::Employee()
{
file.open("employee.dat",ios::out|ios::in);
if(file.fail())
{
cerr<<"\n缺少文件“employee.dat”!!\n";
}
file.seekg(0,ios::end);
streampos pos3=file.tellg();
if((int)pos3==0)
{
cout<<"正在初始化员工文件..."<<endl;
emp.accounts=0;
strcpy(emp.name,"manager");
strcpy(emp.password,"manager");
strcpy(emp.date,"00:00:00");
strcpy(emp.id_card,"000000000000000000");
file.seekg(0);
file.write((char*)&emp,sizeof(employee));
}
memset( emp.name,' ',sizeof(emp.name) );
}
Employee::~Employee()
{
file.close();
}
void Employee::input_password()
{
char *ptr=emp.password;
int count=0;
do
{
char ch=getch();
if(ch == 0x0A || ch == 0x0D)break;//回车
else if(isdigit(ch)||isalpha(ch))
{
*ptr++=ch;
cout<<"*"; count++;
}
else if(ch=='\b'&& ptr>emp.password)
{
*(--ptr)='\0';
cout<<"\b \b";
count--;
}
}while(count<10);
emp.password[count]='\0';
}
void Employee::new_employee()
{
cout<<"\n 注册_________________雇员:";
cout<<"\n_______________________________________________";
cout<<"\n你确定注册一个新帐号吗?(回车确认)";
char get_ok=getche();
if(get_ok==13)
{
while(1)
{
system("cls");
cout<<"\n请输入你的密码:(长度小于10位)\n密码:";
string s1,s2;
Employee::input_password();
s1=emp.password;
cout<<"\n密码:";
input_password();
s2=emp.password;
if(s1==s2)break;
else cerr<<"\n\nError:密码不一致!重试!!\n";
}
cout<<"\n请输入你的资料:\n姓名: ";
cin>>emp.name;
cout<<"\n身份证号:";
cin>>emp.id_card;
ostringstream os;//注册日期
SYSTEMTIME st;
GetLocalTime(&st);
os<<st.wYear<<"/"<<st.wMonth<<"/"<<st.wDay<<"/"<<st.wHour<<":"<<st.wMinute
<<":"<<st.wSecond<<endl;
cout<<"创建日期:"<<st.wYear<<"/"<<st.wMonth<<"/"<<st.wDay<<"/"<<st.wHour
<<":"<<st.wMinute<<":"<<st.wSecond<<endl;
string s5=os.str();
const char* c5=s5.c_str();
strcpy(emp.date,c5);
file.seekp(0,ios_base::end);//写入文件
streampos ps=file.tellg();
int size=sizeof(employee);//68
emp.accounts=ps/size;
//先查找,看这次的号是否要覆盖00号
file.seekg(0);
employee e_sure;
file.read((char*)&e_sure,sizeof(employee));
//先读取第一个帐号,
if(emp.accounts==1&&!strcmp(e_sure.id_card,"000000000000000000"))
{
file.seekp(0,ios::beg);
emp.accounts=0;
cout<<"\n恭喜:你已经成为该超市的管理员,你的ID为00,有权对超市进行管理!!!\n";
cout<<"\n下次请以该号码登入!!!"<<endl;
}
else
{
file.seekp(0,ios_base::end);
cout<<"你的ID为:"<<setw(2)<<setfill('0')<<emp.accounts<<endl;
cout<<"\n下次请以该号码登入!!!"<<endl;
}
file.write((char*)&emp,sizeof(employee));
}
system("PAUSE");
system("cls");
cout<<" ***********************"<<endl;
cout<<" * *"<<endl;
cout<<" * —— 超市 *"<<endl;
cout<<" * *"<<endl;
cout<<" ***********************"<<endl;
}
int Employee::landing()
{
cout<<"\n =========登入==========\n";
cout<<" =======================\n";
cout<<"-----------------------------------------------------------------\n";
bool right=true;
int size1=0;
file.seekg(0,ios::end);
streampos ps1=file.tellp();
while(right)
{
cout<<"\n 帐号: ";
while(1)
{
cin>>emp.accounts;
if(cin.fail())
{
cin.clear(ios::goodbit);
fflush(stdin);
cout<<"\nError!!\n 帐号: ";
continue;
}
else break;
} //判断文件大小
if(emp.accounts>1000){cout<<"\nError:帐号错误!!\n";continue;}
size1=(emp.accounts+1)*sizeof(employee);
if(size1>ps1)
{
cout<<"\n该帐号不存在!!"<<endl;
clear();
continue;
}
cout<<endl<<" 密码: ";
input_password();
string s3=emp.password;
file.seekg((emp.accounts)*(sizeof(employee)+1));
file.read((char*)&emp,sizeof(employee));
string s4=emp.password;
int account=emp.accounts;
if(s3==s4)
{
cout<<"\n++++++你已经成功登陆"<<emp.accounts<<"号服务员,请作业+++++\n";
cout<<"++++++++++++++++++++++++++++++++++++++++++++\n";
right=false;
}
else{ cerr<<"\n密码错误!!!";clear();}
}
return emp.accounts;
}
void Employee::change_password()
{
cout<<"\n\n ========修改密码==(请先登陆)======\n";
cout<<"===================================================\n";
int no_account=landing();
while(1)
{
cout<<"\n请输入你修改后的密码: ";
string s3,s4;
input_password();
s3=emp.password;
cout<<"\n请再次输入你的新密码: ";
input_password();
s4=emp.password;
if(s3==s4)break;
else cerr<<"\n密码不一致,请重试!"<<endl;
} //写入文件
file.seekp((emp.accounts)*(sizeof(employee)+1));
file.write((char*)&emp,sizeof(employee));
}
void Employee::view_record()
{
cout<<"\n -------员工资料阅览---------\n";
cout<<"**********************************\n";
cout<<"帐号| 密码 | 姓名 | 身份证 | 创建日期 | \n";
file.seekg(0,ios::beg);
while(file.read((char*)&emp,sizeof(employee)))
{
cout<<setw(2)<<setfill('0')<<emp.accounts<<setfill(' ')
<<setw(15)<<emp.password<<setw(15)<<emp.name
<<setw(23)<<emp.id_card<<setw(23)<<emp.date;
}
file.clear(ios::goodbit);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -