【C语言】火车订票系统



技术分享


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<process.h>
#include<conio.h>
#include<iomanip>
#define max 60

typedef struct customer
{//乘客信息
	char name[10];//姓名
	int	 amount;//订票数
	char rank;//火车票种类:硬座、软座、硬卧、软卧
	int  idcard;//身份证号码
	struct customer *next;//指向下一乘客结点
}customer;
typedef struct railway
{//列车信息
	char des_name[10];//乘车区间
	char railway_no[6];//列车次
	char time[10];//时间
	int customer_a;//乘员定额
	int free_a;//剩余票数
	int price[4];//各种车票的价格
	customer *custname;//以订票乘客名单
	customer *replname;//候补乘客名单
	struct railway *next;//指向下列车结点
}railway,*prailway;
int customer_c=0;//所有列车的订票乘客总数
railway *head;//列车头结点
railway *p2;//列车结点指针
customer *custp1[max];//各列车乘客结点指针
customer *replp1[max];//各列车候补结点指针
int Isempty=1;//是否有订票乘客
int Isreplace=1;//是否有候补乘客
customer *prior;//满足要求的订票乘客的前节点,以作删除操作
int shouldsave=0;
char Continue()//询问是否继续函数
{char answer;
	while(1)
	{printf("\n\t 您是否想继续(Y/N)?"); scanf("%s",&answer);
		system("cls");
		if(answer==‘y‘||answer==‘Y‘) return ‘y‘;
		else if(answer==‘n‘||answer==‘N‘) return ‘n‘;
		else printf("\n\t输入有误,请重新输入!");}
}
void errormess()//操作出错函数
{printf("\n\t对不起,您的操作有误!"); getch();}
int find_railway(prailway l,char *key)//核对列车号是否唯一
{int flag=0;railway *p1;p1=l;
	if(p1==p2) return flag;
	while(p1!=p2&&p1!=NULL)
	{if(strcmp(p1->railway_no,key)==0)
		{flag=1; break;}
		p1=p1->next;}
	return flag;
}
int find_railway(prailway l,char *key,prailway &p2,int &railway_no)//列车号查找函数
{int flag=0;railway *p1;p1=l;
	while(p1!=NULL)
	{if(strcmp(p1->railway_no,key)==0)
		{flag=1; p2=p1; break;}
		p1=p1->next;
		if(p1!=NULL) railway_no++;}
	return flag;
}
void des_name_search()//乘车区间查找
{railway *p1=head;char desname[50];
	if(head==NULL)
	{printf("\n\t没有您要查找的乘车区间!"); getch();
		return;}
	printf("\n\t请输入乘车区间:"); scanf("%s",&desname);
	printf("\n\t您所查询的乘车区间信息:");
	printf("\n__________________________________________________________________________________\n");
	while(p1!=NULL)
	{if(strcmp(p1->des_name,desname)==0)
		{printf("车 次   乘车区间  时 间 总票额   剩余票数 硬座票价 软座票价 硬卧票价 软卧票价\n");
printf("\n%-9s%-9s%-9s%-9d%-9d%-9d%-9d%-9d%-9d",p1->railway_no,p1->des_name,p1->time,p1->customer_a,p1->free_a,p1->price[0],p1->price[1],p1->price[2],p1->price[3]);}
		p1=p1->next;
	}printf("\n__________________________________________________________________________________\n");
	Continue();
}
void railway_add()//列车增加函数
{railway *p1;
	while(1)
	{if(head==NULL)
		{p1=p2=new railway;head=p2;
		}
		else
		{p1=new railway;p2->next=p1;p2=p1;
		}
		printf("\n\t添加新的列车号!\n");
		printf("\n\t请输入乘车区间:");
		scanf("%s",&p2->des_name);
		while(1)
		{printf("\n\t请输入--列车号:"); scanf("%s",&p2->railway_no);
		if(find_railway(head,p2->railway_no)) printf("\n\t列车号已存在!\n");
			else break;
		}
		printf("\n\t请输入----时间:");scanf("%s",&p2->time);
		printf("\n\t请输入座位数量:"); scanf("%d",&p2->customer_a);
		printf("\n\t请输入硬座价格:"); scanf("%d",&p2->price[0]);
		printf("\n\t请输入软座价格:"); scanf("%d",&p2->price[1]);
		printf("\n\t请输入硬卧价格:"); scanf("%d",&p2->price[2]);
		printf("\n\t请输入软卧价格:"); scanf("%d",&p2->price[3]);
		p2->free_a=p2->customer_a;p2->custname=NULL;
		p2->replname=NULL;shouldsave=1;
		if(Continue() ==‘n‘)
		{p2->next=NULL; return;}
	}
}
int empty_railway()//判断列车是否存在
{if(head==NULL)
	{system("cls");
		printf("\n\t对不起,此列车不存在,按任意键返回!"); getch();
		return 1;
	}
	else return 0;
}
void railway_see()//查看列车信息函数
{
	system("cls");
	railway *p1;
	p1=head;
	if(empty_railway() ) return;
	printf("\n\n\t\t\t\t  列车信息:\n");
	printf("\n__________________________________________________________________________________\n");
	printf("车 次   乘车区间  时 间  总票额  剩余票数 硬座票价 软座票价 硬卧票价 软卧票价\n ");
	while(p1!=NULL)
	{
		printf("\n%-9s%-9s%-9s%-9d%-9d%-9d%-9d%-9d%-9d",p1->railway_no,p1->des_name,p1->time,p1->customer_a,p1->free_a,p1->price[0],p1->price[1],
				p1->price[2],p1->price[3]);
		p1=p1->next;
	}
	printf("\n__________________________________________________________________________________\n");
	printf("\n\t按任意键返回!\n"); getch();
}
void railwaymanage()//列车管理
{char c;system("cls");
	while(1)
	{printf("\n\t\t\t\t  列车管理菜单");
	printf("\n__________________________________________________________________________________\n\n");
		printf("\t\t\t\t1.添加新的列车号\n");
		printf("\t\t\t\t2.查 看 列 车 号\n");
		printf("\t\t\t\t3.查 看 乘车区间\n");
		printf("\t\t\t\t4.返 回 主 菜 单\n");
	printf("\n__________________________________________________________________________________\n");
		printf("\t请选择您要的服务:");scanf("%s",&c);
		switch(c)
		{case ‘1‘: railway_add(); break;
		case ‘2‘: railway_see(); break;
		case ‘3‘: des_name_search(); break;
		case ‘4‘: return;}
	}
}
void booking()//订票乘客信息
{int ticket_c,idcard,i,flag=0;int railway_no=0;railway *p1;customer *p2;
	customer *p3;char answer[7];char temp,c;int tag=0;int Isrepl=0;
	if(empty_railway() ) return;
	while(1)
	{printf("\n\t现在您可以订票了!");
		flag=0;railway_no=0;  tag=0;
		printf("\n\t请输入列车号:");scanf("%s",&answer);
		if(find_railway(head,answer,p1,railway_no) )
		{while(1)
			{printf("\n\t请输入您想要订购的票的数量:");
				scanf("%d",&ticket_c);
				if(ticket_c==0)
				{printf("\n\t请在此输入列车号:"); getch();}
				else break;
			}if(p1->free_a>=ticket_c)
			{customer_c++;flag=1;Isrepl=1;
				if(p1->custname==NULL)
				{custp1[railway_no]=p2=new customer;
					p1->custname=custp1[railway_no];}
				else
				{p2=new customer;custp1[railway_no]->next=p2;
					custp1[railway_no]=p2;}
				Isempty=0;custp1[railway_no]->amount=ticket_c;
				idcard=p1->customer_a-p1->free_a+1;
				custp1[railway_no]->idcard=idcard;
				p1->free_a-=ticket_c;printf("\n\t请输入您的姓名:");
				scanf("%s",&custp1[railway_no]->name);
				while(1)
				{printf("\n\t请输入票种:"); scanf("%s",&custp1[railway_no]->rank);
		if(!(custp1[railway_no]->rank>=‘1‘&&custp1[railway_no]->rank<=‘3‘))
					{printf("\n\t输入有误,请重新输入"); getch();}
					else break;
				}
				printf("\n\t输入您的id信息:"); scanf("%d",&custp1[railway_no]->idcard);
				if(ticket_c<10) printf("\n\t");else printf("\n\t");
				printf("\n\t恭喜您订票成功!\n");
				for(i=0;i<ticket_c;i++)
				{printf("\n\t您所预定的座位号是%d",idcard++);
					if(i%10==0) printf("\n\t");}
				printf("\n");
			}
			else if(p1->free_a==0)
			{printf("\n\t对不起,票已售完!\n"); Isrepl=0;}
			else
			{printf("\n\t对不起,当前没有多余的票!\n"); Isrepl=0;}
			if(!Isrepl)
			{
		printf("\n\t您是否想成为后部乘客(Y/N)?"); scanf("%s",&temp);
				if(temp==‘y‘||temp==‘Y‘)
				{if(p1->replname==NULL)
					{replp1[railway_no]=p3=new customer;
						p1->replname=replp1[railway_no];}
					else
					{p3=new customer;replp1[railway_no]->next=p3;
						replp1[railway_no]=p3;}
					Isreplace=0;tag=1;
					replp1[railway_no]->amount=ticket_c;
	printf("\n\t请输入您的姓名:"); scanf("%s",&replp1[railway_no]->name);
					replp1[railway_no]->idcard=idcard;
					replp1[railway_no]->amount=ticket_c;
					while(1)
					{
		printf("\n\t请输入车票种类:"); scanf("%s",&replp1[railway_no]->rank);
printf("\n\t请输入您的ID信息:"); scanf("%d",&replp1[railway_no]->idcard);
		if(!(replp1[railway_no]->rank>=‘1‘&&replp1[railway_no]->rank<=‘3‘))
						{printf("\n\t输入有误,请重新输入:"); getch();}
						else break;
					}
					printf("\n\t没有剩余座位!\n");shouldsave=1;
				}
			}
		}
else printf("\n\t对不起,没有此列车!\n");
		if(flag)
			custp1[railway_no]->next=NULL;
		if(tag)
		{replp1[railway_no]->next=NULL;
			printf("\n\t您已经成功排入后部订票队列中!\n");}
		printf("\n\t是否退出菜单?:(Y/N)"); scanf("%s",&c);
		if(c==‘y‘) return;
	}
}
void display_reserve()//显示订票乘客信息
{system("cls");railway *p1;customer *p2;p1=head;
	if(empty_railway() ) return;
	printf("\n\t\t\t订票信息");
	if(Isempty)
	{printf("\n\t对不起,没有订票乘客信息!\n");  getch();
		return;}
	printf("\n__________________________________________________________________________________\n");
	printf("姓  名 车  次   订票数   乘车区间  票类  身份证号码\n");
	while(p1!=NULL)
	{if(p1->custname!=NULL)
		{p2=p1->custname;
			while(p2!=NULL)
			{
printf("\n%-8s%-10s%-9d%-9s%-6c%-9d",p2->name,p1->railway_no,p2->amount,p1->des_name,p2->rank,p2->idcard);
if(p1->free_a>=1) printf("\n\n\t还有多余的票!\n");
else printf("\n\n\t票已售完!\n");p2=p2->next;
			}
		}
		p1=p1->next;
	printf("\n\n__________________________________________________________________________________\n");}
	printf("\n\t按任意键返回!"); getch();return;
}
void display_replace()//候补乘客信息
{system("cls");railway *p1;customer *p2;p1=head;
	if(empty_railway() ) return;printf("\n\t候补乘客信息!");
	if(Isreplace)
	{printf("\n\t对不起,没有候补乘客信息!\n"); getch();
		return;}
	printf("\n__________________________________________________________________________________\n");
	printf("姓  名 车  次   订票数   乘车区间  票类  身份证号码\n");
	while(p1!=NULL)
	{if(p1->replname!=NULL)
		{p2=p1->replname;
			while(p2!=NULL)
			{
printf("\n%-8s%-10s%-9d%-9s%-6c%-9d",p2->name,p1->railway_no,p2->amount,p1->des_name,p2->rank,p2->idcard);
if(p1->free_a>=1) printf("\n\t还有多余的票!\n");
else printf("\n\t票已售完!\n");p2=p2->next;}
		}
		p1=p1->next;}
	printf("\n\n__________________________________________________________________________________\n");
	printf("\n\t按任意键返回!"); getch();return;
}
void customermagmenu()//乘客管理菜单
{char c;system("cls");
	while(1)
	{printf("\n\t\t   乘客管理菜单\n");
printf("\n________________________________________________________\n");
		printf("\t\t1.乘   客   信   息\n");
		printf("\t\t2.候 补 乘 客 信 息\n");
		printf("\t\t3. 返 回 主 菜 单\n");
printf("\n________________________________________________________\n");
		printf("\t请选择您要的服务:"); scanf("%s",&c);
		switch(c)
		{case ‘1‘:display_reserve(); break;
		case ‘2‘:display_replace(); break;
		case ‘3‘:return;
		default:errormess();}
	}
}
void refundticketmenu()//退票函数
{int railway_no=0,flag=0;railway *p1;customer *p2,*p4;customer *p3,*p5;
	char answer[7],name[7];int tag=0;int idcard;
	if(empty_railway() ) return;
	printf("\n\t现在开始退票手续");
	if(Isempty)
	{printf("\n\t对不起,乘客不存在!"); getch();return;}
	while(1)
	{flag=0; tag=0; railway_no=0;
		printf("\n\t请输入车次:"); scanf("%s",&answer);
		if(find_railway(head,answer,p1,railway_no))
		{p2=p1->custname;
		printf("\n\t请输入您的姓名:"); scanf("%s",&name);
			if(p2==NULL)
			{printf("\n\t对不起,乘客不存在!");if(Continue()==‘n‘) return;
			}
			else
				while(p2!=NULL)
				{if(strcmp(p2->name,name)==0)
					{if(p2==p1->custname)
						{prior=p1->custname;idcard=p2->idcard;
							flag=1; break;}
					}
					else if(p2->next!=NULL)
					{if(strcmp(p2->next->name,name)==0)
						{tag=1;prior=p2;
					idcard=p2->next->idcard;flag=1; break;
						}
					}
					p2=p2->next;shouldsave=1;
				}
				if(!flag) printf("\n\t对不起,乘客不存在!\n");
		}
		else printf("\n\t对不起,该车次列车不存在!\n");
		if(flag)
		{if(prior==p1->custname&&!tag)
			{if(prior->next==NULL)
				{p1->free_a+=prior->amount;p1->custname=NULL;}
				else
				{p1->free_a+=prior->next->amount;p1->custname=prior->next;
				}
			}
			else{
			p1->free_a+=prior->next->amount;prior->next=prior->next->next;
				}
			customer_c--;if(customer_c==0) Isempty=1;shouldsave=1;}
		if(flag)
		{p3=p1->replname;
			while(p3!=NULL)
			{if(p3->amount<=p1->free_a)
				{printf("\n\t后部乘客已经存在!\n");
					p4=custp1[railway_no]->next=new customer;
					p4->next=NULL;Isempty=0;
					if(p1->custname==NULL) p1->custname=p4;
					strcpy(p4->name,p3->name);p4->rank=p3->rank;
					p4->amount=p3->amount;p4->idcard=idcard;
					p1->free_a=p3->amount;customer_c++;
					if(p3->next==NULL) Isreplace=1;
					if(p1->replname==p3)
					{if(p1->replname->next==NULL) p1->replname=NULL;
						else p1->replname=p3->next;}
					else p5->next=p3->next->next;break;
				}
				if(p3->next!=NULL)
					if(p3->next->amount<=p1->free_a)
					p5=p3;p3=p3->next;shouldsave=1;}
			printf("\n\t退票成功!\n"); getch();return;
		}
		shouldsave=1;if(Continue()==‘n‘) return;
	}
}
int main()//主函数
{system("cls");system("mode con: cols=90 lines=35");//窗口设计
	railway *p1;p1=head;char c;
	do{system("cls");
		printf("\n\t\t\t\t     火车票订票系统\n");
		printf("\n           ****************************************************************\n");
		printf("\t\t\t            (1)列车----管理\n");
		printf("\t\t\t            (2)订票----菜单\n");
		printf("\t\t\t            (3)退票办理菜单\n");
		printf("\t\t\t            (4)乘客管理菜单\n");
		printf("\t\t\t            (5)退出----系统\n");
		printf("\n           ****************************************************************\n");
		printf("请选择您要的服务:");
		scanf("%s",&c);
		switch(c)
		{case ‘1‘: railwaymanage(); break;
		case ‘2‘: booking(); break;
		case ‘3‘: refundticketmenu(); break;
		case ‘4‘: customermagmenu(); break;
		case ‘5‘: exit(0);
		default: break;}
	}while(c!=5);
	return 0;
}


windows编译运行

C:\Users\chunli>g++ main.c
C:\Users\chunli>a

                                     火车票订票系统

           ****************************************************************
                                    (1)列车----管理
                                    (2)订票----菜单
                                    (3)退票办理菜单
                                    (4)乘客管理菜单
                                    (5)退出----系统

           ****************************************************************



本文出自 “魂斗罗” 博客,请务必保留此出处http://990487026.blog.51cto.com/10133282/1792645

文章来自:http://990487026.blog.51cto.com/10133282/1792645
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3