C++实现汽车订票系统(C++课程设计)

我会带着你远行 2022-05-10 09:16 417阅读 0赞
  1. #include<iostream>
  2. #include<fstream>
  3. #include<cstring>
  4. #include<iomanip>
  5. #define Maxsize 100
  6. using namespace std;
  7. struct Route
  8. {
  9. char number[20];
  10. char startandreach[20];
  11. char type[20];
  12. char longroute[20];
  13. char time[20];
  14. double price;
  15. int sitnumber;
  16. int save;
  17. int sale;
  18. };
  19. struct Ticketnews
  20. {
  21. char type[20];
  22. char time[20];
  23. int booked;
  24. int remained;
  25. };
  26. struct Booknews
  27. {
  28. char name[20];
  29. char identitycard[20];
  30. char number[20];
  31. char date[20];
  32. char road[20];
  33. int sitnumber;
  34. };
  35. void Match(Route L1[],Ticketnews L2[],int n)//票务信息保存
  36. {
  37. ofstream outfile("票务信息.txt",ios::out);
  38. if (!outfile)
  39. {
  40. cerr<<"open error!"<<endl;
  41. exit(1);
  42. }
  43. int i;
  44. for(i=0;i<n;i++)
  45. {
  46. strcpy(L2[i].type,L1[i].type);
  47. strcpy(L2[i].time,L1[i].time);
  48. L2[i].booked=L1[i].sale;
  49. L2[i].remained=L1[i].save;
  50. outfile<<L2[i].type<<" "<<L2[i].time<<" "<<L2[i].booked<<" "<<L2[i].remained<<endl;
  51. }
  52. outfile.close();
  53. }
  54. int Book(Booknews &L1,Route &L2,int s)//这里一定要是参数传递
  55. {
  56. ofstream outfile("买票客户信息.txt",ios::app);//怎么可以重复的写入订票人的数据
  57. if (!outfile)
  58. {
  59. cerr<<"open error!"<<endl;
  60. exit(1);
  61. }
  62. cout<<"请输入订票信息:"<<endl;
  63. cout<<"姓名:";cin>>L1.name;
  64. cout<<"身份证:";cin>>L1.identitycard;
  65. cout<<"车次:";cin>>L1.number;
  66. cout<<"日期:";cin>>L1.date;
  67. cout<<"路线:";cin>>L1.road;
  68. L2.sale++;
  69. L2.save--;
  70. L1.sitnumber=L2.sale;
  71. cout<<"订票信息为:"<<endl;
  72. cout<<"姓名:"<<L1.name<<endl<<"身份证:"<<L1.identitycard<<endl<<"车次:"<<L1.number<<endl<<"日期:"<<L1.date<<endl<<"座位号:"<<L1.sitnumber<<endl<<"路线:"<<L1.road<<endl;
  73. outfile<<L1.name<<" "<<L1.identitycard<<" "<<L1.number<<" "<<L1.date<<" "<<L1.sitnumber<<" "<<L1.road<<endl;
  74. outfile.close();
  75. s++;
  76. return s;
  77. }
  78. int Return(Booknews L1[],int s)
  79. {
  80. Booknews L;
  81. con:cout<<"请输入退票者姓名,身份证";
  82. cin>>L.name>>L.identitycard;
  83. int i;
  84. for(i=0;i<s;i++)
  85. {
  86. if(((strcmp(L.name,L1[i].name))&&(strcmp(L.identitycard,L1[i].identitycard))))
  87. {
  88. cout<<"这个人没有订票!"<<endl;
  89. return 0;
  90. }
  91. else
  92. {
  93. int j=i;
  94. cout<<"该订票者的信息为:";
  95. cout<<"姓名:"<<L1[i].name<<endl;
  96. cout<<"身份证:"<<L1[i].identitycard<<endl;
  97. cout<<"车次:"<<L1[i].number<<endl;
  98. cout<<"订票日期:"<<L1[i].date<<endl;
  99. cout<<"座位号:"<<L1[i].sitnumber<<endl;
  100. cout<<"路线:"<<L1[i].road<<endl;
  101. cout<<"以上信息是否符合?符合请按1,不符合请按2:";
  102. int k;
  103. cin>>k;
  104. if(k==2) goto con;
  105. else
  106. /*cout<<"输入订票者的车次,座位号和日期:";
  107. cin>>L.number>>L.sitnumber>>L.date;
  108. for(i=L.sitnumber-1;i<s;i++)
  109. {
  110. strcpy(L1[i].name,L1[i+1].name);
  111. strcpy(L1[i].identitycard,L1[i+1].identitycard);
  112. strcpy(L1[i].number,L1[i+1].number);
  113. strcpy(L1[i].date,L1[i+1].date);
  114. L1[i].sitnumber=L1[i+1].sitnumber;
  115. cout<<"退票成功!"<<endl;
  116. }*/
  117. {
  118. for(;i<s;i++)//删除退票者的信息
  119. {
  120. strcpy(L1[i].name,L1[i+1].name);
  121. strcpy(L1[i].identitycard,L1[i+1].identitycard);
  122. strcpy(L1[i].number,L1[i+1].number);
  123. strcpy(L1[i].date,L1[i+1].date);
  124. L1[i].sitnumber=L1[i+1].sitnumber;
  125. }
  126. cout<<"退票成功!"<<endl;
  127. ofstream outfile("买票客户信息.txt",ios::out);
  128. if(!outfile)
  129. {
  130. cerr<<"open error!"<<endl;
  131. exit(1);
  132. }
  133. for(i=0;i<j;i++)
  134. {
  135. outfile<<L1[i].name<<" "<<L1[i].identitycard<<" "<<L1[i].number<<" "<<L1[i].date<<" "<<L1[i].sitnumber<<endl;
  136. }
  137. outfile.close();
  138. }
  139. return j;
  140. }
  141. }
  142. }
  143. int main()
  144. {
  145. ifstream infile("所有车次路线信息.txt",ios::in);
  146. if (!infile)
  147. {
  148. cerr<<"open error!"<<endl;
  149. exit(1);
  150. }
  151. ifstream infile1("买票客户信息.txt",ios::in);//怎么可以重复的写入订票人的数据
  152. if (!infile1)
  153. {
  154. cerr<<"open error!"<<endl;
  155. exit(1);
  156. }
  157. int n,i,n1,s,j;
  158. infile>>n;
  159. infile1>>s;
  160. infile1.close();
  161. Route route[Maxsize];
  162. Ticketnews ticketnews[Maxsize];
  163. Booknews booknews[Maxsize];
  164. for(i=0;i<n;i++)
  165. {
  166. infile>>route[i].number>>route[i].startandreach>>route[i].type>>route[i].longroute>>route[i].time>>route[i].price>>route[i].sitnumber>>route[i].save>>route[i].sale;
  167. }
  168. cout<<"输出所有汽车路线为:"<<endl;
  169. cout<<"车次"<<setw(7)<<"路线"<<setw(18)<<"车型"<<setw(12)<<"路线长度"<<setw(7)<<"时间"<<setw(14)<<"价格"<<setw(8)<<"座位"<<
  170. setw(5)<<"剩票"<<setw(5)<<"卖票"<<endl;
  171. for(i=0;i<n;i++)
  172. {
  173. cout<<route[i].number<<setw(20)<<route[i].startandreach<<setw(12)<<route[i].type<<setw(8)<<route[i].longroute<<setw(15)<<route[i].time<<setw(8)<<
  174. route[i].price<<setw(4)<<route[i].sitnumber<<setw(4)<<route[i].save<<setw(4)<<route[i].sale<<endl;
  175. //cout<<route[i].number<<" "<<route[i].startandreach<<" "<<route[i].type<<" "<<route[i].longroute<<" "<<route[i].time<<" "<<
  176. // route[i].price<<" "<<route[i].sitnumber<<" "<<route[i].save<<" "<<route[i].sale<<endl;
  177. }
  178. Match(route,ticketnews,n);
  179. cout<<"主菜单为:"<<endl;
  180. con:cout<<"1 为订票,2 为退票,3 为改变路线,4为查询订票情况,5为退出该系统,请根据你所要实现的功能输入相应的数字:";
  181. cin>>n1;
  182. switch(n1)
  183. {
  184. case 1:
  185. {
  186. con1:cout<<"请输入你要订票的车次:";
  187. cin>>booknews[s].number;
  188. bool flag(true);//一定要用bool变量控制
  189. for(i=0;i<n;i++)
  190. {
  191. if(!(strcmp(booknews[s].number,route[i].number))&&(route[i].save!=0))
  192. {
  193. //定义一个变量是否需要订票
  194. flag = false;
  195. cout<<booknews[s].number<<"车次还有票是否需要订票,是就输入yes,否就输入no:";
  196. char k1[5];
  197. con3:cin>>k1;
  198. if(!strcmp(k1,"yes"))
  199. {
  200. s=Book(booknews[s],route[i],s);
  201. }
  202. else
  203. break;
  204. cout<<"是否还要订票,是就输入yes,不是就输入no:";
  205. goto con3;
  206. }
  207. }
  208. if(flag)
  209. {
  210. cout<<"你输入的车次不存在,请重新输入!"<<endl;
  211. goto con1;
  212. }
  213. ofstream outfile("买票客户信息.txt",ios::out||ios::app);
  214. if (!outfile)
  215. {
  216. cerr<<"open error!"<<endl;
  217. exit(1);
  218. }
  219. outfile<<s<<endl;//一定要加一行endl,在另一行添加才可
  220. outfile.close();
  221. cout<<"返回主菜单请按1,否则请按2:";
  222. int kkk;
  223. cin>>kkk;
  224. if(kkk==1)goto con;
  225. else
  226. break;
  227. }
  228. case 2:
  229. {
  230. if(s==0){cout<<"目前没有人订票,不能退票!"<<endl;goto con;}
  231. else
  232. {
  233. j=Return(booknews,s);
  234. s--;
  235. route[j].sale--;
  236. route[j].save++;
  237. cout<<"返回主菜单请按1,退出请按2:";
  238. int kk1;
  239. cin>>kk1;
  240. if(kk1==1)goto con;
  241. }
  242. ofstream outfile("买票客户信息.txt",ios::out||ios::app);//怎么可以重复的写入订票人的数据
  243. if (!outfile)
  244. {
  245. cerr<<"open error!"<<endl;
  246. exit(1);
  247. }
  248. outfile<<s<<endl;
  249. outfile.close();
  250. break;
  251. }
  252. case 3://用指针指向文件??
  253. {
  254. cout<<"请输入管理员密码:";
  255. int code;
  256. cin>>code;
  257. if(code==123) //记住密码
  258. {
  259. con4:cout<<"请根据A为增加路线,B为删除路线,C为修改汽车路线信息输入相应的字母:";
  260. char z;cin>>z;
  261. if(z=='A')
  262. {
  263. infile.close();
  264. con5:n++;//注意con5要指对地方
  265. ofstream outfile("所有车次路线信息.txt",ios::out||ios::app);
  266. if (!outfile)
  267. {
  268. cerr<<"open error!"<<endl;
  269. exit(1);
  270. }
  271. outfile<<n;//输出过后要在把n输进去????
  272. //infile>>n;
  273. if(!outfile.eof())//指向文件末尾
  274. outfile.seekp(0,ios::end);
  275. //outfile<<endl;
  276. cout<<"请输入增加路线的所有数据:"<<endl;
  277. cout<<"车次:";cin>>route[n-1].number;
  278. cout<<"路线:";cin>>route[n-1].startandreach;
  279. cout<<"车辆类型:";cin>>route[n-1].type;
  280. cout<<"里程:";cin>>route[n-1].longroute;
  281. cout<<"时间:";cin>>route[n-1].time;
  282. cout<<"票价:";cin>>route[n-1].price;
  283. cout<<"座位数:";cin>>route[n-1].sitnumber;
  284. cout<<"剩余票数:";cin>>route[n-1].save;
  285. cout<<"卖票数:";cin>>route[n-1].sale;
  286. outfile<<route[n-1].number<<" "<<route[n-1].startandreach<<" "<<route[n-1].type<<" "<<route[n-1].longroute<<" "
  287. <<route[n-1].time<<" "<<route[n-1].price<<" "<<route[n-1].sitnumber<<" "<<route[n-1].save<<" "<<route[n-1].sale<<endl;
  288. outfile.close();
  289. cout<<"是否还要增加路线,是请输入yes,退出请输入no:";
  290. char kk3[5];
  291. cin>>kk3;
  292. if(strcmp(kk3,"no")) goto con5;
  293. }
  294. else
  295. {
  296. if(z=='B')
  297. {
  298. con6:cout<<"请输入你要删除的车次路线:";
  299. char z1[20];int z2=-1;
  300. cin>>z1;
  301. for(i=0;i<n;i++)
  302. {
  303. if(!strcmp(z1,route[i].number))
  304. {
  305. z2=i;
  306. break;
  307. }
  308. }
  309. if(z2>0)
  310. {
  311. for(;z2<n-1;z2++)
  312. {
  313. strcpy(route[z2].number,route[z2+1].number);
  314. strcpy(route[z2].startandreach,route[z2+1].startandreach);
  315. strcpy(route[z2].type,route[z2+1].type);
  316. strcpy(route[z2].longroute,route[z2+1].longroute);
  317. strcpy(route[z2].time,route[z2+1].time);
  318. route[z2].price=route[z2+1].price;
  319. route[z2].sitnumber=route[z2+1].sitnumber;
  320. route[z2].save=route[z2+1].save;
  321. route[z2].sale=route[z2+1].sale;
  322. }
  323. n--;
  324. ofstream outfile("所有车次路线信息.txt",ios::out);
  325. if (!outfile)
  326. {
  327. cerr<<"open error!"<<endl;
  328. exit(1);
  329. }
  330. outfile<<n<<endl;
  331. for(i=0;i<n;i++)
  332. {
  333. outfile<<route[i].number<<" "<<route[i].startandreach<<" "<<route[i].type<<" "<<route[i].longroute<<" "<<route[i].time<<" "<<
  334. route[i].price<<" "<<route[i].sitnumber<<" "<<route[i].save<<" "<<route[i].sale<<endl;
  335. }
  336. outfile.close();
  337. con7:cout<<"是否还要删除路线,是就输入yes,否就输入no:";
  338. char kk4[5];
  339. cin>>kk4;
  340. if(!strcmp(kk4,"yes"))goto con6;
  341. else
  342. if(!strcmp(kk4,"no"))break;
  343. else
  344. if(strcmp(kk4,"yes")) {cout<<"你输入的单词不符合要求,请重新输入:";goto con7;}
  345. }
  346. }
  347. else
  348. {
  349. if(z=='C')
  350. {
  351. con2:cout<<"请输入你要修改的路线:";
  352. char k[20],k2[20];int kk;
  353. cin>>k;
  354. for(i=0;i<n;i++)
  355. {
  356. if(!strcmp(k,route[i].startandreach))
  357. {
  358. cout<<"修改路线为:";
  359. cin>>k2;
  360. strcpy(route[i].startandreach,k2);
  361. /*cout<<"是否还要修改,是就输入yes,否就输入no:";当想要把重复的路线修改时,就要用到的程序段
  362. char kk4[5];
  363. cin>>kk4;
  364. if(strcmp(kk4,"yes")) break;*/
  365. }
  366. ofstream outfile("所有车次路线信息.txt",ios::out);
  367. if (!outfile)
  368. {
  369. cerr<<"open error!"<<endl;
  370. exit(1);
  371. }
  372. outfile<<n<<endl;
  373. for(i=0;i<n;i++)
  374. {
  375. outfile<<route[i].number<<" "<<route[i].startandreach<<" "<<route[i].type<<" "<<route[i].longroute<<" "<<route[i].time<<" "<<
  376. route[i].price<<" "<<route[i].sitnumber<<" "<<route[i].save<<" "<<route[i].sale<<endl;
  377. }
  378. outfile.close();
  379. }
  380. break;
  381. con8:cout<<"是否还需要修改,是就选1,不是就选2:";
  382. cin>>kk;
  383. if(kk==1)goto con2;
  384. else
  385. if(kk==2)break;
  386. else {cout<<"你输入的数字不符合要求,请重新输入:"; goto con8;}
  387. }
  388. else
  389. {
  390. cout<<"你输入的数字不符合要求,请重新输入:";
  391. goto con4;
  392. }
  393. }
  394. }
  395. }
  396. else
  397. {
  398. cout<<"你所输入的密码错误,返回主菜单请按1,退出请按2:";
  399. int kk2;
  400. cin>>kk2;
  401. if(kk2==2)return 0;
  402. else
  403. goto con;
  404. }
  405. break;
  406. }
  407. case 4:
  408. {
  409. char k3[10],k4[20];
  410. cout<<"请输入用户名和身份证号:";
  411. cin>>k3>>k4;
  412. for(i=0;i<s;i++)
  413. {
  414. if(!(strcmp(k3,booknews[i].name)&&strcmp(k4,booknews[i].identitycard)))
  415. {
  416. cout<<"你的订票信息为:"<<endl;
  417. cout<<"姓名:"<<booknews[i].name<<endl;
  418. cout<<"身份证号:"<<booknews[i].identitycard<<endl;
  419. cout<<"车次:"<<booknews[i].number<<endl;
  420. cout<<"座位号:"<<booknews[i].sitnumber<<endl;
  421. cout<<"日期:"<<booknews[i].date<<endl;
  422. }
  423. }
  424. }
  425. return 0;
  426. case 5:
  427. {
  428. return 0;
  429. }
  430. default:
  431. {
  432. cout<<"你所选的功能不存在,请重新再选择!"<<endl;
  433. goto con;
  434. }
  435. }
  436. cout<<"输出所有汽车路线为:"<<endl;
  437. for(i=0;i<n;i++)
  438. {
  439. cout<<route[i].number<<setw(20)<<route[i].startandreach<<setw(12)<<route[i].type<<setw(8)<<route[i].longroute<<setw(15)<<route[i].time<<setw(8)<<
  440. route[i].price<<setw(4)<<route[i].sitnumber<<setw(4)<<route[i].save<<setw(4)<<route[i].sale<<endl;
  441. /*cout<<route[i].number<<" "<<route[i].startandreach<<" "<<route[i].type<<" "<<route[i].longroute<<" "<<route[i].time<<" "<<
  442. route[i].price<<" "<<route[i].sitnumber<<" "<<route[i].save<<" "<<route[i].sale<<endl;*/
  443. }
  444. //goto con;
  445. return 0;
  446. }

