C++进阶训练——停车收费系统设计

时间:2023-03-09 07:56:05
C++进阶训练——停车收费系统设计

一、简介

经过一段时间的c++基础学习,是时候做一个较为全面的、运用c++功能的较复杂的项目练练手了。

运用软件:Visual Studio   (VS)。

题目:c++停车收费系统设计(某本编程书进阶习题)

要求: 管理员登陆; 有停车、取车、查询实时停车情况三个功能; 按照停车后时间分段计时收费。

分析: 管理员登陆需要密码的不可视输入,即输入密码输出“*”,输入错误用户名或者错误密码要报错并且返回初始界面; 三个功能要基于写入txt文件来实行,其中停车写入信息,取车读取信息并初始化信息,查询输出文件信息; 通过写入文件的停车时间和取车时时间来计算停车分钟,然后分段计时收费; 主界面需要实时显示系统时间,并且在一段时间没有操作后返回初始界面,标题要自定义为c++停车系统; 注意输出格式; 文件信息应包括车位序号、车牌号、入库时间(按指定位数输出,如201808041128方便保存),停车时优先存在车位序号小的车位。

流程图如下图所示。

C++进阶训练——停车收费系统设计

二、系统设计思路

 void mainspace();//主界面,功能界面

 void stop();//停车子函数

 void move();//取车子函数

 void check();//检查当前库存情况子函数

 int money(int n);//计时收费子函数

 void code();//密码登陆子函数

 string hide(int size);//输入密码输出*子函数     

根据流程图大致需要以上几个子函数,其中登陆界面包含code()和hide()两个函数,主界面为mainspace(),三个操作功能一一对应一个子函数,其中取车多包含money()计时收费功能。

根据分析,这些函数应分别实现以下功能:

code():密码登陆界面,用户名与密码预先设置好,若输入错误则报错并返回登陆界面,成功则进入主界面

hide():输入一个字符时在操作台上不显示本来字符,并输出一个“*”

mainspace():输出三个功能选择,跳转页面

stop():将停车的三个信息(车位序号,车牌号,停车时间)写入txt文件,车位序号小数字优先,其中停车时间由读取当前系统时间进行处理

move():将选择的车位序号的那一行信息删除

check():将txt文件内容全部输出

money():读取选择的车位序号的那一行信息,并处理计算得出停车时间并计时收费

三、函数功能实现

 #include <iostream>
