①://源.cpp
#include<iostream>
#include<string>
#include"User.h"
#include"SIZE.h"
#include"Manage.h"
#include"Time.h"
#define MAX 1000
using namespace std;
int main()
{
user b;
Manage c;
while (1)//界面菜单
{
A: cout << "**************欢迎进入XX停车场*****************" << endl;
cout << "* 请选择您的身份 *" << endl;
cout << "* 1、用户 2、管理员 0、退出程序 *" << endl;
cout << "***********************************************" << endl;
int choice;
cin >> choice;
switch (choice)
{
case 1:
{
if (b.size == MAX)//判断车位是否还有剩余
{
cout << "不好意思,该车库已满。请您寻找下一个停车点。谢谢";
exit(0);
}
else
{
cout << "**************************************************" << endl;
cout << "* 1、进入车库 2、离开车库 3、回到菜单 *" << endl;
cout << "**************************************************" << endl;
int choice;
cin >> choice;
switch (choice)
{
case 1: {
b.info();
goto A; };
break;
case 2:
b.delet();
break;
case 3:
goto A; break;
default:system("pause"),system("cls"); break;//输入错误,就按任意键继续
}
}
}break;
case 2: {
string mm;
cout << "请输入管理员密码:";
cin >> mm;
if (c._mm == mm)
{
c.show();
}
else {
cout << "密码错误!";
goto A;
}
}
break;
case 0:cout << "退出成功,欢迎您下次光临!"; exit(0);
default:
{
cout << "请输入正确选项!";
system("pause"), system("cls"); break;//输入错误,就按任意键继续
}
}
} return 0;
}
②://Time.h,Time这个类没有cPP
#pragma once
class classTime
{
public:
classTime() {P=0, r = 0, d = 0, h = 0, l = 0, s = 0; };
classTime(int p, int m, int n, int x, int e, int z) { P = p, r = m, d = n, h = x, l = e, s = z; };
friend int dTime(classTime& t1, classTime& t2);
friend int transeTime(classTime &t);
private:
int P,r, d, h, l, s;
};
int dTime(classTime& t1, classTime& t2)
{
int delta = transeTime(t2) - transeTime(t1);
return delta;
}
int transeTime(classTime& t)
{
return t.r * 365 * 24 * 60 * 60 + t.d * 24 * 60 * 60 + t.h * 60 * 60 + t.l * 60 + t.s;
}
③:User.h 以及User.cpp
#pragma once
#include<string>
#include"SIZE.h"
#define MAX 1000
using namespace std;
class user :public _SIZE
{
private:
string carno;
string phone;
public:
_SIZE num[MAX];
void info();//输入登记信息函数
void save();//保存信息函数
void delet();//离开车库函数
int isexist()//查找车牌号,看是否存在
{
string no;
cout << "请输入车牌号:";
for (int i = 0; i < this->size; i++)
{
if (this->num[i].Carno == no)
return i;
else
{
cout << "没有查询到该号码,请检查后重新输入。";
return -1;
}
}
}
void show();//显示信息函数
int fee(int);//计算费用函数
};
#include<iomanip>
#include<fstream>
#include<iostream>
#include"User.h"
#include<windows.h>
#include"SIZE.h"
#include"Time.h"
#define NAME "停车场系统.txt"
using namespace std;
void user::info()
{
SYSTEMTIME st;//定义本地时间变量,该变量为结构体
GetLocalTime(&st);//获取本地时间函数,参数为时间变量指针
cout << "请输入您的车牌号:" << endl;
cin >> carno;
this->num[this->size].Carno = carno;
cout << "请输入您的有效联系方式:" << endl;
cin >> phone;
this->num[this->size].Phone = phone;
B:cout << "-------------------------------" << endl;
cout << "-- 请选择您停车的方式 --" << endl;
cout << "-- 1、临时停车 2、长期停车 --" << endl;
int style_choice;
switch (style_choice)
{
case 1:this->num[this->size].Type ="临时"; break;
case 2:this->num[this->size].Type ="长期"; break;
default:cout << "请重新输入!"; goto B; break;
}
this->num[this->size].time[0] = st.wYear;
this->num[this->size].time[1] = st.wMonth;
this->num[this->size].time[2] = st.wDay;
this->num[this->size].time[3] = st.wHour;
this->num[this->size].time[4] = st.wMinute;
this->num[this->size].time[5] = st.wSecond;
this->size++;
this->isFileempty = false;//表示当前停车场不为空
cout << "登记成功!" << endl;
this->save();//将信息写入文件
//按任意键继续后清屏
system("pause");
system("cls");
}
void user::save()//写文件登记的用户信息
{
ofstream ofs;
ofs.open(NAME, ios::out);
for (int i = 0; i < this->size; i++)
{
ofs << this->num[i].Carno << " " << this->num[i].Phone << " " << this->num[i].Type << " "<< this->num[i].time[0]<<"年"<< this->num[i].time[1]<<"月"<<this->num[i].time[2]
<<"日"<< this->num[i].time[3]<<"时"<< this->num[i].time[4]<<"分"<< this->num[i].time[5]<<"秒"<<endl;
}
ofs.close();
}
void user::delet()//删除信息
{
int b = isexist();
double c=-1;//标志停车类型
if (num[b].Type == "临时")
c = 1;
else c = 0.5;
if (b)
{
for (int i = b; i < this->size; i++)
{
this->num[i] = this->num[i + 1];
}
this->size--;//数量减少
}
int b=fee(b);
cout << "您的停车时长为:" << b << "秒," << "应当缴纳费用为:" << c * b<<endl;
this->save();//更新删除后的名单
}
void user::show()
{
if (this->isFileempty)
{
cout << "当前没有用户信息!" << endl;
}
else {
for (int i = 0; i < this->size; i++)
{
cout << "车牌:" << num[i].Carno << " " << "电话:" << num[i].Phone << " " << "类型:" << num[i].Type << " " << "停车时间:" << num[i].time[0] << "年" << num[i].time[1] << "月" << num[i].time[2] << "日" << num[i].time[3] << "时" << num[i].time[4] << "分" << num[i].time[5] << "秒" << endl;
}
//按任意键后清屏
system("pause");
system("cls");
}
}
int user::fee(int a)
{
int _time[6] = { 0 };//存放出去时间的数组
SYSTEMTIME st;//定义本地时间变量,该变量为结构体
GetLocalTime(&st);//获取本地时间函数,参数为时间变量指针
_time[0] = st.wYear;
_time[1] = st.wMonth;
_time[2] = st.wDay;
_time[3] = st.wHour;
_time[4] = st.wMinute;
_time[5] = st.wSecond;
classTime t1(num[a].time[0], num[a].time[1], num[a].time[2], num[a].time[3], num[a].time[4], num[a].time[5]);
classTime t2(_time[0], _time[1], _time[2], _time[3], _time[4], _time[5]);
return dTime(t1, t2);
}
④:Manage.h Manage.cpp
#pragma once
#include<iostream>
#include"SIZE.h"
#include"User.h"
#include<string>
using namespace std;
class Manage:public user
{
public:
void show();
string _mm = "1234567";
//改变用户信息
void Mchange();
};
#include<iostream>
#include"Manage.h"
#include"User.h"
using namespace std;
void Manage::show()
{
user a;
A:cout << "****************************************************************************" << endl;
cout << "* 1、查看用户信息 2、查找用户 3、删除用户 4、修改信息 5、添加用户 0、退出*" << endl;
cout << "****************************************************************************" << endl;
int choice;
cin >> choice;
switch (choice)
{
case 1:
a.show(); break;//展示信息
case 2://查找用户信息
{
int is = a.isexist();
if (!is)
{
cout << "为找到该车牌号,请重新输入!" << endl;
}
else
{
cout << "车牌:" << num[is].Carno << " " << "电话:" << num[is].Phone << " " << "类型:" << num[is].Type << " " << "停车时间:" << num[is].time[0] << "年" << num[is].time[1] << "月" << num[is].time[2] << "日" << num[is].time[3] << "时" << num[is].time[4] << "分" << num[is].time[5] << "秒" << endl;
//按任意键后清屏
system("pause");
system("cls");
}
break;
}
case 3://删除用户函数
{
a.delet();
cout << "删除成功!";
break;
}
case 4:Mchange(); break;//修改信息函数
case 5://添加用户函数
a.info();
break;
case 0:exit(0); break;//退出程序函数
default: {
cout << "请输入正确选项!"; goto A; break;
}
}
}
void Manage::Mchange()
{
int b=isexist();
cout << "请选择要修改的信息:" << endl;
cout << "1:类型 2:号码 0:返回" << endl;
int choice;
cin >> choice;
switch (choice)
{
case 1:
{
A:cout << "请选择类型:(1:临时。2:长期)";
int choice;
cin >> choice;
if (choice = 1)
{
num[b].Type = "临时";
cout << "修改成功!"; this->save();
break;
}
else if (choice = 2)
{
num[b].Type = "长期";
cout << "修改成功!"; this->save(); break;
}
else cout << "请输入正确选项!"; goto A;
}break;
case 2:
{
cout << "请输入要修改的号码:";
string a;
cin >> a;
num[b].Phone = a;
this->save();
cout << "修改成功!";
}break;
case 0:exit(0); break;
default:system("pause"), system("cls");
break;
}
}
⑤:SIZE.h SIZE.cpp
#pragma once
#include<string>
using namespace std;
#define MAX 1000//该变量就是停车场的最大容量
class _SIZE
{
public:
_SIZE ();
string Type;//用户类型
string Carno=NULL;//车牌号
string Phone=NULL;//电话号码
int time[6] = { 0 };//登记时间
int size=0;//车位占有数量
//判断文件是否为空
bool isFileempty;
//记录文件中用户的数量
int get_size();
};
#include<iostream>
#include"SIZE.h"
#include<fstream>
#include<string>
#define NAME "停车场系统.txt"
using namespace std;
_SIZE::_SIZE()
{
ifstream ifs;
ifs.open(NAME, ios::in);
if (!ifs.is_open())//当文件不存在
{
cout << "停车场系统名单文件不存在!" << endl;
this->size = 0;
this->isFileempty = true;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())//文件存在,但是里面没有数据
{
cout << "停车场系统名单文件为空!" << endl;
this->size = 0;
this->isFileempty = true;
ifs.close();
return;
}
//文件存在,并且写有数据
int sum = this->get_size();
cout << "当前停车场停放车辆的数量为:" << sum << endl;
this->size = sum;
}
int _SIZE::get_size()
{
ifstream ifs;
ifs.open(NAME,ios::in);
int num = 0;
string no, call, Type;
int Time[6];
while (ifs >> no && ifs >> call && ifs >> Type && ifs >> Time[0] && ifs >> Time[1] && ifs >> Time[2] && ifs >> Time[3] && ifs >> Time[4] && ifs >> Time[5])
{
num++;//一行一行的统计数量
}
return num;
}
这是一些代码,不知道要不要放完整,就放了几张
1.添加一个Time.cpp文件,把Time.h中的两个友元函数的实现放在Time.cpp中
#include "Time.h"
int dTime(classTime& t1, classTime& t2)
{
int delta = transeTime(t2) - transeTime(t1);
return delta;
}
int transeTime(classTime& t)
{
return t.r * 365 * 24 * 60 * 60 + t.d * 24 * 60 * 60 + t.h * 60 * 60 + t.l * 60 + t.s;
}
2.SIZE.h文件中,成员变量的初始化放在cpp文件中,不要在.h中初始化,SIZE.h修改如下
#pragma once
#include<string>
using namespace std;
#define MAX 1000//该变量就是停车场的最大容量
class _SIZE
{
public:
_SIZE ();
string Type;//用户类型
string Carno;//车牌号
string Phone;//电话号码
int time[6];//登记时间
int size;//车位占有数量
//判断文件是否为空
bool isFileempty;
//记录文件中用户的数量
int get_size();
};
SIZE.cpp中,构造函数修改:
_SIZE::_SIZE()
{
//初始化放在开头
Carno="";//车牌号
Phone="";//电话号码
memset(time,0,6);
size=0;//车位占有数量
//后面的不变。省略不写了
ifstream ifs;
ifs.open(NAME, ios::in);
/* ... */
}
3.Manage.h中的string _mm = "1234567";
初始化放在Manage.cpp的构造函数中
代码都贴出来吧,方便复制,帮你看一下,图片里看不到错误信息
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632