C++ 班车信息管理系统

     C++ 班车信息管理系统

#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<<"\t\t___________________________________________\n"
		<<"\t\t|\t\t欢迎使用本系统            |\n"
		<<"\t\t|------------------------------------------\n"
		<<"\t\t|请选择需要的功能。\t\t          |\n"
		<<"\t\t|1、增加班次记录。\t\t          |\n"
		<<"\t\t|2、退订车票。    \t\t          |\n"
		<<"\t\t|3、班次查询。    \t\t          |\n"
		<<"\t\t|4、班次订票。    \t\t          |\n"
		<<"\t\t|5、作者信息        \t\t          |\n"
		<<"\t\t|6、退出        \t\t          |\n"
		<<"\t\t|_________________________________________|\n"
		<<"\n\t\t请输入你数字1~6来选择你需要的功能!!!\n\t\t";
	for(;key>'6'||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':
			{	//显示作者信息
				int i=0;
				char a[]="\t\t作者:\t黄浩\n\t\t专业:\t自动化1121\n\t\t欢迎您的使用!!\n\t\t\t ^.^ \n";
				system("cls");
				for(i;i<58;i++)
				{
					Sleep(100);
					cout<<a[i];
				}
				Sleep(1000);
				system("cls");
				main();
				break;
			}
		case '6':
			system("cls");
			cout<<"\t\t\t\t欢迎您的再次使用!!!!\n\n"
				<<"\t\t\t\t   (^▽^)         \n\n"
				<<"\t\t\t\t   SEE YOU!!!   \n\n\n";
			exit(1);
			break;
			
		default:							//人性化
			system("cls");
			cout<<"\t\t (>.<)  亲,没有这项功能啊!! \n\n\n";
			Sleep(1000);
			system("cls");
			main();
			break;
			
		}
	}	
}

/*----------------我是华丽丽丽丽丽的分割线--------------*/
/*                  功能1:增加班次记录                 */
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();
		}
	}
}
/*----------------我是华丽丽丽丽丽的分割线--------------*/
/*                    功能2:车票退订                   */
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    m(._.)m    您正在使用班次取消功能。 m(._.)m\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,' ');		//读取TXT中第一个班次号
						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;						//退出if循环
						}
						getline(myfile,num,'\n');  //与退订班次不同,将TXT中这一行直接获取。
					}
					myfile.close();					//关闭文件
					if(atof(&num[0])/atof(&max[0])<=0.1)		//如果已订票人数小于最大载客数10%
						ff=1;									//则标记 ff=1
					else ff=0;									//否则 标记为 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())				//开始从txt查询信息
						{
							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())		//读取车辆信息txt
						{	
							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;
		}
		
		
	}
}

/*----------------我是华丽丽丽丽丽的分割线--------------*/
/*                  功能3:查询班次记录                 */
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);
}


首先我们来看一下主界面!!清爽无比!!


增加班次记录界面!!


退订车票界面!!!


班次查询界面!!!


订车票界面!!!


作者信息界面!!!





退出界面!!!


我们来增加班次信息!!!这里 车号,起点,终点都是可以用中文的。

当信息录入后,就会提示是否继续操作,

若输入 Y 或 y则会返回到增加班次记录界面,

若输入N 或 n 则会返回到主界面

若输入错误则会提示


班次信息输入后就会保存在car.txt文本中。

从图中我们可以看到,第一个为班车的班次,第二个为班车的车号

第三个为最大载客数,第四个为出发地,第五个为 出发时间

第六个为目的地,第七个为到达时间,第八个为票价,第九个为订票人数。

每个信息之间用空格隔开,清晰明了。



我们现在来检验一下是否能够查询到刚才我们输入的班车信息!

  我们先按班次号来查询!!由于文本中只存在K02的班车,所以提示查找不到。

这一次我们输入K02。结果出来了。信息一览无误。下方还显示了当前时间。

接下来我们按起点和出发时间来查询!结果显而易见!

我们再试试用终点和到达时间来查询。也是能查询得到的。

我们返回到主界面,测试订票功能。

假设我们要打霞山校区,而且要在9点到达。

系统显示了班次信息,并询问是否订票。

我们选择订票。从上面得知要坐的车为K02 ,输入K02,提示已发出。

在这里我回去继续添加了班车信息,以便于我们继续测试。过程自己脑补。

我们选择要去霞山校区,21:00到达。

这里继续选择订票。

这里就借王大锤哥哥的名字来订票。结果显示成功。而且提示了座位号为1-2.

那么我们来看一下是否已经记录下来我们的订票信息呢。

从图中可以看出,我们的订票信息已经记录在了passenger.txt中。


我们再来看看,记录班车信息的文本有木有变化。

这是订票前的。

这是订票后的。我们可以发现,在K01班次里,订票人数变为了2 ,说明系统更新了文本信息。

这真是极好了。

最后我们来看一下退票功能吧!输入班次号,显示信息。可以退订。


我们输入名字。王大锤。显示退订成功。

如果已发车的呢。输入后发现,无法退订,而且提示联系前台。

这里就可以交给我们亲爱的乘务员了。

最后最后,我们来看一下记录的文本是否有变化。

这是为退订前的文本记录。

这是退订后的文本记录,说明系统自己又更新了文本信息。这就达到了我们预期的效果了。

整个系统大概就是这样。

在做课程设计的过程中,遇到了种种问题,不过在自己的努力下,都克服了。当遇到不懂的就自行百度。我觉得课堂上老师教的东西实在是太少了。做这个课程设计,能用到课堂上的内容不到一半。而且C++的 面向过程都没讲到,这对于我们来说无疑是最大的损失,或许是我们专业没有要求。希望老师以后能够在教新同学的时候多讲解一下课外内容,让课堂更有活力,而不是单纯且所谓的与学生互动就是叫学生起来回答问题。最后感谢一学期来明哥的教导。的确辛苦了,每次博客作业都有认真看。带着我们走向C++编程之路也就到这里了,以后的路还得靠我们自己去走。好了,上课去。


  • 10
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值