-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar.h
284 lines (255 loc) · 7.61 KB
/
car.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#ifndef CAR_H
#define CAR_H
#include<string>
#include<iostream>
#include<fstream>
#include<ctime>
using namespace std;
class Car{
public:
void addCar();//???????
void delCar();//???????
void findCar();//???????
void modCar();//????????
void timeAmount();//??????
void showInfor();//???????????????????
void saveInfor();//???????????????????
private:
string carNum;//???????
string carType;//???
string color; //???
time_t intTime; //??????
};
//增加车辆
void Car::addCar(){
time_t _time;//定义时间变量
while(1){
AA: cout<<"请输入车牌号 ";
cin>>carNum;
//判断车牌号是否重复
ifstream ifs("carData.txt",ios::in);//读取文件
if(ifs){//若文件读取成功,则判断车牌号是否重复
char buf[1024];
string strs[20];
int index=0;
while(!ifs.eof()){
ifs.getline(buf,100);//一行行读入数据
strs[index]=buf[0];//每次将第一个字段读入到str数组中
index++;
}
for(auto& num:strs){
if(num==carNum){
cout<<"车牌号重复!"<<endl;
goto AA;//跳转到车牌输入处
}
}
}
cout<<"请输入车辆种类:";
cin>>carType;
cout<<"请输入车的颜色:";
cin>>color;
intTime =time(&_time);//记录停车时间
saveInfor();
char ch;
cout<<"\t是否继续?(y/n)";
cin>>ch;
if(ch=='n'|| ch=='N')
break;
}
}
//删除车辆
void Car::delCar(){
ifstream carData("carData.txt",ios::in);
ofstream outData("tempcarData.txt",ios::out);
if(!carData||!outData){
cout<<"文件打开失败!"<<endl;
return ;
}
string carlisence,name,str;
bool flag=true;
cout<<"请输入要删除的车牌号:";
cin>>carlisence;
//将cardata的第一个字段读入到name里,>>遇到空格就停止读入
while(carData>>name){
getline(carData,str);
if(carlisence==name){
cout<<"要删除的车辆信息:"<<endl<<str<<endl;
flag=false;
break;
}
//若车牌号不相等,将信息写入到tempcarData.txt文件中
outData<<name<<" "<<str<<endl;
}
if(flag){
cout<<"该车牌号不存在"<<endl;
}
else{
while(getline(carData,str)){
outData<<str<<endl;
}
carData.close();
outData.close();
//下面再将tempcardata里的数据再次写入到cardata里面去
ifstream in("tempcarData.txt",ios::in);
ofstream out("carData.txt",ios::out);
if(!in||!out){
cout<<"文件打开失败!"<<endl;
return ;
}
else
while(getline(in,str)){
out<<str<<endl;
}
in.close();
out.close();
}
}
//查找车辆
void Car::findCar(){
ifstream carData("carData.txt",ios::in);
if(!carData){
cout<<"打开文件失败!"<<endl;
}
else{
string carlisence;
time_t _time,t1;
bool flag=true;
cout<<"请输入要查询的车牌号:";
cin>>carlisence;
while(carData>>carNum){
carData>>carType>>color>>intTime;
t1=time(&_time);
if(carlisence==carNum){
flag=false;
break;
}
}
if(flag)
cout<<"未查询到该车辆信息!"<<endl;
else{
cout<<"车牌号:"<<carNum<<" "<<"车辆类型:"<<carType<<" "<<"颜色:"<<color<<" "
<<"停车时长:"<<(t1-intTime)<<"秒"
<<" "<<"计费:"<<(t1-intTime)*0.005<<"元"<<endl;
}
}
carData.close();
}
//修改车辆信息
void Car::modCar(){
ifstream carData("carData.txt",ios::in);
ofstream outData("tempcarData.txt",ios::out);
if(!carData||!outData){
cout<<"文件打开失败!"<<endl;
return ;
}
string name,str;
bool flag=true;
cout<<"请输入要修改的车牌号:";
cin>>carNum;
//将cardata的第一个字段读入到name里,>>遇到空格就停止读入
while(carData>>name){
getline(carData,str);
if(carNum==name){
time_t _time;
cout<<"修改后的车牌号:";
cin>>carNum;
cout<<"修改后的车的种类";
cin>>carType;
cout<<"修改后的车的颜色";
cin>>color;
intTime =time(&_time);
outData<<carNum<<" "<<carType<<" "<<color<<" "<<intTime<<endl;
flag=false;
break;
}
//若车牌号不相等,将信息写入到tempcarData.txt文件中
outData<<name<<" "<<str<<endl;
}
if(flag){
cout<<"修改车辆信息不存在!"<<endl;
}
else{
while(getline(carData,str)){
outData<<str<<endl;
}
carData.close();
outData.close();
//下面再将tempcardata里的数据再次写入到cardata里面去
ifstream in("tempcarData.txt",ios::in);
ofstream out("carData.txt",ios::out);
if(!in||!out){
cout<<"文件打开失败!"<<endl;
return ;
}
else
while(getline(in,str)){
out<<str<<endl;
}
in.close();
out.close();
}
}
//显示所有车辆信息
void Car::showInfor(){
ifstream carData("carData.txt",ios::in);
if(!carData){
cout<<"打开文件失败!"<<endl;
return ;
}
else
{
bool flag=true;
time_t _time,t;
while(carData>>carNum){
carData>>carType>>color>>intTime;
t=time(&_time);
cout<<"车牌号:"<<carNum<<" "<<"车辆类型:"<<carType<<" "<<"颜色:"<<color<<" "
<<"停车时长:"<<(t-intTime)<<"秒"
<<" "<<"计费:"<<(t-intTime)*0.005<<"元"<<endl;
flag=false;
}
if(flag){
cout<<"\n无车辆信息"<<endl;
}
else{
cout<<"\n车辆信息已显示"<<endl;
}
}
carData.close();
}
//计算停车时长
void Car::timeAmount(){
ifstream carData("carData.txt",ios::in);
if(!carData){
cout<<"打开文件失败!"<<endl;
return ;
}
else{
int c1=0,c2=0;//c1:车辆总数,c2:不超过一天的车辆总数
time_t _time,t1;
while(carData>>carNum){
c1++;
t1=time(&_time);
carData>>carType>>color>>intTime;
if((t1-intTime)*0.0001<long(8.64))
c2++;
}
cout<<"车辆总数:"<<c1<<endl;
cout<<"停车时间不超过一天:"<<c2<<endl;
cout<<"停车时间超过一天:"<<c1-c2<<endl;
}
carData.close();
}
//保存车辆信息
void Car::saveInfor(){
ofstream outData("carData.txt",ios::app);
if(!outData){
cout<<"打开文件失败!"<<endl;
return ;
}
else{
outData<<carNum<<" "<<carType<<" "<<color<<" "<<intTime<<endl;
}
outData.close();
}
#endif