...
1 #include <iostream>
2 #include <iomanip>
3 #include <fstream>
4 #include <string>
5 #include <list>
6 using namespace std;
7
8 class Violation{
9 public:
10 Violation(){points=fine=0;
11 number=carNumber=tel=location=isSpot
12 =police=camera=type=isTreated=treatmentDate=notes="";}
13 Violation(string snumber,string sdate="",string scarNumber="",string stel="",
14 string slocation="",int ipoints=-1,string sisSpot="",
15 string spolice="",string scamera="",string itype="",int ifine=0,
16 string sisTreated="",string streatmentDate="",string snotes="")
17 :number(snumber),date(sdate),carNumber(scarNumber),tel(stel),
18 location(slocation),points(ipoints),isSpot(sisSpot),police(spolice),
19 camera(scamera),type(itype),fine(ifine),isTreated(sisTreated),
20 treatmentDate(streatmentDate),notes(snotes){}
21
22 bool operator==(const Violation& vltn); //查找用
23 friend bool compNumber(Violation& vltn1,Violation& vltn2); //編號比較規則
24 friend bool compDate(Violation& vltn1,Violation& vltn2); //日期比較規則
25 friend bool compCarNumber(Violation& vltn1,Violation& vltn2); //車牌號比較規則
26 friend bool compLocation(Violation& vltn1,Violation& vltn); //地點比較規則
27 friend bool compPoints(Violation& vltn1,Violation& vltn); //扣分比較規則
28 friend istream& operator>>(istream& input,Violation& vltn);
29 friend ostream& operator<<(ostream& output,Violation& vltn);
30 friend class List;
31 friend int saveFile();
32 private:
33 string number; //編號
34 string date; //日期
35 string carNumber; //車牌號
36 string tel; //聯繫電話-
37
38 string location; //地點
39 int points; //扣分
40 string isSpot; //現場
41 string police; //現場執勤交警
42
43 string camera; //非現場攝像頭編號
44 string type; //違章類別
45 int fine; //罰款
46 string isTreated; //是否處理
47
48 string treatmentDate; //處理日期
49 string notes; //備註
50 };
51
52 bool Violation::operator==(const Violation& vltn){
53 if(location==""||vltn.location=="")
54 if(tel==""||vltn.tel=="")
55 if(carNumber==""||vltn.carNumber=="")
56 if(date==""||vltn.date=="")
57 return number==vltn.number;
58 else return date==vltn.date;
59 else return carNumber==vltn.carNumber;
60 else return tel==vltn.tel;
61 else return location==vltn.location;
62 }
63
64 bool compNumber(Violation& vltn1,Violation& vltn2){
65 return vltn1.number>vltn2.number;
66 }
67
68 bool compDate(Violation& vltn1,Violation& vltn2){
69 return vltn1.date>vltn2.date;
70 }
71
72 bool compCarNumber(Violation& vltn1,Violation& vltn2){
73 return vltn1.carNumber<vltn2.carNumber;
74 }
75
76 bool compLocation(Violation& vltn1,Violation& vltn2){
77 return vltn1.location>vltn2.location;
78 }
79
80 bool compPoints(Violation& vltn1,Violation& vltn2){
81 return vltn1.points>vltn2.points;
82 }
83
84 istream& operator>>(istream& input,Violation& vltn){
85 input>>vltn.number>>vltn.date>>vltn.carNumber>>vltn.tel>>vltn.location
86 >>vltn.points>>vltn.isSpot>>vltn.police>>vltn.camera>>vltn.type
87 >>vltn.fine>>vltn.isTreated>>vltn.treatmentDate>>vltn.notes;
88 return input;
89 }
90
91 ostream& operator<<(ostream& output,Violation& vltn){
92 output<<setw(6)<<vltn.number
93 <<setw(9)<<vltn.date
94 <<setw(9)<<vltn.carNumber
95 <<setw(12)<<vltn.tel
96 <<setw(11)<<vltn.location
97 <<setw(5)<<vltn.points
98 <<setw(9)<<vltn.isSpot
99 <<setw(13)<<vltn.police
100 <<setw(17)<<vltn.camera
101 <<setw(11)<<vltn.type
102 <<setw(5)<<vltn.fine
103 <<setw(9)<<vltn.isTreated
104 <<setw(9)<<vltn.treatmentDate
105 <<setw(33)<<vltn.notes<<endl;
106 return output;
107 }
108
109 class List{
110 public:
111 friend class User;
112 int loadFile(); //讀取文件
113 int saveFile(); //保存到文件
114 int insert(); //添加記錄
115 int change(); //修改記錄
116 int del(); //刪除記錄
117 int browse(); //瀏覽記錄
118
119 int searchMenu(); //查詢信息
120 int searchCarNumber(); //按車牌號
121 int searchDate(); //按日期查找
122 int searchTel(); //按電話查找
123 int searchLocation(); //按地點查詢
124
125 int countMenu(); //統計信息
126 int countDate(); //按日期統計違章
127 int countCarNumber(); //按車牌統計違章
128 int countLocation(); //按地點統計違章
129 int countPoints(); //按扣分統計違章
130 static List* getList();
131 private:
132 list <Violation> vltnlist;
133 static List* m_list;
134 List(){};
135 List(const List&){}
136 List& operator=(const List&){}
137 };
138
139 const char* filepath="violation.dat"; //指定數據文件路徑
140
141 bool checkNumber(string s,int size){
142 if(s.size()!=size) return false;
143 for(int i=0;i<size;i++)
144 if(s[i]<'0'||s[i]>'9') return false;
145 return true;
146 }
147
148 List* List::m_list=NULL;
149 List* List::getList(){
150 if(NULL==m_list) m_list=new List;
151 return m_list;
152 }
153
154 int List::loadFile(){
155 ifstream infile(filepath,ios::in);
156 if(infile)
157 for(;!infile.eof();){
158 Violation vltn;infile>>vltn;
159 vltnlist.push_back(vltn);
160 }
161 infile.close();
162 return 0;
163 }
164
165 int List::saveFile(){
166 ofstream outfile(filepath,ios::out);
167 list<Violation>::iterator it;
168 for(it=vltnlist.begin();it!=vltnlist.end();it++)
169 outfile<<endl<<it->number<<' '<<it->date<<' '<<it->carNumber<<' '
170 <<it->tel<<' '<<it->location<<' '<<it->points<<' '<<it->isSpot<<' '
171 <<it->police<<' '<<it->camera<<' '<<it->type<<' '<<it->fine<<' '
172 <<it->isTreated<<' '<<it->treatmentDate<<' '<<it->notes;
173 outfile.close();
174 return 0;
175 }
176
177 int List::insert(){
178 list<Violation>::iterator it;
179 string number,date,carNumber,tel,location,isSpot,police,camera,type,
180 isTreated,treatmentDate,notes;
181 int points,fine;
182 cout<<"請輸入違章編號【放棄添加 0】【5位數字,如16384】:";
183 for(;;){
184 cin>>number;cin.sync();
185 if(number=="0") return 0;
186 if(!checkNumber(number,5)) {
187 cout<<"請輸入有效編號:";
188 continue;
189 }
190 Violation l(number);
191 it=find(vltnlist.begin(),vltnlist.end(),l);
192 if(it!=vltnlist.end())
193 cout<<"編號已存在,請重新輸入:";
194 else break;
195 }
196 cout<<"請輸入違章日期【8位數字,如20160630】:";
197 while(1){
198 cin>>date;cin.sync();
199 if(!checkNumber(date,8)){
200 cout<<"請輸入有效日期:";
201 continue;
202 }
203 else break;
204 }
205 cout<<"請輸入違章車輛車牌號:";cin>>carNumber;cin.sync();
206 cout<<"請輸入登記聯繫電話【11位數字,如13751516464】:";
207 while(1){
208 cin>>tel;cin.sync();
209 if(!checkNumber(tel,11)){
210 cout<<"請輸入有效電話:";
211 continue;
212 }
213 else break;
214 }
215 cout<<"請輸入違章地點:";cin>>location;cin.sync();
216 cout<<"請輸入處罰扣分【0~12分】:";
217 while(1){
218 if(cin>>points){
219 cin.sync();
220 if(points<0||points>12){
221 cout<<"請輸入有效扣分:";
222 continue;
223 }
224 else break;
225 }
226 else{
227 cin.clear();cin.sync();
228 cout<<"請輸入有效扣分:";
229 continue;
230 }
231 }
232 cout<<"請輸入是否現場【是 1】:";cin>>isSpot;cin.sync();
233 if(isSpot=="1"){
234 cout<<"請輸入現場執勤交警:";cin>>police;cin.sync();
235 isSpot="是";camera="現場";
236 }
237 else{
238 cout<<"請輸入非現場攝像頭編號【6位數字,如646464】:";
239 while(1){
240 cin>>camera;cin.sync();
241 if(!checkNumber(camera,6)){
242 cout<<"請輸入有效編號:";
243 continue;
244 }
245 else break;
246 }
247 isSpot="否";police="非現場";
248 }
249 cout<<"*******************************************************"<<endl;
250 cout<<"* ┌------------違章類別-------------請輸入---------┐*"<<endl;
251 cout<<"* | | *"<<endl;
252 cout<<"* | ◆ 超速 1 | *"<<endl;
253 cout<<"* | ◆ 酒駕 2 | *"<<endl;
254 cout<<"* | ◆ 闖紅燈 3 | *"<<endl;
255 cout<<"* | ◆ 違規變道 4 | *"<<endl;
256 cout<<"* | ◆ 未系安全帶 5 | *"<<endl;
257 cout<<"* | ◆ 超載 6 | *"<<endl;
258 cout<<"* | | *"<<endl;
259 cout<<"* └------------------------------------------------┘*"<<endl;
260 cout<<"*******************************************************"<<endl;
261 cout<<"請選擇違章類別:";
262 int Mark=1;
263 while(Mark){
264 char n;cin>>n;cin.sync();
265 switch(n){
266 case '1': type="超速";Mark=0;break;
267 case '2': type="酒駕";Mark=0;break;
268 case '3': type="闖紅燈";Mark=0;break;
269 case '4': type="違規變道";Mark=0;break;
270 case '5': type="未系安全帶";Mark=0;break;
271 case '6': type="超載";Mark=0;break;
272 default : cout<<"輸入有誤,請重新輸入:";
273 }
274 }
275 cout<<"請輸入罰款金額【0~5000元】:";
276 while(1){
277 if(cin>>fine){
278 cin.sync();
279 if(fine<0||fine>50000){
280 cout<<"請輸入有效金額:";
281 continue;
282 }
283 else break;
284 }
285 else{
286 cin.clear();cin.sync();
287 cout<<"請輸入有效金額:";
288 continue;
289 }
290 }
291 cout<<"請輸入是否處理【是 1】:";cin>>isTreated;cin.sync();
292 if(isTreated=="1"){
293 cout<<"請輸入處理日期【8位數字,如20160630】:";//cin>>treatmentDate;cin.sync();
294 while(1){
295 cin>>treatmentDate;cin.sync();
296 if(!checkNumber(treatmentDate,8)){
297 cout<<"請輸入有效日期:";
298 continue;
299 }
300 else break;
301 }
302 isTreated="是";
303 }
304 else{
305 isTreated="否";treatmentDate="未處理";
306 }
307 cout<<"請輸入備註【無 0】:";cin>>notes;cin.sync();
308 if(notes=="0") notes="無";
309 Violation vltn(number,date,carNumber,tel,location,points,isSpot,police,camera,
310 type,fine,isTreated,treatmentDate,notes);
311 vltnlist.push_back(vltn);
312 saveFile();
313 cout<<"車輛違章信息添加成功!";getchar();
314 return 0;
315 }
316
317 int List::change(){
318 list<Violation>::iterator it;
319 if(!vltnlist.size()){
320 cout<<"還沒有記錄,不能修改!"<<endl;getchar();
321 return 0;
322 }
323 string number,date,carNumber,tel,location,isSpot,police,camera,type,
324 isTreated,treatmentDate,notes;
325 int points,fine;
326 browse();
327 cout<<"請輸入想要修改的記錄編號【放棄0】:";
328 cin>>number;cin.sync();
329 if(number=="0") return 0;
330 Violation vltn(number);
331 it=find(vltnlist.begin(),vltnlist.end(),vltn);
332 if(it==vltnlist.end()){
333 cout<<"此編號不存在!";getchar();
334 return 0;
335 }
336 while(1){
337 system("cls");
338 cout<<" 當前信息 請輸入"<<endl;
339 cout<<" 違章編號:"<<setw(17)<<(*it).number<<" 1"<<endl
340 <<" 違章日期:"<<setw(17)<<(*it).date<<" 2"<<endl
341 <<" 車輛車牌號:"<<setw(17)<<(*it).carNumber<<" 3"<<endl
342 <<" 登記聯繫電話:"<<setw(17)<<(*it).tel<<" 4"<<endl
343 <<" 違章地點:"<<setw(17)<<(*it).location<<" 5"<<endl
344 <<" 處罰扣分:"<<setw(17)<<(*it).points<<" 6"<<endl
345 <<" 是否現場:"<<setw(17)<<(*it).isSpot<<endl
346 <<" 現場執勤交警:"<<setw(17)<<(*it).police<<" 7"<<endl
347 <<" 非現場攝像頭編號:"<<setw(17)<<(*it).camera<<" 8"<<endl
348 <<" 違章類別:"<<setw(17)<<(*it).type<<" 9"<<endl
349 <<" 罰款金額:"<<setw(17)<<(*it).fine<<" a"<<endl
350 <<" 是否處理:"<<setw(17)<<(*it).isTreated<<" b"<<endl
351 <<" 處理日期:"<<setw(17)<<(*it).treatmentDate<<" c"<<endl
352 <<" 備註:"<<endl<<setw(34)<<(*it).notes<<" d"<<