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

一、简介      

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

运用软件:Visual Studio   (VS)。

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

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

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

流程图如下图所示。

 


 二、系统设计思路

 1 void mainspace();//主界面,功能界面
 2 
 3 void stop();//停车子函数
 4 
 5 void move();//取车子函数
 6 
 7 void check();//检查当前库存情况子函数
 8 
 9 int money(int n);//计时收费子函数
10 
11 void code();//密码登陆子函数
12 
13 string hide(int size);//输入密码输出*子函数     

 

 

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

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

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

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

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

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

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

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

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

三、函数功能实现

  1 #include <iostream>
  2 #include <ctime>
  3 #include <string>
  4 #include <stdio.h>
  5 #include <conio.h>
  6 #include <cstdlib>
  7 #include <windows.h>
  8 #include <iomanip>
  9 #include <fstream>
 10 using namespace std;
 11 void mainspace();//主界面,功能界面
 12 void stop();//停车子函数
 13 void move();//取车子函数
 14 void check();//检查当前库存情况子函数
 15 int money(int n);//计时收费子函数
 16 void code();//密码登陆子函数
 17 string hide(int size);//输入密码输出*子函数
 18 int park[10] = { 1,1 };//停车位序号,此时txt里预设了两组数据
 19 struct pay //返回自定义值
 20 {
 21     int timemin;
 22     int money;
 23 }pay1;
 24 int main()
 25 {
 26     SetConsoleTitle("Stop Cars");//标题
 27     while (1) {
 28         time_t now = time(0);
 29         tm *ltm = localtime(&now);
 30         cout << 1900 + ltm->tm_year << "." << 1 + ltm->tm_mon << "." << ltm ->tm_mday << endl;
 31         cout << ltm->tm_hour << ":" << ltm->tm_min << endl;//输出时间
 32         code();
 33         mainspace();
 34         Sleep(60000);//一段时间不操作返回初始界面
 35         system("cls");
 36     }
 37 }
 38 void mainspace()
 39 {
 40     cout << "功能" << endl;
 41     cout << "1.停车" << endl;
 42     cout << "2.取车" << endl;
 43     cout << "3.查询" << endl;
 44     int x;
 45     cin >> x;
 46     switch (x)
 47     {
 48     case 1:stop(); break;
 49     case 2:move(); break;
 50     case 3:check(); break;
 51     }
 52 }
 53 void code()
 54 {
 55     string name = "linuas";
 56     string pw = "111111";
 57     string name1;
 58     string code;
 59     while (1)
 60     {
 61         cout << "请输入用户名:" << endl;
 62         cin >> name1;
 63         if (strcmp(name1.c_str(), name.c_str()) == 0)
 64         {
 65             system("cls");
 66             cout << "请输入密码:" << endl;
 67             code = hide(7);
 68             if (strcmp(pw.c_str(), code.c_str()) == 0)
 69             {
 70                 system("cls");
 71                 cout << "登陆成功!" << endl;
 72                 Sleep(2000);
 73                 break;
 74             }
 75             else cout << endl << "密码错误!" << endl;
 76             Sleep(2000);
 77             system("cls");
 78             continue;
 79         }
 80         else cout << endl << "用户名不存在!" << endl;
 81         Sleep(2000);
 82         system("cls");
 83         continue;
 84     }
 85 }
 86 string hide(int size)
 87 {
 88     char c;
 89     int count = 0;
 90     char *password = new char[size]; // 动态申请空间
 91     string str;
 92     while ((c = _getch()) != '\r') {
 93 
 94         if (c == 8) { // 退格
 95             if (count == 0) {
 96                 continue;
 97             }
 98             putchar('\b'); // 回退一格
 99             putchar(' '); // 输出一个空格将原来的*隐藏
100             putchar('\b'); // 再回退一格等待输入
101             count--;
102         }
103         if (count == size - 1) { // 最大长度为size-1
104             continue;
105         }
106         if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <=            '9')) {  // 密码只可包含数字和字母
107             putchar('*');  // 接收到一个字符后, 打印一个*
108             password[count] = c;
109             count++;
110         }
111     }
112     password[count] = '\0';
113     str = password;
114     return str;
115 }
116 void stop()
117 {
118     int n = 0;
119     int i = 0;
120     string str;
121     time_t now = time(0);
122     tm *ltm = localtime(&now);//获取当前系统时间准备写入
123     cout << "请输入车牌号:" << endl;
124     cin >> str;
125     for (i = 0; i < 10; i++)//车位序号存在与否判定
126     {
127         if (park[i] != 1)
128         {
129             n = i;
130             break;
131         }
132         else continue;
133     }
134     park[n] = 1;
135     ofstream outfile("C:\\Users\\Linuas\\Desktop\\demo.txt", ios::app);//写入文件式打开
136     outfile << endl << n << " " << str << " " << (1900 + ltm->tm_year) * 10000 + (1 + ltm->tm_mon) * 100 +ltm->tm_mday << " " << (ltm->tm_hour) * 100 + ltm->tm_min;
137     outfile.close();//写入成功后关闭文件,切记!
138     cout << "停车成功" << endl;
139     Sleep(2000);
140     mainspace();
141 }
142 void check()
143 {
144     ifstream myfile("C:\\Users\\Linuas\\Desktop\\demo.txt");
145     string temp;
146     cout << "车位 " << "车牌号 " << "停车时间" << endl;
147     while (getline(myfile, temp))
148     {
149         cout << temp << endl;
150     }
151     myfile.close();
152     mainspace();
153 }
154 void move()
155 {
156     ifstream file("C:\\Users\\Linuas\\Desktop\\demo.txt");
157     string line;
158     int m, n, count = 0;
159     ofstream outfile("test2.txt", ios::out | ios::trunc);
160     cout << "您停车的车位是:" << endl;
161     cin >> m;
162     n=money(m)+1;
163     n = m + 1;
164     while (!file.eof()) {
165         getline(file, line);
166         if (count != n - 1)//保留行
167             outfile << line << endl;
168         count++;
169     }
170     outfile.close();
171     file.close();
172     ofstream outfile1("C:\\Users\\Linuas\\Desktop\\demo.txt", ios::out | ios::trunc);
173     fstream file1("test2.txt");
174     while (!file1.eof()) {
175         getline(file1, line);
176         outfile1 << line << endl;
177     }
178     outfile1.close();
179     file1.close();
180     system("del test2.txt");//删除中间文件
181     Sleep(2000);
182     mainspace();
183 }
184 int money(int n)
185 {
186     fstream infile;
187     infile.open("C:\\Users\\Linuas\\Desktop\\demo.txt", ios::in);
188     string line;
189     int count = 0;
190     int a, b, c, d;
191     int b1, b2, c1, c2;
192     if (n == 0) { infile >> a >> line >> b >> c; }
193     else
194     {
195         while (!infile.eof()) {
196             getline(infile, line);//自动回车了一次
197             if (count == n - 1)
198             {
199                 infile >> a >> line >> b >> c;
200             }
201             count++;
202         }
203     }
204     infile.close();
205     d = b % 10000;
206     b1 = d % 100;
207     b2 = (d - b1) / 100;
208     c1 = c % 100;
209     c2 = (c - c1) / 100;
210     time_t now = time(0);
211     tm *ltm = localtime(&now);
212     pay1.timemin = (1 + ltm->tm_mon - b2) * 31 * 24 * 60 + (ltm->tm_mday - b1) * 24 * 60 + (ltm->tm_hour -        c2) * 60 + ltm->tm_min - c1;
213     if (pay1.timemin <= 15)
214     {
215         pay1.money = 10;
216     }
217     else if (pay1.timemin <= 60)
218     {
219         pay1.money = 10 + pay1.timemin - 15;
220     }
221     else pay1.money = 60;
222     cout << "停车时间为" << pay1.timemin << "分钟" << endl;
223     cout << "计时收费为" << pay1.money << "" << endl;
224     Sleep(2000);
225     return n;
226 }

其中,

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分。

四、程序运行图示

登陆界面。

密码不可视输入。

主界面。

 停车指令。

查询现在停车场的情况。

此时txt文件情况。

 

 

取车计算时间与收费。

取车完后删除那一行信息。

五、总结与分享

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

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

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

 

posted @ 2018-08-28 11:48  会武术之白猫  阅读(3056)  评论(1编辑  收藏  举报