自己设置的票务信息

quickplane 12:00—19:00 0 100
quickplane 12:00—19:00 0 100
train 12:00—19:00 0 100
quickplane 12:00—19:00 0 100
train 12:00—19:00 0 100
quickplane 12:00—19:00 0 100
car 12:00—19:00 0 100
car 12:00—19:00 0 100
train 12:00—19:00 0 100
quickplane 12:00—19:00 0 100
car 12:00—19:00 0 100
car 12:00—19:00 0 100
car 4:00—8:00 0 35
car 8:00—9:00 0 45
car 9:00—19:00 0 48
car 9:00—19:00 0 45

所有车次的路线信息

16
A000 Beijing—Nanchang quickplane 1000Km 12:00—19:00 456.25 100 100 0
A001 Beijing—Nanchang quickplane 1000Km 12:00—19:00 456.25 100 100 0
A002 Beijing—Nanchang train 1000Km 12:00—19:00 456.25 100 100 0
A003 Beijing—Nanchang quickplane 1000Km 12:00—19:00 456.25 100 100 0
A004 Beijing—Nanchang train 1000Km 12:00—19:00 456.25 100 100 0
A005 Beijing—Nanchang quickplane 1000Km 12:00—19:00 456.25 100 100 0
A006 Beijing—Nanchang car 1000Km 12:00—19:00 456.25 100 100 0
A007 Beijing—Nanchang car 1000Km 12:00—19:00 456.25 100 100 0
A008 Beijing—Nanchang train 1000Km 12:00—19:00 456.25 100 100 0
A009 Beijing—Nanchang quickplane 1000Km 12:00—19:00 456.25 100 100 0
A010 Beijing—Nanchang car 1000Km 12:00—19:00 456.25 100 100 0
A011 Beijing—Nanchang car 1000Km 12:00—19:00 456.25 100 100 0
A012 J—P car 456km 4:00—8:00 45 35 35 0
A013 JJ—PP car 456km 8:00—9:00 45.2 45 45 0
A013 pp—ll car 489km 9:00—19:00 45.32 48 48 0
A014 car car 456km 9:00—19:00 45.2 45 45 0

发表评论

表情:
评论列表 (有 0 条评论,417人围观)

还没有评论,来说两句吧...

相关阅读