#include<iostream> #include<stdio.h> #include<conio.h> #include<fstream> #include<string.h> using namespace std; #define N 20 #define Max 100 int n=10,m=10; struct Car//汽车路线 { char checi[N];//车次 char Sname[N];//始发站 char Lname[N];//终点站 char type[N];//车辆类型 long length;//里程 double Stime;//出站时间 double Ltime;//到站时间 int price;//票价 int zuonum;//座位数 }car1[100]; struct Ticket//票务信息 { char checi[N];//车次 double timelong;//时间 int sellticketnum;//已订票数 int Nsellticketnum;//剩余票数 }ticket1[100]; struct information//订票信息 { char name[N];//姓名 char identity[N];//身份证号 char checi[N];//车次 char Date[N];//日期 int zuoNum;//座位号 }number[100]; void inputCar()//汽车路线数据读入 { ifstream iofile("checi.txt",ios::in); for(int i=0;i<m;i++) iofile>>car1[i].checi>>car1[i].Sname>>car1[i].Lname>>car1[i].type >>car1[i].length>>car1[i].Stime>>car1[i].Ltime>>car1 [i].price>>car1[i].zuonum; iofile.close(); } void outputcar1()//汽车路线数据输出 { for(int i=0;i<m;i++) cout<<car1[i].checi<<" "<<car1[i].Sname<<" "<<car1[i].Lname<<" "<<car1 [i].type <<" "<<car1[i].length<<" "<<car1[i].Stime<<" "<<car1[i].Ltime<<" "<<car1[i].price<<" "<<car1[i].zuonum<<endl; } void inputTicket()//票务信息数据输入 { ifstream iofile("ticket1.txt",ios::in); for(int i=0;i<m;i++) iofile>>ticket1[i].checi>>ticket1[i].timelong>>ticket1 [i].sellticketnum>>ticket1[i].Nsellticketnum; iofile.close(); } void outputTicket()//票务信息数据输出 { for(int i=0;i<m;i++) cout<<ticket1[i].checi<<" "<<ticket1[i].timelong<<" "<<ticket1 [i].sellticketnum<<" "<<ticket1[i].Nsellticketnum<<endl; } void Inquire_Ticket()//车次查询 { information number1; cout<<"请输入要查询的车次"<<endl; cin>>number1.checi; for(int i=0;i<m;i++) { if(strcmp(number1.checi,ticket1[i].checi)==0) { cout<<"信息如下:"<<endl; cout<<"车次: "<<ticket1[i].checi<<" 历时: "<<ticket1[i].timelong<<" 已售票数: "<<ticket1[i].sellticketnum <<" 剩余票数: "<<ticket1[i].Nsellticketnum<<endl <<" 出站地点: "<<car1[i].Sname<<" 到站地点: "<<car1[i].Lname<<" 车 辆类型: "<<car1[i].type<<" 路程: "<<endl <<car1[i].length<<"千米 开始时间: "<<car1[i].Stime<<"小时 到站时间: "<<car1[i].Ltime<<"小时 票价: " <<car1[i].price<<"元 总座位数: "<<car1[i].zuonum<<endl; break; } } if(i==m) cout<<"无此车次"<<endl; } void outputTicket_file()//票务信息用文件输出 { ofstream outfile; outfile.open("ticket1.txt",ios::out); for(int i=0;i<m;i++) outfile<<ticket1[i].checi<<" "<<ticket1[i].timelong<<" "<<ticket1 [i].sellticketnum<<" "<<ticket1[i].Nsellticketnum<<" "<<endl; outfile.close(); } void inputinformation()//订票信息数据输入 { ifstream iofile("number.txt",ios::in); for(int i=0;i<n;i++) iofile>>number[i].name>>number[i].identity>>number[i].checi>>number [i].Date>>number[i].zuoNum; iofile.close(); } void outputinformation()//订票信息数据输出 { for(int i=0;i<n;i++) cout<<number[i].name<<" "<<number[i].identity<<" "<<number[i].checi<<" "<<number [i].Date<<" "<<number[i].zuoNum<<endl; } void outputinformation_file()//订票信息用文件输出 { ofstream outfile("number.txt",ios::out); for(int j=0;j<n;j++)//将数组的信息读到文件里,更新数据 outfile<<number[j].name<<" "<<number[j].identity<<" "<<number[j].checi<<" "<<number[j].Date<<" "<<number[j].zuoNum<<endl; outfile.close(); } void Book_ticket()//建立订票功能 { information number1; cout<<"请输入订票信息:(姓名,身份证号,车次,日期,座位号)"<<endl; cin>>number1.name>>number1.identity>>number1.checi>>number1.Date>>number1.zuoNum; for(int i=0;i<n;i++) { if(strcmp(number1.checi,ticket1[i].checi)==0) { if(ticket1[i].Nsellticketnum==0) cout<<"对不起,客户已满"<<endl; else { cout<<"订票成功"<<endl<<endl; strcpy(number[n].name,number1.name);//复制信息到数组里 strcpy(number[n].identity,number1.identity); strcpy(number[n].checi,number1.checi); strcpy(number[n].Date,number1.Date); number[n].zuoNum=number1.zuoNum; n++;//长度加1 outputinformation_file(); ticket1[i].sellticketnum++;//已定票数加1 ticket1[i].Nsellticketnum--;//未定票数减1 outputTicket_file(); //outputinformation(); } break; } } } void Return_ticket()//退票功能 { int i,j; information number1; cout<<"请输入退票信息:(姓名,身份证号,车次,日期,座位号)"<<endl; cin>>number1.name>>number1.identity>>number1.checi>>number1.Date>>number1.zuoNum; for(i=0;i<n;i++) { if(strcmp(number1.name,number[i].name)==0) break; } if(i==n) { cout<<"退票不成功:"<<endl; return; } for(j=0;j<m;j++) { if(strcmp(number1.checi,ticket1[j].checi)==0) break; } for(;i<n;i++) { strcpy(number[i].name,number[i+1].name);//复制信息到数组里 strcpy(number[i].identity,number[i+1].identity); strcpy(number[i].checi,number[i+1].checi); strcpy(number[i].Date,number[i+1].Date); number[i].zuoNum=number[i+1].zuoNum; } n--; outputinformation_file(); ticket1[j].sellticketnum--;//已定票数加1 ticket1[j].Nsellticketnum++;//未定票数减1 outputTicket_file(); cout<<"恭喜你,退票成功"<<endl<<endl; } void AddCarData()//路线增添 { ofstream outfile("checi.txt",ios::out); cout<<"请输入增添的路线(车次,路程,开始时间,到站时间,票价,始点站,终点站,车辆 类型,座位数):"<<endl; cin>>car1[m].checi>>car1[m].Sname>>car1[m].Lname>>car1[m].type>>car1 [m].length>>car1[m].Stime>>car1[m].Ltime>>car1[m].price>>car1[m].zuonum; strcpy(ticket1[m].checi,car1[m].checi); ticket1[m].Nsellticketnum=car1[m].zuonum; ticket1[m].sellticketnum=0; ticket1[m].timelong=car1[m].Ltime-car1[m].Stime; m++; for(int i=0;i<m;i++) outfile<<car1[i].checi<<" "<<car1[i].Sname<<" "<<car1[i].Lname<<" "<<car1 [i].type<<" "<<car1[i].length <<" "<<car1[i].Stime<<" "<<car1[i].Ltime<<" "<<car1[i].price<<" "<<car1[i].zuonum<<endl; outputTicket_file(); outfile.close(); } void DeleteCarData()//路线删除 { Car cartemp; int i; cout<<"请输入删除的车次"<<endl; cin>>cartemp.checi; for(i=0;i<m;i++) if(strcmp(cartemp.checi,car1[i].checi)==0) break; if(i==m) { cout<<m<<endl; cout<<"找不到该车次,删除失败。"<<endl; return; } m--; for(;i<m;i++) { strcpy(car1[i].checi,car1[i+1].checi);//比较函数跟复制函数写混了啊 car1[i].length=car1[i].length; strcpy(car1[i].Sname,car1[i+1].Sname); strcpy(car1[i].Lname,car1[i+1].Lname); car1[i].Stime=car1[i+1].Stime; car1[i].Ltime=car1[i+1].Ltime; car1[i].price=car1[i+1].price; strcmp(car1[i].type,car1[i+1].type); car1[i].zuonum=car1[i+1].zuonum; strcpy(ticket1[i].checi,ticket1[i+1].checi); ticket1[i].Nsellticketnum=ticket1[i+1].Nsellticketnum; ticket1[i].sellticketnum=ticket1[i+1].sellticketnum; ticket1[i].timelong=ticket1[i+1].timelong; } ofstream outfile("checi.txt",ios::out); for(i=0;i<m;i++) outfile<<car1[i].checi<<" "<<car1[i].Sname<<" "<<car1[i].Lname<<" "<<car1 [i].type <<" "<<car1[i].Stime<<" "<<car1[i].Ltime<<" "<<car1[i].price<<" "<<car1[i].zuonum<<endl; outputTicket_file(); outfile.close(); cout<<"删除成功!!!"<<endl<<endl; } void UpCarData()//路线更改 { Car cartemp; int i; cout<<"请输入要更改的车次"<<endl; cin>>cartemp.checi; for(i=0;i<m;i++) if(strcmp(cartemp.checi,car1[i].checi)==0) break; if(i==m) { cout<<"找不到该车次,更改失败。"<<endl; return; } cout<<"请输入新的路线(车次,路程,开始时间,到站时间,票价,始点站,终点站,车辆类 型,座位数):"<<endl; cin>>car1[i].checi>>car1[i].Sname>>car1[i].Lname>>car1[i].type>>car1 [i].length>>car1[i].Stime>>car1[i].Ltime>>car1[i].price>>car1[i].zuonum; strcpy(ticket1[i].checi,car1[i].checi); ticket1[i].Nsellticketnum=car1[i].zuonum; ticket1[i].sellticketnum=0; ticket1[i].timelong=car1[i].Ltime-car1[i].Stime; ofstream outfile("checi.txt",ios::out); for(i=0;i<m;i++) outfile<<car1[i].checi<<" "<<car1[i].Sname<<" "<<car1[i].Lname<<" "<<car1 [i].type<<" "<<car1[i].length <<" "<<car1[i].Stime<<" "<<car1[i].Ltime<<" "<<car1[i].price<<" "<<car1[i].zuonum<<endl; outputTicket_file(); outfile.close(); cout<<"更改成功!!!"<<endl<<endl; } void Change_Car() { int i; cout<<"请选择操作:(1.增添路线,2.删除路线,3.更改路线)"<<endl; cin>>i; switch(i) { case 1:AddCarData();break; case 2:DeleteCarData();break; case 3:UpCarData();break; } } void Copyfile()//将三个文本文档的数据还原,借用临时文档 { int i=0; m=10; n=10; ifstream infile;//,infile2,infile3; ofstream outfile; infile.open("checi2.txt",ios::in); for(i=0;i<m;i++) infile>>car1[i].checi>>car1[i].Sname>>car1[i].Lname>>car1[i].type >>car1[i].length>>car1[i].Stime>>car1[i].Ltime>>car1[i].price>>car1 [i].zuonum; infile.close(); //outputcar1(); infile.clear();//没加之前就乱码啊,什么指针的啊, outfile.open("checi.txt",ios::out); for(i=0;i<m;i++) outfile<<car1[i].checi<<" "<<car1[i].Sname<<" "<<car1[i].Lname<<" "<<car1 [i].type <<" "<<car1[i].length<<" "<<car1[i].Stime<<" "<<car1[i].Ltime<<" "<<car1 [i].price<<" "<<car1[i].zuonum<<endl; outfile.close(); infile.open("ticket2.txt",ios::in); for(i=0;i<m;i++) infile>>ticket1[i].checi>>ticket1[i].timelong>>ticket1 [i].sellticketnum>>ticket1[i].Nsellticketnum; //outputTicket(); outputTicket_file(); infile.close(); infile.clear(); infile.open("number2.txt",ios::in); for(i=0;i<n;i++) infile>>number[i].name>>number[i].identity>>number[i].checi>>number [i].Date>>number[i].zuoNum; outputinformation_file(); infile.close(); //outputinformation(); } void main() { int i; char a[7],b[7]={'y','a','n','y','a','n'};//自动加反斜杠0 bool flag=true; system("color FC"); inputCar(); //outputcar1(); inputTicket(); //outputTicket(); inputinformation(); //outputinformation(); system("title 简单车票管理系统-myth"); system("color 98"); printf(" -------------------------------------------- \n"); printf(" | | \n"); printf(" | Welcome to MIS of Tickrt | \n"); printf(" | myth | \n"); printf(" -------------------------------------------- \n"); printf(" Please input a number(0..9) to continue!"); scanf("%d",&i); system("cls"); cout<<"请输入登陆密码(由6个字符组成):"<<endl; while(flag) { for(i=0;i<6;i++) { a[i]=getch(); putch('*'); } a[6]='\0';//不会自动加反斜杠0啊,自己来 cout<<endl; if(strcmp(a,b)==0) { cout<<"恭喜你,密码正确"<<endl<<endl; flag=0; } else cout<<"So bad!!!请重新输入密码(由6个字符组成):"<<endl; } int select; do { cout<<"请选择:"<<endl; cout<<"1.定购车票"<<endl; cout<<"2.退票"<<endl; cout<<"3.票价查询"<<endl; cout<<"4.修改路线"<<endl; cout<<"5.退出:"<<endl; cin>>select; switch(select) { case 1: Book_ticket(); break; case 2: Return_ticket(); break; case 3: Inquire_Ticket(); break; case 4: Change_Car(); break; } }while(select!=5); Copyfile(); getch(); cout<<"谢谢使用!"<<endl; } /*checi k_6150 nanchang chongqing changtu 3500000 0 24.3 170 103 k_6151 nanchang chongqing changtu 3500000 1 25.3 171 103 k_6152 nanchang chongqing changtu 3500000 2 26.3 172 103 k_6153 nanchang chongqing changtu 3500000 3 27.3 173 103 k_6154 nanchang chongqing changtu 3500000 4 28.3 174 103 k_6155 nanchang chongqing changtu 3500000 5 29.3 175 103 k_6156 nanchang chongqing changtu 3500000 6 30.3 176 103 k_6157 nanchang chongqing changtu 3500000 7 31.3 177 103 k_6158 nanchang chongqing changtu 3500000 8 32.3 178 103 k_6159 nanchang chongqing changtu 3500000 9 33.3 179 103 */ /*number myth 0113470 k_6150 2012/5/6 45 liua 0113471 k_6151 2012/5/6 46 shen 0113472 k_6152 2012/5/6 47 huge 0113473 k_6153 2012/5/6 48 ting 0113474 k_6154 2012/5/6 49 lili 0113475 k_6155 2012/5/6 60 yanz 0113476 k_6156 2012/5/6 61 zhou 0113477 k_6157 2012/5/6 62 chen 0113478 k_6158 2012/5/6 66 bing 0113479 k_6159 2012/5/6 68 */ /*ticket1 k_6150 24.3 45 68 k_6151 24.3 46 67 k_6152 24.3 47 66 k_6153 24.3 48 65 k_6154 24.3 49 64 k_6155 24.3 60 53 k_6156 24.3 61 52 k_6157 24.3 62 51 k_6158 24.3 66 47 k_6159 24.3 68 45 */ /*要建立6个文件,checi,number,ticket1;checi2,number2,ticket2.后三个文件是用来复制前三个文 件的,保持原来的数据,其实也可以不需要手动建立文件,不过我就不写了,重要的是要把变化的数组 长度m,n,用文件保存,这个是我以前没想到的。*/