📄 电话超市管理.cpp
字号:
////// 计算机012班 201041231 廖文帅 ////
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include <fstream.h>
const int Maxg=10; //最多的用户数量
const int Maxtel=10; //超市最大数电话
class Telphone //电话类
{
int tag; //占用标记 1:已占用 0:未占用
int no; //电话编号
public:
Telphone() {}
int getno()
{ return no;} //获取电话编号
int gettag()
{ return tag;} //获取占用标记
void setno(int newno)
{ no=newno;} //设置电话编号
void settag(int newtag)
{ tag=newtag;} //设置占用标记
int applytelphone() //申请电话
{
if (tag==0)
{
tag=1;
return 1;
}
return 0;
}
void exittelphone() //退机操作
{
tag = 0;
}
void disp() //输出电话使用情况
{
cout << setw(6) << no << (tag==1?"已占用":"可用") << endl;
}
};
class TELDatabase //电话库类
{
int top; //记录电话
Telphone telphone[Maxtel]; //电话记录
public:
TELDatabase() //构造函数,将telphone.txt读到telphone[]中
{
Telphone tel;
top=-1; //赋出值-1
fstream file("telphone.txt",ios::in);
file.read((char *)&tel,sizeof(tel));
if (!file) //创新处,如文件为空
{ //就自动生成电话编号和给状态赋予初值
file.close();
for(int i=0;i<Maxtel;i++)
{
telphone[i].setno( i );
telphone[i].settag( 0 );
}
}
else
{
top++;
telphone[top]=tel;
while(1)
{
file.read((char *)&tel,sizeof(tel));
if (!file) break;
top++;
telphone[top]=tel;
}
file.close();
}
}
Telphone *query(int telphoneid) //查找电话
{
for(int i=0;i<Maxtel;i++)
if(telphone[i].getno()==telphoneid)
return &telphone[i];
return NULL;
}
void telphonedata(); //申明TELDatabase的成员函数telphonedata()
void disp()
{
for(int i=0;i<Maxtel;i++)
telphone[i].disp();
}
~TELDatabase() //析构函数,将 telphone[]写入telphone.txt 文件中
{
fstream file("telphone.txt",ios::out);
for(int i=0;i<Maxtel;i++)
file.write((char *)&telphone[i],sizeof(telphone[i]));
file.close();
}
};
void TELDatabase::telphonedata()
{
int choice=1;
int telphoneid;
Telphone *tel;
while(choice!=0)
{
cout << "\t━━━━━━━━电话信息━━━━━━━━━━"<<endl;
cout << "\t 1:查找 2:显示所有 "<<endl;
cout << "\t 0: 返回 ┃"<<endl;
cout << "\t━━━━━━━━━━━━━━━━━━━━━━"<<endl<<endl;
cout << "请输入你的选择:";
cin >> choice;
switch (choice)
{
case 1:
cout << "输入电话编号:";
cin >> telphoneid;
tel=query(telphoneid);
if (tel != NULL)
tel->disp();
else
cout<<"电话不存在";
break;
case 2:
disp();
break;
}
}
}
class Guest //用户类
{
int tag; //删除标记 1:已删 0:未删
int no; //用户卡号
char name[10]; //用户姓名
int apptelphone; //申请电话标记
public:
Guest() //构造函数,给上面定义赋予初值
{
tag = 1 ;
no = 0;
apptelphone = -1; //初值-1代表没有申请电话
}
char *getname()
{ return name;} //获取姓名
int getno()
{ return no;} //获取用户卡号
int gettag()
{ return tag;} //获取删除标记
int getapptelphone()
{ return apptelphone;} //获取申请电话标记
void setname( char na[]) //设置用户姓名
{
strcpy(name,na);
}
void settag(int newtag) //设置标记
{ tag = newtag;}
void addguest(int n,char *na) //增加用户
{
tag = 0;
no=n;
strcpy(name,na);
}
void deleteguest() //删除用户
{
tag = 1;
apptelphone=-1;
}
void applytelphone(int telphoneid) //申请用电话
{
if(apptelphone==-1) //如果没有申请
{
apptelphone=telphoneid;
return;
}
}
int exittelphone(int telphoneid) //退用电话
{
if(apptelphone==telphoneid)
{
apptelphone=-1;
return 1;
}
return 0;
}
void disp() //输出用户信息
{
if (tag == 0) //有记录信息的输出
{
cout << setw(8) <<"用户卡号:"<< no << setw(10) <<" 用户姓名: "<< name<< setw(10) << " 正在使用的电话编号:";
if(apptelphone>0) //如果申请有电话就输出
cout << apptelphone;
}
}
};
class GDatabase //用户库类
{
int top; //记录用户
Guest cli[Maxg]; //Guest 的对像数组cli[]
public:
GDatabase() //构造函数,将guest.txt 读到cli[]中
{
Guest s;
top=-1; //赋初值
fstream file("guest.txt",ios::in);
while (1)
{
file.read((char *)&s,sizeof(s));
if(!file) break;
top++;
cli[top]=s;
}
file.close();
}
Guest *query(int guestid) //按编号查找
{
for(int i=0; i<=top;i++)
if(cli[i].getno()==guestid && cli[i].gettag()==0)
return &cli[i];
return NULL;
}
void disp() //输出所有用户信息
{
for(int i=0;i<=top;i++)
{
if (cli[i].gettag() == 0) //未删除的显示
cli[i].disp();
}
if (top == -1)
cout<<"没有用户资料"<<endl;
}
void guestdata(TELDatabase &TelphoneDB); //申明GDatabase类的成员函数guestdata()
~GDatabase() //析构函数,将cli[]写入guest.txt文件中
{
fstream file("guest.txt",ios::out);
for(int i=0;i<=top;i++)
if(cli[i].gettag()==0)
file.write((char *)&cli[i],sizeof(cli[i]));
file.close();
}
};
void GDatabase::guestdata(TELDatabase &TelphoneDB)//引用主函数的TelphoneDB
{
int choice=1;
char cname[20];
int guestid;
Guest *c;
Telphone *tel;
int t;
while (choice!=0)
{
cout << "\t━━━━━━━━ 用户维护 ━━━━━━━━"<<endl;
cout << "\t 1: 新增 2: 更改 3: 删除 "<<endl;
cout << "\t 4: 查找 5: 显示 0: 返回 "<<endl;
cout << "\t━━━━━━━━━━━━━━━━━━━━━"<<endl<<endl;
cout << "请输入你的选择:";
cin >> choice;
switch (choice)
{
case 1:
cout << "输入用户卡号:";
cin >> guestid;
cout << "输入用户姓名:";
cin >> cname;
c=query(guestid); //查找,如果为空就写入
if (c == NULL)
{
for(int i=0; i<Maxg;i++)
{
if(cli[i].gettag() == 1)
{
top++;
cli[i].addguest(guestid, cname);
cout<<"增加用户成功"<<endl;
break;
}
}
if (i==Maxg)
cout<<"用户库已满"<<endl;
}
break;
case 2:
cout << "输入用户编号:";
cin >> guestid;
c=query(guestid);
if(c==NULL)
{
cout << "该用户没有注册" << endl;
break;
}
cout << " 输入新的姓名:";
cin >> cname;
c->setname(cname); //写入新的值
cout<<"修改用户成功"<<endl;
break;
case 3:
cout << "输入用户卡号:";
cin >>guestid;
c=query(guestid);
if (c != NULL)
{
t = c->getapptelphone();
if (t > -1)
{
tel = TelphoneDB.query(t);
if (tel != NULL)
{
tel->exittelphone(); //释放所占用的电话
}
}
c->deleteguest();
cout<<"删除用户成功"<<endl;
}
else
{
cout << "该用户没有注册" << endl;
}
break;
case 4:
cout << " 输入用户卡号:";
cin >> guestid;
c=query(guestid);
if(c==NULL)
{
cout <<"该用户没有注册" << endl;
break;
}
c->disp();
break;
case 5:
disp();
break;
}
}
}
void key()
{
char s[80],password[]="012";
int flag=0,n=3,j;
do
{
cout<<"请输入口令:"<<endl;
cin>>s;
if (!(strcmp(s,password)))
{
cout<<"欢迎你使用本系统!"<<endl;
cout<<"请稍等... "<<endl;
for(j=0;j<10000;j++)
for(n=0;n<10000;n++);
flag=1;
break;
}
else
if (n>0)
{ cout<<"口令错,请再输入!"<<endl;
n--;
}
} while (n>0);
if (!flag)
{
cout<<"对不起,你无权使用!"<<endl;
}
};
void main()
{
int choice=1,telphoneid,guestid;
GDatabase GuestDB;
Guest *c;
TELDatabase TelphoneDB;
Telphone *tel;
int t ;
key();
while(choice!=0)
{ cout << "\t ━━━━━━━━超市管理系统━━━━━━━━━━"<<endl;
cout << "\t "<<endl;
cout << "\t 1: 申请电话 2: 退电话 3: 电话信息 "<<endl;
cout << "\t 4: 用户信息维护 0: 退出系统 "<<endl;
cout << "\t ━━━━━━━━━━━━━━━━━━━━━━━━"<<endl<<endl;
cout << "请输入你的选择:";
cin >> choice;
switch(choice)
{
case 1:
cout << "申请用户卡号:";
cin >> guestid;
cout << "电话编号:";
cin >> telphoneid;
c=GuestDB.query(guestid);
if (c==NULL)
{
cout << "该用户没有注册,不能打电话。" << endl;
break;
}
else
{
t = c->getapptelphone();
if (t > -1) //-1是位占用电话
{
cout<<"该用户已经占用一台电话。" << endl;
break;
}
}
tel=TelphoneDB.query(telphoneid);
if(tel==NULL)
{
cout << "该电话不存在" << endl;
break;
}
else if (tel->gettag() == 1)
{
cout << "该电话已占用" << endl;
break;
}
c->applytelphone(tel->getno());
tel->settag(1);
break;
case 2:
cout << "要退还的用户卡号:";
cin >> guestid;
c=GuestDB.query(guestid);
if(c==NULL)
{
cout << "不存在该用户!" << endl;
break;
}
else
{
t = c->getapptelphone();
if (t > -1)
{
tel = TelphoneDB.query(t);
if (tel != NULL)
{
tel->exittelphone();
c->exittelphone(tel->getno());
cout<<"退还成功"<<endl;
}
}
}
break;
case 3:
TelphoneDB.telphonedata();
break;
case 4:
GuestDB.guestdata(TelphoneDB);
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -