c++程序设计第二题
来源:互联网 发布:淘宝提升信誉的方法 编辑:程序博客网 时间:2024/06/14 15:47
2、班车信息管理
一交通公司,班车系统的数据包括如下两部分:
①班车信息:班交及车号、最大载客数、起点、开车时间、终地点、到达终点站时间,单价;
② 乘客信息:车次及车号、身份证号码、姓名、性别、出生年月、座位号。
乘客订票的主要方式是:乘客提供起点、时间、终点、订票数等订票要求,根据事先保存的班数据决定乘客能否订票?只有全部满足了乘客的订票要求并且所订班次有足够的未订座位之后才能完成订票处理,并且修改该班次的未订座位数(每个班的未订座位数的初始值就是该班次的最大载客数);否则,订票失败,并且给出不能订票的原因。
功能要求 :
⑴ 增加班次记录。将新的次班记录增加到原有的次班数据中。在进行处理时必须检查所要增加的班次记录是否存在,如果已经存在,应给出提示信息后停止增加;
⑵ 班次取消。如果某班次的乘客数太少(已订票的少于本次班次最大载客数的10%),将取消该班次,但该班欠的记录仍然保存在原有的班数据中;
⑶ 班次查询。应该有以下几种基本的查询方式:按班次号、按起点和时间、按终地点,和时间;
⑷ 班次订票。按上述问题描述中的乘客订票方式完成班次订票处理。
⑸ 设计一个菜单,至少具有上述操作要求的基本功能。
#include<iostream> #include<windows.h> #include <time.h> #include<fstream> #include<cstdlib> #include <string> #include<iomanip> using namespace std; void menu(); //菜单选择函数 void add(); //增加班次信息函数 void search(); //班次查询函数 void book(); //订票函数 void cancel(); //退票函数 int comeon(); //是否继续操作函数 int gethour(char *s); //小时转换函数 int getmin(char *s); //分钟转换函数 int i=0,flag=1,f=1; char key=0; struct car { char numbers[10]; //班次 char carnumber[10]; //车号 char people[5]; //最大载客数 char start[50]; //起点 char starttime[30]; //出发时间 char finish[50]; //终点 char finishtime[30]; //到达时间 char money[5]; //票价 char man[3]; //订票人数 struct *next; }; struct passenger { char numbers[10]; //班次 char id[20]; //身份证号码 char name[50]; //姓名 char ticket[2]; //订票数 char snum[2]; //座位号 struct *next; }; int main() { system("color f9"); time_t rawtime; //获取系统时间函数 struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); cout<<timeinfo->tm_hour<<":"<<timeinfo->tm_min; key=0; //这里赋值为0防止后面返回主界面功能失效 flag=1; menu(); //调用功能跳转函数 return 0; } void menu() { cout<<"**********************************************\n" <<" * 欢迎使用本系统 *\n" <<" *-------------------------------------------------\n" <<" * 请选择您需要的服务: *\n" <<" * 1、增加班次记录 *\n" <<" * 2、退订车票 *\n" <<" * 3、班次查询 *\n" <<" * 4、班次订票 *\n" <<" * 5、退出 *\n" <<" -------------------------------------------------*\n" <<" 请输入数字1~5来选择您需要的服务 *\n" <<"***************************************************\n"; for(;key>'5'||key<'1';) { fflush(stdin); cin>>key; switch(key) { case '1': add(); break; case '2': cancel(); break; case '3': search(); break; case '4': book(); break; case '5': system("cls"); cout<<"\t\t\t\t欢迎您的再次使用\n\n" <<"\t\t\t\t 再见! \n\n\n"; exit(1); break; default: system("cls"); cout<<"\t\t 没有此服务啊 \n\n\n"; Sleep(1000); system("cls"); main(); break; } } } void add() { f=1; system("cls"); //清屏函数 fflush(stdin); //清楚键盘缓存 char k; while(f) { if(flag == 0) { f=0; system("cls"); main(); } if(f==0) break; cout<<"\n\n 您正在使用增加班次记录功能\n\n"; cout<<"\t\t\t 1、增加班次\n\n" <<"\t\t\t 2、返回主界面\n\n" <<"\t\t\t "; cin>>k; switch(k) { case '1': system("cls"); break; case '2': system("cls"); main(); f = 0; flag = 0; break; default: system("cls"); cout<<">>>>>>>> 输入错误!\n\n\n"; Sleep(1000); system("cls"); break; } if(k=='1') { ofstream myfile; myfile.open("car.txt",ios::app); car *head,*p1,*p2; head = NULL; p1 = new(car); p2 = p1; cout<<">>>>>请输入对应的信息:\n" <<" 车次: "; fflush(stdin); cin>>p1 -> numbers; cout<<" 车号: "; cin>>p1 -> carnumber; cout<<"最大载客数: "; cin>>p1 -> people; cout<<" 起点: "; cin>>p1->start; cout<<" 出发时间(英文状态下输入): "; cin>>p1 ->starttime; cout<<" 终点: "; cin>>p1 ->finish; cout<<" 到达时间(英文状态下输入): "; cin>>p1->finishtime; cout<<" 票价: "; cin>>p1->money; myfile<<p1 -> numbers<<" "<<p1 ->carnumber<<" "<<p1 ->people<<' ' <<p1 ->start<<" "<<p1 ->starttime<<" "<<p1->finish<<" " <<p1->finishtime<<" "<<p1->money<<" "<<"0 \n"; myfile.close(); delete head,p1,p2; comeon(); } } } void cancel() { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); f=1; char a,tmp[100]; while(f) { if(flag == 0) { f=0; system("cls"); main(); } system("cls"); cout<<"\n\n 您正在使用班次取消功能。 \n" <<"\t\t *******************************\n" <<"\t\t * 请选择 *\n" <<"\t\t *******************************\n" <<"\t\t * 1、按班次号 *\n" <<"\t\t * 2、返回 *\n" <<"\t\t *******************************\n" <<"\t\t "; cin>>a; system("cls"); switch(a) { case '1': fflush(stdin); system("cls"); cout<<" 请输入班次号: "; cin.getline(tmp,50); while(1) { int ff,flag1=0; char *s; string str,st1,st2,st3,st4,st5,st6,st7,st8; str=" "; s =&str[0]; ifstream myfile("car.txt"); while(!myfile.eof()) { getline(myfile,str,' '); getline(myfile,st1,' '); getline(myfile,st2,' '); getline(myfile,st3,' '); getline(myfile,st4,' '); getline(myfile,st5,' '); getline(myfile,st6,' '); getline(myfile,st7,' '); getline(myfile,st8,'\n'); if(strcmp(tmp,s)==0) { cout<<"\n 车次 : "<<str<<"\t车号 : "<<st1<<"\t最大载客数 : "<<st2<<" 人" <<"\n 始发地 : "<<left<<setw(20)<<st3<<"\t出发时间 : "<<st4 <<"\n 目的地 : "<<left<<setw(20)<<st5<<"\t到达时间 : "<<st6 <<"\n 票 价 : "<<st7<<" 元\t\t\t"<<"已订票人数: "<<st8<<" 人\n"; flag1=1; } } myfile.close(); if(flag1) { ifstream myfile("car.txt"); string t2,max,num,ttl,t1=" "; char *p1,*p3; p1 = &t1[0]; while(!myfile.eof()) { getline(myfile,t1,' '); if(strcmp(p1,tmp)==0) { flag=1; for(i=1;i<8;i++) { getline(myfile,t1,' '); if(i==2) max=t1; if(i==4) ttl=t1; p3 = &(ttl[0]); } getline(myfile,num,'\n'); break; } getline(myfile,num,'\n'); } myfile.close(); if(atof(&num[0])/atof(&max[0])<=0.1) ff=1; else ff=0; if(gethour(p3)<timeinfo->tm_hour) { if(ff) { cout<<"\n 对不起,该班次因订票人数过少已自动取消," <<"请到前台办理退票!\n"; comeon(); cancel(); } else { cout<<"\n 对不起,该班次已发出,无法退订\n"; comeon(); cancel(); } } else if(gethour(p3)<=timeinfo->tm_hour) { if(getmin(p3)<=timeinfo->tm_min) { if(ff) { cout<<"\n 对不起,该班次因订票人数过少已自动取消," <<"请到前台办理退票!\n"; comeon(); cancel(); } else { cout<<"\n 对不起,该班次已发出!!!无法退订\n"; comeon(); cancel(); } } } else { string name,name1,numbers,num; ifstream f1("car.txt"); ifstream f2("passenger.txt"); ofstream f3,f4; f3.open("bak.txt",ios::trunc); f4.open("bak1.txt",ios::trunc); cout<<"请输入您的姓名 :"; cin>>name; while(!f2.eof()) { string str,str1,str2,str3; str=str1=str2=str3=" "; int ff=0; getline(f2,str,' '); getline(f2,name1,' '); getline(f2,str1,' '); getline(f2,numbers,' '); getline(f2,str2,'\n'); if(strcmp(&str[0],&tmp[0])==0 && strcmp(&name[0],&name1[0])==0) num=numbers; else f4<<str<<' '<<name1<<' '<<str1<<' ' <<numbers<<' '<<str2<<' '<<'\n'; } while(!f1.eof()) { string s1,s2; getline(f1,s1,' '); if(strcmp(&s1[0],&tmp[0])==0) { f3<<s1<<' '; for(int i=1;i<8;i++) { getline(f1,s2,' '); f3<<s2<<' '; } getline(f1,s2,'\n'); f3<<atoi(&s2[0])-atoi(&num[0])<<'\n'; } else { f3<<s1<<' '; getline(f1,s1,'\n'); f3<<s1<<'\n'; } } f1.close(); f2.close(); f3.close(); f4.close(); ofstream ff1("car.txt",ios::trunc); ofstream ff2("passenger.txt",ios::trunc); ifstream ff3("bak.txt"); ifstream ff4("bak1.txt"); while(!ff3.eof()) { string s; s=" "; getline(ff3,s,'\0'); ff1<<s; } while(!ff4.eof()) { string s; s=" "; getline(ff4,s,'\0'); ff2<<s; } ff1.close(); ff2.close(); ff3.close(); ff4.close(); cout<<" 退订成功!!! "; comeon(); cancel(); } } else { cout<<"\n 对不起,找不到该班车信息 \n\n"; comeon(); cancel(); break; } } break; case '2': { system("cls"); main(); break; } default: system("cls"); cout<<">>>>>>>> 输入错误!\n\n\n"; Sleep(1000); system("cls"); break; } } } void search() { system("cls"); f=1; char a,tmp[100],tmp1[30]; time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); while(f) { if(f==0) break; if(flag == 0) { f=0; system("cls"); main(); } cout<<"\n\n 您正在使用班次查询功能。 \n\n" <<"\t\t *******************************\n" <<"\t\t * 请选择你要查询的类型: *\n" <<"\t\t *******************************\n" <<"\t\t * 1、班次号 *\n" <<"\t\t * 2、起点和出发时间 *\n" <<"\t\t * 3、终点和到达时间 *\n" <<"\t\t * 4、返回 *\n" <<"\t\t *******************************\n" <<"\t\t "; cin>>a; switch(a) { case '1': fflush(stdin); system("cls"); cout<<" 请输入班次号: "; cin.getline(tmp,50); while(1) { int flag1=0; char *s; string str,st1,st2,st3,st4,st5,st6,st7,st8; str=" "; s =&str[0]; ifstream myfile("car.txt"); while(!myfile.eof()) { getline(myfile,str,' '); getline(myfile,st1,' '); getline(myfile,st2,' '); getline(myfile,st3,' '); getline(myfile,st4,' '); getline(myfile,st5,' '); getline(myfile,st6,' '); getline(myfile,st7,' '); getline(myfile,st8,'\n'); if(strcmp(tmp,s)==0) { cout<<"\n 车次 : "<<str<<"\t车号 : "<<st1<<"\t最大载客数 : "<<st2<<" 人" <<"\n 始发地 : "<<left<<setw(20)<<st3<<"\t出发时间 : "<<st4 <<"\n 目的地 : "<<left<<setw(20)<<st5<<"\t到达时间 : "<<st6 <<"\n 票 价 : "<<st7<<" 元\t\t"<<"已订票人数: "<<st8<<" 人\n"; flag1=1; } } cout<<" 当前时间 : " <<timeinfo->tm_hour<<":"<<timeinfo->tm_min; myfile.close(); if(!flag1) cout<<"\n 对不起,找不到该班车信息 (>.<) \n\n"; comeon(); search(); break; } case '2': fflush(stdin); system("cls"); cout<<" 请输入起始点: "; cin.getline(tmp,50); cout<<" 请输入出发时间(请英文状态下输入): "; cin.getline(tmp1,50); while(1) { int flag1=0; char *s,*p; string str,str1,st1,st2,st3,st4,st5,st6,st7; str=str1=" "; s =&str[0]; p =&str1[0]; ifstream myfile("car.txt"); while(!myfile.eof()) { getline(myfile,st1,' '); getline(myfile,st2,' '); getline(myfile,st3,' '); getline(myfile,str,' '); getline(myfile,str1,' '); getline(myfile,st4,' '); getline(myfile,st5,' '); getline(myfile,st6,' '); getline(myfile,st7,'\n'); if(strcmp(tmp,s)==0 && strcmp(tmp1,p)==0) { cout<<"\n 车次 : "<<st1<<"\t车号 : "<<st2<<"\t最大载客数 : "<<st3<<" 人" <<"\n 始发地 : "<<left<<setw(20)<<str<<"\t出发时间 : "<<str1 <<"\n 目的地 : "<<left<<setw(20)<<st4<<"\t到达时间 : "<<st5 <<"\n 票 价 : "<<st6<<" 元\t\t"<<"已订票人数: "<<st7<<" 人\n"; flag1=1; } } cout<<" 当前时间 : " <<timeinfo->tm_hour<<":"<<timeinfo->tm_min; myfile.close(); if(!flag1) cout<<"\n 对不起,找不到该班车信息 \n"; comeon(); search(); break; } case '3': fflush(stdin); system("cls"); cout<<" 请输入目的地: "; cin.getline(tmp,100); cout<<" 请输入到达时间(请英文状态下输入):: "; cin.getline(tmp1,50); while(1) { int flag1=0; char *s,*p; string str,str1,st1,st2,st3,st4,st5,st6,st7; str=str1=" "; s =&str[0]; p =&str1[0]; ifstream myfile("car.txt"); while(!myfile.eof()) { getline(myfile,st1,' '); getline(myfile,st2,' '); getline(myfile,st3,' '); getline(myfile,st4,' '); getline(myfile,st5,' '); getline(myfile,str,' '); getline(myfile,str1,' '); getline(myfile,st6,' '); getline(myfile,st7,'\n'); if(strcmp(tmp,s)==0 && strcmp(tmp1,p)==0) { cout<<"\n 车次 : "<<st1<<"\t车号 : "<<st2<<"\t最大载客数 : "<<st3<<" 人" <<"\n 始发地 : "<<left<<setw(20)<<st4<<"\t出发时间 : "<<st5 <<"\n 目的地 : "<<left<<setw(20)<<str<<"\t到达时间 : "<<str1 <<"\n 票 价 : "<<st6<<" 元\t\t\t"<<"已订票人数: "<<st7<<" 人\n"; flag1=1; } } cout<<" 当前时间 : " <<timeinfo->tm_hour<<":"<<timeinfo->tm_min; myfile.close(); if(!flag1) cout<<"\n 对不起,找不到该班车信息 \n"; comeon(); search(); break; } case '4': system("cls"); main(); break; default: system("cls"); cout<<">>>>>>>> 输入错误!\n\n\n"; Sleep(1000); system("cls"); break; } } } void book() { system("cls"); f=1; char a,tmp[100],tmp1[30]; time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); while(f) { if(flag == 0) { f=0; system("cls"); main(); } cout<<"\n\n 您正在使用班次订票功能 \n"; cout<<"\t\t *******************************\n" <<"\t\t * 请选择你要查询的类型: *\n" <<"\t\t *******************************\n" <<"\t\t * 1、订车票 *\n" <<"\t\t * 2、返回 *\n" <<"\t\t *******************************\n"; cin>>a; switch(a) { case '1': fflush(stdin); system("cls"); cout<<" 请输入目的地: "; cin.getline(tmp,100); cout<<" 请输入到达时间(请英文状态下输入): "; cin.getline(tmp1,50); while(1) { int flag1=0; char *s,*p,b; string str,str1,st1,st2,st3,st4,st5,st6,st7; str=str1=" "; s =&str[0]; p =&str1[0]; ifstream myfile("car.txt"); while(!myfile.eof()&&flag==1) { getline(myfile,st1,' '); getline(myfile,st2,' '); getline(myfile,st3,' '); getline(myfile,st4,' '); getline(myfile,st5,' '); getline(myfile,str,' '); getline(myfile,str1,' '); getline(myfile,st6,' '); getline(myfile,st7,'\n'); if(strcmp(tmp,s)==0 && strcmp(tmp1,p)==0) { cout<<"\n 车次 : "<<st1<<"\t车号 : "<<st2<<"\t最大载客数 : "<<st3<<" 人" <<"\n 始发地 : "<<left<<setw(20)<<st4<<"\t出发时间 : "<<st5 <<"\n 目的地 : "<<left<<setw(20)<<str<<"\t到达时间 : "<<str1 <<"\n 票 价 : "<<st6<<" 元\t\t\t"<<"已订票人数: "<<st7<<" 人\n"; flag1=1; } } myfile.close(); if(flag1) { tmp[0]=' '; cout<<"以上为班车信息!" <<"是否订票? Y/N\n"; cin>>b; } if(b=='y'||b=='Y') { ofstream file; ofstream tmpfile; ifstream myfile("car.txt"); tmpfile.open("bak.txt",ios::trunc); file.open("passenger.txt",ios::app); string tmp1,tmp,num,max,ttl; tmp=" "; int i=1,flag=0; char *pp,*p; passenger *head,*p1,*p2; head = NULL; p1 = new(passenger); p2 = p1; pp=&tmp[0]; system("cls"); cout<<"请输入对应的信息>>>>>>>>>>>>>\n" <<" 车 次 :"; cin>>p1->numbers; while(!myfile.eof()) { getline(myfile,tmp,' '); if(strcmp(pp,p1->numbers)==0) { flag=1; for(i=1;i<8;i++) { getline(myfile,num,' '); if(i==2) max=num; if(i==4) ttl=num; p=&(ttl[0]); } getline(myfile,num,'\n'); if(gethour(p)<timeinfo->tm_hour) { file.close(); myfile.close(); tmpfile.close(); cout<<"\n 对不起,该班次已发出\n"; comeon(); book(); break; } else if(gethour(p)<=timeinfo->tm_hour) { if(getmin(p)<=timeinfo->tm_min) cout<<"\n 对不起,该班次已发出\n"; file.close(); myfile.close(); tmpfile.close(); comeon(); book(); break; } if(atoi(&num[0])>atoi(&max[0])) { file.close(); myfile.close(); tmpfile.close(); cout<<"您所订的班次已订满\n"; comeon(); book(); break; } break; } getline(myfile,tmp1,'\n'); } myfile.close(); if(flag==0) { cout<<"该班次不存在!\n"; file.close(); myfile.close(); tmpfile.close(); comeon(); book(); } cout<<" 姓 名 :"; cin>>p1 ->name; cout<<" 身份证号码:"; cin>>p1->id; cout<<" 订 票 数:"; cin>>p1->ticket; if(atoi(&num[0])+atoi(&(p1->ticket[0]))>atoi(&max[0])) { file.close(); myfile.close(); tmpfile.close(); cout<<" 您无法订购过多的票,剩余票数已不足\n" <<" 您最多只能订购 "<<atoi(&max[0])-atoi(&num[0])<<" 张票\n"; comeon(); book(); } file<<p1->numbers<<" "<<p1->name<<" " <<p1->id<<" "<<p1->ticket<<" "; cout<<" 座 位 号:" <<atoi(&(num[0]))+1<<" --- " <<atoi(&(num[0]))+atoi(&(p1->ticket[0]))<<'\n'; file<<atoi(&(num[0]))+atoi(&(p1->ticket[0]))<<'\n'; cout<<" 订票成功!请准时上车!\n"; file.close(); ifstream ff("car.txt"); while(!ff.eof()) { string str,str1,str2; int tt,flag=0; for(i=0;i<8;i++) { getline(ff,str,' '); if(strcmp(&str[0],p1->numbers)==0) flag=1; tmpfile<<str<<' '; } getline(ff,str1,'\n'); if(flag) { flag=0; tt=atoi(&str1[0])+atoi(&(p1->ticket[0])); tmpfile<<tt<<'\n'; getline(ff,str,'\0'); tmpfile.close(); } else tmpfile<<str1<<'\n'; } ff.close(); tmpfile.close(); ofstream f1("car.txt",ios::trunc); ifstream f2("bak.txt"); while(!f2.eof()) { string s; getline(f2,s,'\0'); f1<<s; } f1.close(); f2.close(); comeon(); book(); } else if(b=='n'||b=='N') { comeon(); book(); } else { system("cls"); cout<<">>>>>>>> 输入错误\n\n\n"; Sleep(1000); system("cls"); book(); } if(!flag1) { cout<<"\n 对不起,找不到该班车信息 \n"; comeon(); book(); break; } } case '2': system("cls"); main(); break; default: system("cls"); cout<<">>>>>>>> 输入错误\n\n\n"; Sleep(1000); system("cls"); break; } } } int comeon() { char answer,a=0; do{ cout<<"\n 是否继续操作? Y/N "; fflush(stdin); cin>>answer; if(answer=='y'||answer=='Y') { system("cls"); f = 1; flag = 1; return 1; } else if(answer=='n'||answer=='N') { flag =0; system("cls"); break; } else { system("cls"); cout<<"输入错误\n\n"; a=1; } }while(a); return 0; } int gethour(char *s) { char *p,*q; p=(char*)malloc(10); strcpy(p,s); q = p; while(*q!=':') q++; *q='\0'; return atoi(p); } int getmin(char *s) { char *p; p=(char*)malloc(10); strcpy(p,s); while(*p!=':') p++; p++; return atoi(p); }
选择1
选择3
1
选择退票
再增加班次,订票然后退票
退票前
退票后
经过测试,基本达到要求。
0 0
- C程序设计第二章
- 第二届北京林业大学“计蒜客”杯程序设计竞赛 C题 Candy
- c++程序设计第二题
- 程序设计第二题
- 经典《C程序设计第二版--谭浩强》 视频
- 谭浩强著《C程序设计(第二版)》习题13.5
- 谭浩强著《C程序设计(第二版)》习题13.6
- 第二章总结 C/C++程序设计
- c程序设计第二版学习笔记
- C程序设计(第二版 新版)第二章 习题
- C语言标准库函数(选自C程序设计第二版)
- 高质量程序设计指南C++/c语言 第二章
- 2015年第六届蓝桥杯C/C++程序设计本科B组决赛第二题
- C程序设计(第二版)第六章习题
- C程序设计(第二版)第七章习题
- C程序设计(第二版) 第八章习题
- C程序设计(第二版) 第十章习题
- C程序设计(第二版 新版)第六章 习题
- Spring3.0官网文档学习笔记(六)--3.4.1
- 在已有win7系统下安装ubuntu遇到的一些问题
- Socket网络编程
- hdu1238
- C++课程设计----ATM机
- c++程序设计第二题
- 顶点和片段着色器
- ZwSystemDebugControl函数R3读R0的内核数据
- CSS hack 如何区分所有IE浏览器和非IE浏览器
- json对象结合for in,for in中遍历到的是json对象的key 而不是value
- Android入门
- C++ 中string函数的用法总结(转载)
- Java解惑七:更多类之谜
- 蛋黄饼干