#include <ctime>
#include <string>
#include <stdio.h>
#include <conio.h>
#include <cstdlib>
#include <windows.h>
#include <iomanip>
#include <fstream>
using namespace std;
void mainspace();//主界面,功能界面
void stop();//停车子函数
void move();//取车子函数
void check();//检查当前库存情况子函数
int money(int n);//计时收费子函数
void code();//密码登陆子函数
string hide(int size);//输入密码输出*子函数
int park[] = { , };//停车位序号,此时txt里预设了两组数据
struct pay //返回自定义值
{
int timemin;
int money;
}pay1;
int main()
{
SetConsoleTitle("Stop Cars");//标题
while () {
time_t now = time();
tm *ltm = localtime(&now);
cout << + ltm->tm_year << "." << + ltm->tm_mon << "." << ltm ->tm_mday << endl;
cout << ltm->tm_hour << ":" << ltm->tm_min << endl;//输出时间
code();
mainspace();
Sleep();//一段时间不操作返回初始界面
system("cls");
}
}
void mainspace()
{
cout << "功能" << endl;
cout << "1.停车" << endl;
cout << "2.取车" << endl;
cout << "3.查询" << endl;
int x;
cin >> x;
switch (x)
{
case :stop(); break;
case :move(); break;
case :check(); break;
}
}
void code()
{
string name = "linuas";
string pw = "";
string name1;
string code;
while ()
{
cout << "请输入用户名:" << endl;
cin >> name1;
if (strcmp(name1.c_str(), name.c_str()) == )
{
system("cls");
cout << "请输入密码:" << endl;
code = hide();
if (strcmp(pw.c_str(), code.c_str()) == )
{
system("cls");
cout << "登陆成功!" << endl;
Sleep();
break;
}
else cout << endl << "密码错误!" << endl;
Sleep();
system("cls");
continue;
}
else cout << endl << "用户名不存在!" << endl;
Sleep();
system("cls");
continue;
}
}
string hide(int size)
{
char c;
int count = ;
char *password = new char[size]; // 动态申请空间
string str;
while ((c = _getch()) != '\r') { if (c == ) { // 退格
if (count == ) {
continue;
}
putchar('\b'); // 回退一格
putchar(' '); // 输出一个空格将原来的*隐藏
putchar('\b'); // 再回退一格等待输入
count--;
}
if (count == size - ) { // 最大长度为size-1
continue;
}
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '' && c <= '')) { // 密码只可包含数字和字母
putchar('*'); // 接收到一个字符后, 打印一个*
password[count] = c;
count++;
}
}
password[count] = '\0';
str = password;
return str;
}
void stop()
{
int n = ;
int i = ;
string str;
time_t now = time();
tm *ltm = localtime(&now);//获取当前系统时间准备写入
cout << "请输入车牌号:" << endl;
cin >> str;
for (i = ; i < ; i++)//车位序号存在与否判定
{
if (park[i] != )
{
n = i;
break;
}
else continue;
}
park[n] = ;
ofstream outfile("C:\\Users\\Linuas\\Desktop\\demo.txt", ios::app);//写入文件式打开
outfile << endl << n << " " << str << " " << ( + ltm->tm_year) * + ( + ltm->tm_mon) * +ltm->tm_mday << " " << (ltm->tm_hour) * + ltm->tm_min;
outfile.close();//写入成功后关闭文件,切记!
cout << "停车成功" << endl;
Sleep();
mainspace();
}
void check()
{
ifstream myfile("C:\\Users\\Linuas\\Desktop\\demo.txt");
string temp;
cout << "车位 " << "车牌号 " << "停车时间" << endl;
while (getline(myfile, temp))
{
cout << temp << endl;
}
myfile.close();
mainspace();
}
void move()
{
ifstream file("C:\\Users\\Linuas\\Desktop\\demo.txt");
string line;
int m, n, count = ;
ofstream outfile("test2.txt", ios::out | ios::trunc);
cout << "您停车的车位是:" << endl;
cin >> m;
n=money(m)+;
n = m + ;
while (!file.eof()) {
getline(file, line);
if (count != n - )//保留行
outfile << line << endl;
count++;
}
outfile.close();
file.close();
ofstream outfile1("C:\\Users\\Linuas\\Desktop\\demo.txt", ios::out | ios::trunc);
fstream file1("test2.txt");
while (!file1.eof()) {
getline(file1, line);
outfile1 << line << endl;
}
outfile1.close();
file1.close();
system("del test2.txt");//删除中间文件
Sleep();
mainspace();
}
int money(int n)
{
fstream infile;
infile.open("C:\\Users\\Linuas\\Desktop\\demo.txt", ios::in);
string line;
int count = ;
int a, b, c, d;
int b1, b2, c1, c2;
if (n == ) { infile >> a >> line >> b >> c; }
else
{
while (!infile.eof()) {
getline(infile, line);//自动回车了一次
if (count == n - )
{
infile >> a >> line >> b >> c;
}
count++;
}
}
infile.close();
d = b % ;
b1 = d % ;
b2 = (d - b1) / ;
c1 = c % ;
c2 = (c - c1) / ;
time_t now = time();
tm *ltm = localtime(&now);
pay1.timemin = ( + ltm->tm_mon - b2) * * * + (ltm->tm_mday - b1) * * + (ltm->tm_hour - c2) * + ltm->tm_min - c1;
if (pay1.timemin <= )
{
pay1.money = ;
}
else if (pay1.timemin <= )
{
pay1.money = + pay1.timemin - ;
}
else pay1.money = ;
cout << "停车时间为" << pay1.timemin << "分钟" << endl;
cout << "计时收费为" << pay1.money << "元" << endl;
Sleep();
return n;
}

其中,

str1=str2 即字符串相等时,strcmp(str1.c_str(),str2.c_str())=0。 因为 strcmp 的参数应为 char ,因此 string 定义的参数要加 .c_str(),如上所示。  c++定义字符时,用 string 比用 char 数组要简便的多得多。

读写文件的三个格式:ifstream 只读,ofstream 只写,fstream 打开; 读取和写入指令:infile 读取,outfile 写入;其中 ios::app 表示从文件的末尾开始写入。
在车位序号的循环中,推荐使用 for 循环而不是 while 减少崩溃几率。
这里注意a=0时,if(a) 假,if(!a) 真,if(a=0) 假,if(a==0) 真,if(a!=0) 假的关系。
切记,打开文件执行完操作后务必关闭文件,否则会出现不可名状的错误!
move()这个函数的关键就是删除 txt 文件中的指定行,而c++是没有这样的指令的。因此我们选择曲线实现这个功能:先复制除了将要删除那一行的数据到一个临时文件,再清空原文件,之后将临时文件的数据复制到原文件并删除临时文件即可实现这个功能。
money()这个函数要实现读取特定行的 int 型内容并计算的功能,用 getline 的特性来获取定位行的功能; 然后就是存在文件的时间与当前系统时间进行计算,如201808061123分解成2018年8月6日11时23分。

四、程序运行图示

C++进阶训练——停车收费系统设计登陆界面。

C++进阶训练——停车收费系统设计密码不可视输入。

C++进阶训练——停车收费系统设计主界面。

C++进阶训练——停车收费系统设计停车指令。

C++进阶训练——停车收费系统设计查询现在停车场的情况。

C++进阶训练——停车收费系统设计此时txt文件情况。

C++进阶训练——停车收费系统设计取车计算时间与收费。

C++进阶训练——停车收费系统设计取车完后删除那一行信息。

五、总结与分享

总的来说,这个项目还算有些内容,是即将开始的下一个项目的很好的台阶。希望大家一起学习,在代码学习的道路上越走越远,头发越掉越少!

下个项目,某为校招笔试题。

作者:会武术的白猫
https://www.bilibili.com/read/cv899072
出处: bilibili