Linux涓婥++閰掑簵绠$悊绯荤粺

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux涓婥++閰掑簵绠$悊绯荤粺相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/href' title='href'>href   鍒濆   cout   chm   鍥為【   too   -name   pre   char   

鍔熻兘瑕佹眰锛?/p>

鎶€鏈浘鐗? class=鎶€鏈浘鐗? class=?

鐩稿叧婧愮爜锛氱爜浜戯細浼犻€侀棬锛孏itHub锛?a href="https://github.com/Ikaros-521/Hotel_Manage_System" data-cke-saved-href="https://github.com/Ikaros-521/Hotel_Manage_System">浼犻€侀棬

鐩稿叧鍥剧墖锛?/p>

鎷嗗垎鐗?/p>

make缂栬瘧

鎶€鏈浘鐗? class=鎶€鏈浘鐗? class=?

./hotel杩愯

鎶€鏈浘鐗? class=鎶€鏈浘鐗? class=?

杈撳叆2锛岃繘鍏ュ紑鎴挎ā鍧?/p>

鎶€鏈浘鐗? class=鎶€鏈浘鐗? class=?

鐩稿叧婧愮爜锛?/p>

class.cpp

  1 #include <fstream>
  2 #include "tools.h"
  3 #include "class.h"
  4 
  5 using namespace std;
  6 
  7 Customer* cust[30];
  8 Room* room[30];
  9 
 10 
 11 int live; // 琚鎴块棿鏁?
 12 
 13 // 鑾峰彇room_num
 14 short Room::get_room_num(void)
 15 
 16     return room_num;
 17 
 18 
 19 // 杩斿洖鎴块棿鐨勪笅鏍?/span>
 20 int Manage::room_index(short room_num)
 21 
 22     short num = room_num;
 23     for(int i=0; i<30; i++)
 24     
 25         if(num == room[i]->get_room_num())
 26         
 27             return i;
 28         
 29     
 30     return -1;
 31 
 32 
 33 // 杩斿洖椤惧鐨勪笅鏍?/span>
 34 int Manage::cust_index(short room_num)
 35 
 36     short num = room_num;
 37     for(int i=0; i<30; i++)
 38     
 39         if(num == cust[i]->room_num)
 40         
 41             return i;
 42         
 43     
 44     return -1;
 45 
 46 
 47 // 鏌ヨ鍓╀綑鎴块棿
 48 void Manage::find_room(void)
 49 
 50     for(int i=0; i<30; i++)
 51     
 52         if(i == 10 || i == 20)
 53         
 54             cout << endl;
 55         
 56         if(room[i]->use != 0)
 57         
 58             continue;
 59         
 60         cout << room[i]->room_num << " ";
 61     
 62     cout << endl;
 63 
 64 
 65 // 寮€鎴?/span>
 66 void Manage::get_room(void)
 67 
 68     cout << "鐜版湁鎴块棿濡備笅" << endl;
 69     find_room();
 70     
 71 //    cout << "宸茬粡鍏ヤ綇鐨勪汉鍛? << endl;
 72 //    show_cust();
 73 
 74     cout << "璇疯緭鍏ユ偍閫夋嫨鐨勬埧闂?";
 75     short room_num;
 76     cin >> room_num;
 77     int flag = used(room_num);
 78     if(flag == 1)
 79     
 80         cout << "姝ゆ埧闂翠笉鑳戒娇鐢紝璇烽噸鏂伴€夋嫨" << endl;
 81         getch();
 82         return;
 83     
 84     else if(flag == -1)
 85     
 86         cout << "姝ゆ埧闂翠笉瀛樺湪锛岃閲嶆柊閫夋嫨" << endl;
 87         getch();
 88         return;
 89     
 90     else
 91     
 92         cout << "鎮ㄩ€夋嫨鐨勬埧闂存槸:" << room_num << endl;
 93     
 94 
 95     int index = room_index(room_num);
 96     short type = room[index]->room_type;
 97 
 98     cout << "璇疯緭鍏ユ偍鐨勫鍚?";
 99     string name;
