#include<iostream>
#include<fstream>
#include<cstring>
#include<iomanip>
#define Maxsize 100
using namespace std;
struct Route
{
char number[20];
char startandreach[20];
char type[20];
char longroute[20];
char time[20];
double price;
int sitnumber;
int save;
int sale;
};
struct Ticketnews
{
char type[20];
char time[20];
int booked;
int remained;
};
struct Booknews
{
char name[20];
char identitycard[20];
char number[20];
char date[20];
char road[20];
int sitnumber;
};
void Match(Route L1[],Ticketnews L2[],int n)//票务信息保存
{
ofstream outfile("票务信息.txt",ios::out);
if (!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
int i;
for(i=0;i<n;i++)
{
strcpy(L2[i].type,L1[i].type);
strcpy(L2[i].time,L1[i].time);
L2[i].booked=L1[i].sale;
L2[i].remained=L1[i].save;
outfile<<L2[i].type<<" "<<L2[i].time<<" "<<L2[i].booked<<" "<<L2[i].remained<<endl;
}
outfile.close();
}
int Book(Booknews &L1,Route &L2,int s)//这里一定要是参数传递
{
ofstream outfile("买票客户信息.txt",ios::app);//怎么可以重复的写入订票人的数据
if (!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
cout<<"请输入订票信息:"<<endl;
cout<<"姓名:";cin>>L1.name;
cout<<"身份证:";cin>>L1.identitycard;
cout<<"车次:";cin>>L1.number;
cout<<"日期:";cin>>L1.date;
cout<<"路线:";cin>>L1.road;
L2.sale++;
L2.save--;
L1.sitnumber=L2.sale;
cout<<"订票信息为:"<<endl;
cout<<"姓名:"<<L1.name<<endl<<"身份证:"<<L1.identitycard<<endl<<"车次:"<<L1.number<<endl<<"日期:"<<L1.date<<endl<<"座位号:"<<L1.sitnumber<<endl<<"路线:"<<L1.road<<endl;
outfile<<L1.name<<" "<<L1.identitycard<<" "<<L1.number<<" "<<L1.date<<" "<<L1.sitnumber<<" "<<L1.road<<endl;
outfile.close();
s++;
return s;
}
int Return(Booknews L1[],int s)
{
Booknews L;
con:cout<<"请输入退票者姓名,身份证";
cin>>L.name>>L.identitycard;
int i;
for(i=0;i<s;i++)
{
if(((strcmp(L.name,L1[i].name))&&(strcmp(L.identitycard,L1[i].identitycard))))
{
cout<<"这个人没有订票!"<<endl;
return 0;
}
else
{
int j=i;
cout<<"该订票者的信息为:";
cout<<"姓名:"<<L1[i].name<<endl;
cout<<"身份证:"<<L1[i].identitycard<<endl;
cout<<"车次:"<<L1[i].number<<endl;
cout<<"订票日期:"<<L1[i].date<<endl;
cout<<"座位号:"<<L1[i].sitnumber<<endl;
cout<<"路线:"<<L1[i].road<<endl;
cout<<"以上信息是否符合?符合请按1,不符合请按2:";
int k;
cin>>k;
if(k==2) goto con;
else
/*cout<<"输入订票者的车次,座位号和日期:";
cin>>L.number>>L.sitnumber>>L.date;
for(i=L.sitnumber-1;i<s;i++)
{
strcpy(L1[i].name,L1[i+1].name);
strcpy(L1[i].identitycard,L1[i+1].identitycard);
strcpy(L1[i].number,L1[i+1].number);
strcpy(L1[i].date,L1[i+1].date);
L1[i].sitnumber=L1[i+1].sitnumber;
cout<<"退票成功!"<<endl;
}*/
{
for(;i<s;i++)//删除退票者的信息
{
strcpy(L1[i].name,L1[i+1].name);
strcpy(L1[i].identitycard,L1[i+1].identitycard);
strcpy(L1[i].number,L1[i+1].number);
strcpy(L1[i].date,L1[i+1].date);
L1[i].sitnumber=L1[i+1].sitnumber;
}
cout<<"退票成功!"<<endl;
ofstream outfile("买票客户信息.txt",ios::out);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(i=0;i<j;i++)
{
outfile<<L1[i].name<<" "<<L1[i].identitycard<<" "<<L1[i].number<<" "<<L1[i].date<<" "<<L1[i].sitnumber<<endl;
}
outfile.close();
}
return j;
}
}
}
int main()
{
ifstream infile("所有车次路线信息.txt",ios::in);
if (!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
ifstream infile1("买票客户信息.txt",ios::in);//怎么可以重复的写入订票人的数据
if (!infile1)
{
cerr<<"open error!"<<endl;
exit(1);
}
int n,i,n1,s,j;
infile>>n;
infile1>>s;
infile1.close();
Route route[Maxsize];
Ticketnews ticketnews[Maxsize];
Booknews booknews[Maxsize];
for(i=0;i<n;i++)
{
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;
}
cout<<"输出所有汽车路线为:"<<endl;
cout<<"车次"<<setw(7)<<"路线"<<setw(18)<<"车型"<<setw(12)<<"路线长度"<<setw(7)<<"时间"<<setw(14)<<"价格"<<setw(8)<<"座位"<<
setw(5)<<"剩票"<<setw(5)<<"卖票"<<endl;
for(i=0;i<n;i++)
{
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)<<
route[i].price<<setw(4)<<route[i].sitnumber<<setw(4)<<route[i].save<<setw(4)<<route[i].sale<<endl;
//cout<<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<<endl;
}
Match(route,ticketnews,n);
cout<<"主菜单为:"<<endl;
con:cout<<"1 为订票,2 为退票,3 为改变路线,4为查询订票情况,5为退出该系统,请根据你所要实现的功能输入相应的数字:";
cin>>n1;
switch(n1)
{
case 1:
{
con1:cout<<"请输入你要订票的车次:";
cin>>booknews[s].number;
bool flag(true);//一定要用bool变量控制
for(i=0;i<n;i++)
{
if(!(strcmp(booknews[s].number,route[i].number))&&(route[i].save!=0))
{
//定义一个变量是否需要订票
flag = false;
cout<<booknews[s].number<<"车次还有票是否需要订票,是就输入yes,否就输入no:";
char k1[5];
con3:cin>>k1;
if(!strcmp(k1,"yes"))
{
s=Book(booknews[s],route[i],s);
}
else
break;
cout<<"是否还要订票,是就输入yes,不是就输入no:";
goto con3;
}
}
if(flag)
{
cout<<"你输入的车次不存在,请重新输入!"<<endl;
goto con1;
}
ofstream outfile("买票客户信息.txt",ios::out||ios::app);
if (!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
outfile<<s<<endl;//一定要加一行endl,在另一行添加才可
outfile.close();
cout<<"返回主菜单请按1,否则请按2:";
int kkk;
cin>>kkk;
if(kkk==1)goto con;
else
break;
}
case 2:
{
if(s==0){cout<<"目前没有人订票,不能退票!"<<endl;goto con;}
else
{
j=Return(booknews,s);
s--;
route[j].sale--;
route[j].save++;
cout<<"返回主菜单请按1,退出请按2:";
int kk1;
cin>>kk1;
if(kk1==1)goto con;
}
ofstream outfile("买票客户信息.txt",ios::out||ios::app);//怎么可以重复的写入订票人的数据
if (!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
outfile<<s<<endl;
outfile.close();
break;
}
case 3://用指针指向文件??
{
cout<<"请输入管理员密码:";
int code;
cin>>code;
if(code==123) //记住密码
{
con4:cout<<"请根据A为增加路线,B为删除路线,C为修改汽车路线信息输入相应的字母:";
char z;cin>>z;
if(z=='A')
{
infile.close();
con5:n++;//注意con5要指对地方
ofstream outfile("所有车次路线信息.txt",ios::out||ios::app);
if (!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
outfile<<n;//输出过后要在把n输进去????
//infile>>n;
if(!outfile.eof())//指向文件末尾
outfile.seekp(0,ios::end);
//outfile<<endl;
cout<<"请输入增加路线的所有数据:"<<endl;
cout<<"车次:";cin>>route[n-1].number;
cout<<"路线:";cin>>route[n-1].startandreach;
cout<<"车辆类型:";cin>>route[n-1].type;
cout<<"里程:";cin>>route[n-1].longroute;
cout<<"时间:";cin>>route[n-1].time;
cout<<"票价:";cin>>route[n-1].price;
cout<<"座位数:";cin>>route[n-1].sitnumber;
cout<<"剩余票数:";cin>>route[n-1].save;
cout<<"卖票数:";cin>>route[n-1].sale;
outfile<<route[n-1].number<<" "<<route[n-1].startandreach<<" "<<route[n-1].type<<" "<<route[n-1].longroute<<" "
<<route[n-1].time<<" "<<route[n-1].price<<" "<<route[n-1].sitnumber<<" "<<route[n-1].save<<" "<<route[n-1].sale<<endl;
outfile.close();
cout<<"是否还要增加路线,是请输入yes,退出请输入no:";
char kk3[5];
cin>>kk3;
if(strcmp(kk3,"no")) goto con5;
}
else
{
if(z=='B')
{
con6:cout<<"请输入你要删除的车次路线:";
char z1[20];int z2=-1;
cin>>z1;
for(i=0;i<n;i++)
{
if(!strcmp(z1,route[i].number))
{
z2=i;
break;
}
}
if(z2>0)
{
for(;z2<n-1;z2++)
{
strcpy(route[z2].number,route[z2+1].number);
strcpy(route[z2].startandreach,route[z2+1].startandreach);
strcpy(route[z2].type,route[z2+1].type);
strcpy(route[z2].longroute,route[z2+1].longroute);
strcpy(route[z2].time,route[z2+1].time);
route[z2].price=route[z2+1].price;
route[z2].sitnumber=route[z2+1].sitnumber;
route[z2].save=route[z2+1].save;
route[z2].sale=route[z2+1].sale;
}
n--;
ofstream outfile("所有车次路线信息.txt",ios::out);
if (!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
outfile<<n<<endl;
for(i=0;i<n;i++)
{
outfile<<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<<endl;
}
outfile.close();
con7:cout<<"是否还要删除路线,是就输入yes,否就输入no:";
char kk4[5];
cin>>kk4;
if(!strcmp(kk4,"yes"))goto con6;
else
if(!strcmp(kk4,"no"))break;
else
if(strcmp(kk4,"yes")) {cout<<"你输入的单词不符合要求,请重新输入:";goto con7;}
}
}
else
{
if(z=='C')
{
con2:cout<<"请输入你要修改的路线:";
char k[20],k2[20];int kk;
cin>>k;
for(i=0;i<n;i++)
{
if(!strcmp(k,route[i].startandreach))
{
cout<<"修改路线为:";
cin>>k2;
strcpy(route[i].startandreach,k2);
/*cout<<"是否还要修改,是就输入yes,否就输入no:";当想要把重复的路线修改时,就要用到的程序段
char kk4[5];
cin>>kk4;
if(strcmp(kk4,"yes")) break;*/
}
ofstream outfile("所有车次路线信息.txt",ios::out);
if (!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
outfile<<n<<endl;
for(i=0;i<n;i++)
{
outfile<<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<<endl;
}
outfile.close();
}
break;
con8:cout<<"是否还需要修改,是就选1,不是就选2:";
cin>>kk;
if(kk==1)goto con2;
else
if(kk==2)break;
else {cout<<"你输入的数字不符合要求,请重新输入:"; goto con8;}
}
else
{
cout<<"你输入的数字不符合要求,请重新输入:";
goto con4;
}
}
}
}
else
{
cout<<"你所输入的密码错误,返回主菜单请按1,退出请按2:";
int kk2;
cin>>kk2;
if(kk2==2)return 0;
else
goto con;
}
break;
}
case 4:
{
char k3[10],k4[20];
cout<<"请输入用户名和身份证号:";
cin>>k3>>k4;
for(i=0;i<s;i++)
{
if(!(strcmp(k3,booknews[i].name)&&strcmp(k4,booknews[i].identitycard)))
{
cout<<"你的订票信息为:"<<endl;
cout<<"姓名:"<<booknews[i].name<<endl;
cout<<"身份证号:"<<booknews[i].identitycard<<endl;
cout<<"车次:"<<booknews[i].number<<endl;
cout<<"座位号:"<<booknews[i].sitnumber<<endl;
cout<<"日期:"<<booknews[i].date<<endl;
}
}
}
return 0;
case 5:
{
return 0;
}
default:
{
cout<<"你所选的功能不存在,请重新再选择!"<<endl;
goto con;
}
}
cout<<"输出所有汽车路线为:"<<endl;
for(i=0;i<n;i++)
{
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)<<
route[i].price<<setw(4)<<route[i].sitnumber<<setw(4)<<route[i].save<<setw(4)<<route[i].sale<<endl;
/*cout<<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<<endl;*/
}
//goto con;
return 0;
}
自己设置的票务信息
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