100     cin >> name;
101     cout << "璇疯緭鍏ユ偍鐨勮韩浠借瘉:";
102     string id;
103     cin >> id;
104     cout << "璇疯緭鍏ユ偍鐨?浣嶅灞?0锛岃〃绀烘病鏈?" << endl;
105     string family1,family2;
106     cin >> family1 >> family2;
107 
108     if(type == 1)
109     
110         if(family1 != "0" || family2 != "0")
111         
112             cout << "浜烘暟杩囧锛屽紑鎴垮け璐?/span>" << endl;
113             getch();
114             return;
115         
116     
117     else if(type == 2)
118     
119         if(family1 != "0" && family2 != "0")
120         
121             cout << "浜烘暟杩囧锛屽紑鎴垮け璐?/span>" << endl;
122             getch();
123             return;
124         
125     
126     else
127     
128     
129 
130     cout << "璇疯緭鍏ヨ璁㈢殑澶╂暟:";
131     short day;
132     cin >> day;
133     short pay = day*room[index]->price;
134     cout << "璇锋敮浠?/span>" << pay << "鍏?/span>" << endl;
135     short money = 0,change = 0;
136     cout << "鏀舵偍锛?/span>";
137     cin >> money;
138     cout << "鍏?/span>" << endl;
139     change = money-pay;
140     if(change < 0)
141     
142         cout << "浣欓涓嶈冻锛岃鍏呭€?/span>" << endl;
143         getch();        
144         return;
145     
146     cout << "鎵炬偍" << change << "鍏?/span>" << endl;
147 
148     short floor = room_num/100;
149 
150     cust[live++] = new Customer(name,id,family1,family2,floor,room_num,day);
151 
152     cout << "宸茶鎴块棿:" << live << endl;
153 
154     for(int i=0; i<30; i++)
155     
156         if(room[i]->room_num == room_num)
157         
158             room[i]->use = 1;
159         
160     
161 
162     cout << "寮€鎴挎垚鍔?娆㈣繋鎮ㄧ殑鍏ヤ綇锛岀浣犵敓娲绘剦蹇?" << endl;
163     getch();
164 
165 
166 // 浣跨敤鎯呭喌
167 int Manage::used(short room_num)
168 
169     short num = room_num;
170     for(int i=0; i<30; i++)
171     
172         if(num == room[i]->room_num)
173         
174             if(room[i]->use == 1)
175             
176                 return 1;
177             
178             else
179             
180                 return 0;
181             
182         
183     
184     return -1;
185 
186 
187 // 鏄剧ず椤惧
188 void Manage::show_cust(void)
189 
190     for(int i=0; i<30; i++)
191     
192         if(cust[i]->name == "0")
193         
194             break;
195         
196         else
197         
198             cout << "濮撳悕:" << cust[i]->name << "," << "鎴块棿:" << cust[i]->room_num << ",";
199 
200             string f1,f2;
201             f1 = cust[i]->family1;
202             f2 = cust[i]->family2;
203             cout << "瀹跺睘1:";
204             if(f1 == "0") cout << " ,";
205             else cout << f1 << ",";
206             cout << "瀹跺睘2:";
207             if(f2 == "0") cout << " ";
208             else cout << f2;
209         
210     
211 
212 
213 // 鎴块棿浠锋牸
214 int Manage::room_price(short room_num)
215 
216     short num = room_num;
217     for(int i=0; i<30; i++)
218     
219         if(room[i]->room_num == num)
220         
221             return room[i]->price;
222         
223     
224     return 0;
225 
226 
227 // 缁垂
228 void Manage::renew_room(void)
229 
230     cout << "璇疯緭鍏ヤ綘鐨勬埧闂村彿:";
231     short room_num;
232     cin >> room_num;
233     int flag = used(room_num);
234     if(flag == -1 || flag == 0)
235     
236         cout << "鎮ㄨ緭鍏ョ殑鎴块棿鍙锋湁璇?/span>" << endl;
237         getch();
238         return;
239     
240 
241     int index = cust_index(room_num);
242 
243     cout << "鎮ㄧ殑鎴块棿鍓╀綑:" << cust[index]->day << "澶?/span>" << endl;
244 
245     cout << "璇疯緭鍏ヤ綘瑕佺画鐨勫ぉ鏁帮細";
246     short day;
247     cin >> day;
248     short pay = day*room_price(room_num);
249     cout << "璇锋敮浠?/span>" << pay << "鍏?/span>" << endl;
250     short price = 0,change = 0;
251     cin >> price;
252     change = price-pay;
253     if(change < 0)
254     
255         cout << "浣欓涓嶈冻锛岃鍏呭€?/span>" << endl;
256         getch();
257         return;
258     
259     cout << "鏀舵偍" << price <<"鍏冿紝鎵炬偍" << change << "鍏?/span>" << endl;
260 
261     string rename = cust[index]->name,reid = cust[index]->id;
262     string refamily1=cust[index]->family1,refamily2=cust[index]->family2;
263     short refloor = cust[index]->floor,reday = cust[index]->day+day;
264     cust[index] = new Customer(rename,reid,refamily1,refamily2,refloor,room_num,reday);
265     
266     cout << "缁垂鎴愬姛锛屾埧闂寸殑浣跨敤鏃堕棿涓?" << reday <<"澶?/span>" << endl;
267     getch();
268 
269 
270 
271 // 閫€鎴?/span>
272 void Manage::cancel_room(void)
273 
274     cout << "璇疯緭鍏ユ偍鐨勬埧闂村彿:";
275     short room_num;
276     string name;
277     cin >> room_num;
278     cout << "璇疯緭鍏ユ偍鐨勫鍚?";
279     cin >> name;
280     int flag = used(room_num);
281     if(flag == 0 || flag == -1)
282     
283         cout << "鎮ㄨ緭鍏ョ殑鎴块棿鏈夎銆?/span>" << endl;
284         getch();
285         return;
286     
287     else
288     
289         short refloor = 0,retype = 0,reprice = 0;
290 
291         int i = cust_index(room_num);
292         if(i != -1)
293         
294             if(cust[i]->name == name)
295             
296                 short price = room[room_index(room_num)]->price;
297                 short change = cust[i]->day*price;
298                 cout << "閫€杩樻偍" << change << "鍏?/span>" << endl;
299 
300                 cust[i] = new Customer("0","0","0","0",0,0,0);
301                 int j = room_index(room_num);
302                 refloor = room[j]->floor;
303                 retype = room[j]->room_type;
304                 reprice = room[j]->price;
305                 room[j] = new Room(refloor,room_num,retype,reprice,0);
306 
307                 cout << "閫€鎴挎垚鍔燂紝鎰熻阿鎮ㄧ殑鍏夐【锛屾杩庝笅娆″厜涓达紒"<< endl;
308                 live--;
309                 getch();
310                 return;
311             
312             else
313             
314                 //cout << cust[i]->name << endl;
315                 cout << "鎮ㄨ緭鍏ョ殑鐩稿叧淇℃伅鏈夎" << endl;
316                 getch();                
317                 return;
318             
319         
320         else
321         
322             cout << "鎮ㄨ緭鍏ョ殑淇℃伅鏈夎" << endl;
323             getch();
324             return;
325         
326     
327 
328 
329 // 椤惧鍒濆鍖?/span>
330 void Manage::c_init(void)
331 
332     fstream ci("data/cust.txt",ios::in);
333     if(!ci.good())
334     
335         cout << "cust.txt鏁版嵁鍔犺浇寮傚父" << endl;
336     
337 
338     for(int i=0; i<30; i++)
339     
340         string name,id,family1,family2;
341         short floor,room_num,day;
342         ci >> name >> id >> family1 >> family2;
343         ci >> floor >> room_num >> day;
344         cust[i] = new Customer(name,id,family1,family2,floor,room_num,day);
345         if(name != "0")
346         
347             live++;
348         
349     
350 
351 
352 // 鎴块棿鍒濆鍖?/span>
353 void Manage::r_init(void)
354 
355     live = 0;
356     fstream ri("data/room.txt",ios::in);
357     if(!ri.good())
358     
359         cout << "room.txt鏁版嵁鍔犺浇寮傚父" << endl;
360     
361 
362     for(int i=0; i<30; i++)
363     
364         short floor,room_num,room_type,price,use;
365         ri >> floor >> room_num;
366         ri >> room_type >> price >> use;
367         room[i] = new Room(floor,room_num,room_type,price,use);        
368     
369 
370 
371 // 鏁版嵁淇濆瓨
372 void Manage::save_data(void)
373 
374     fstream co("data/cust.txt",ios::out);
375     fstream ro("data/room.txt",ios::out);
376 
377     for(int i=0; i<30; i++)
378     
379         co << cust[i]->name << " " << cust[i]->id << " ";
380         co << cust[i]->family1 << " " << cust[i]->family2 << " ";
381         co << cust[i]->floor << " " << cust[i]->room_num << " ";
382         co << cust[i]->day << "\n";
383 
384         ro << room[i]->floor << " " << room[i]->room_num << " ";
385         ro << room[i]->room_type << " " << room[i]->price << " ";
386         ro << room[i]->use << "\n";
387     
388 
389 
390 // 鑿滃崟
391 void Manage::menu(void)
392 
393     cout << "***娆㈣繋浣跨敤閰掑簵绠$悊绯荤粺***" << endl;
394     cout << "   1銆佹煡璇㈡埧闂?/span>" << endl;
395     cout << "   2銆佸紑鎴?/span>" << endl;
396     cout << "   3銆佺画璐?/span>" << endl;
397     cout << "   4銆侀€€鎴?/span>" << endl;
398     cout << "   0銆侀€€鍑虹郴缁?/span>" << endl;
399     cout << "-----------------------" << endl;
400 

 

鎶€鏈浘鐗? class=

main.cpp

 1 #include <iostream>
 2 #include <fstream>
 3 #include <stdlib.h>
 4 #include <termio.h>
 5 #include "class.h"
 6 #include "tools.h"
 7 
 8 using namespace std;
 9 
10 Manage admin;
11 
12 // 涓诲嚱鏁?/span>
13 int main()
14 
15     admin.c_init();
16     admin.r_init();
17     while(1)
18     
19         system("clear");
20         admin.menu();
21         switch(get_cmd(鈥?/span>0鈥?/span>,鈥?/span>4鈥?/span>))
22         
23             case 鈥?/span>1鈥?/span>: admin.find_room(); break;
24             case 鈥?/span>2鈥?/span>: admin.get_room(); break;
25             case 鈥?/span>3鈥?/span>: admin.renew_room(); break;
26             case 鈥?/span>4鈥?/span>: admin.cancel_room(); break;
27             case 鈥?/span>0鈥?/span>: admin.save_data(); return 0;
28         
29         getch();
30     
31     
32 

 

鎶€鏈浘鐗? class=

tools.cpp

 1 #include "tools.h"
 2 #include <string.h>
 3 #include <getch.h>
 4 #include <stdbool.h>
 5 
 6 void clear_stdin(void)
 7 
 8     stdin->_IO_read_ptr = stdin->_IO_read_end;//娓呯悊杈撳叆缂撳啿鍖?/span>
 9 
10 
11 char get_cmd(char start,char end)
12 
13     clear_stdin();
14 
15     printf("璇疯緭鍏ユ寚浠?");
16     while(true)
17     
18         char val = getch();
19         if(val >= start && val <= end)
20         
21             printf("%c\n",val);
22             return val;
23         
24     
25 

 

鎶€鏈浘鐗? class=

class.h

 1 #ifndef CLASS_H
 2 #define CLASS_H
 3 
 4 #include <iostream>
 5 #include <string.h>
 6 
 7 using namespace std;
 8 
 9 class Customer
10 
11 public:
12     string name; //濮撳悕
13     string id; //韬唤璇?/span>
14     string family1; //瀹跺睘1
15     string family2; //瀹跺睘2
16     short floor; //妤煎眰
17     short room_num; //鎴块棿鍙?/span>
18     short day; //鏃堕棿
19     Customer(string name="0",string id="0",string family1="0",string family2="0",short floor=0,short room_num=0,short day=0)
20     
21         this->name = name;
22         this->id = id;
23         this->family1 = family1;
24         this->family2 = family2;
25         this->floor = floor;
26         this->room_num = room_num;
27         this->day = day;
28     
29 ;
30 
31 class Room
32 
33 public:
34     short floor; //妤煎眰
35     short room_num; //鎴块棿鍙?/span>
36     short room_type; //鎴块棿绫诲瀷
37     short price; //浠锋牸
38     short use; //鏄惁浣跨敤
39     Room(short floor=0,short room_num=0,short room_type=0,short price=0,short use=0)
40     
41         this->floor = floor;
42         this->room_num = room_num;
43         this->room_type = room_type;
44         this->price = price;
45         this->use = use;
46     
47     short get_room_num(void);
48 
49 ;
50 
51 class Manage
52 
53 public:
54     void menu(void);    // 鑿滃崟
55     void find_room(void);    // 鍓╀綑鎴块棿
56     void get_room(void);    // 寮€鎴?/span>
57     void renew_room(void);    // 缁垂
58     void cancel_room(void);    // 閫€鎴?/span>
59     int room_index(short room_num);    // 鎴块棿涓嬫爣
60     int cust_index(short room_num); // 椤惧涓嬫爣
61     int used(short room_num);    // 鎴块棿浣跨敤鎯呭喌
62     void show_cust(void);    // 鏄剧ず鍏ヤ綇椤惧
63     int room_price(short room_num);    // 鎴块棿浠锋牸
64     void c_init(void);    // 椤惧鍒濆鍖?/span>
65     void r_init(void);    // 鎴块棿鍒濆鍖?/span>
66     void save_data(void);    // 淇濆瓨鏁版嵁
67 ;
68 
69 #endif//CLASS_H

 

鎶€鏈浘鐗? class=

tools.h

 1 #ifndef TOOL_H
 2 #define TOOL_H
 3 
 4 #include <stdio.h>
 5 
 6 #include "tools.h"
 7 #include <string.h>
 8 #include <getch.h>
 9 #include <stdbool.h>
10 
11 void clear_stdin(void);
12 
13 char get_cmd(char start,char end);
14 
15 #endif//TOOL_h

 

鎶€鏈浘鐗? class=

以上是关于Linux涓婥++閰掑簵绠$悊绯荤粺的主要内容,如果未能解决你的问题,请参考以下文章

Atitit 娓告垙寮曟搸---鐗╃悊绯荤粺锛?)------鐖嗙偢鏁堟灉

瀹胯垗绠$悊绯荤粺绛旇京闂鎬荤粨

Java寮€鍙戜紒涓氱骇鏉冮檺绠$悊绯荤粺 瑙嗛鏁欑▼

linux绯荤粺IO鎬ц兘娴嬭瘯

Linux绯荤粺-鍛戒护琛屽揩鎹烽敭

linux涓父鐢ㄧ殑绯荤粺鍑芥暟