刚写好,呵呵,以后要养成写注释的习惯
//编一个火车票售票系统,可以完成售票、退票、车票剩余查询等功能,每张车票车次座位信息。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define null 0
#define elemtype int
typedef struct node//定义数据结构
{
char checi[6];//定义车次
elemtype seat;//定义座号
struct node *next;
}ticket; //定义结构类型
ticket *sale,*back;//定义两个表 sale ,back是备份的表
int count()//查询车票剩余情况
{
ticket *p;
int n=0;
p=sale;
while(p)
{
n++;
p=p->next;
}
return (n);
}
void tuipiao(elemtype x,char t[])
{
ticket *p,*s;
int j=1;
s=(ticket*)malloc(sizeof(ticket));
s->seat=x;
p=back;
strcpy(s->checi,t);
while(strcmp(s->checi,p->checi)&&(s->seat=p->seat)&&p)
{
p=p->next;
}
if(!p)
printf("对不起!您退的票不是本次列车的票!");
else
{
s->next=sale;
sale=s;
}
}
void shoupiao()//售票函数 每售出一张从sale中删除一个结点
{
ticket *p;
if(sale)
{
p=sale;
sale=sale->next;
printf("您购票的车次是:%s,座号是:%d",p->checi,p->seat);
free(p);
}
else
printf("对不起!票以售完!/n");
}
void display()//显示sale中剩余的票数
{
ticket *p;
p=sale;
if(p==null)
{
printf("对不起!票以售完!/n");
}
else
{
//printf("剩余票为:");
while(p!=null)
{
printf("%s,%d ",p->checi,p->seat);
p=p->next;
}
}
printf("/n");
}
int main()//主函数 包含链表的创建
{
ticket *p,*q;
char t1[4];
int d,i,n,select,k,flag=1;
sale=null;
back=null;
printf("请输入列车座位数:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p=(ticket*)malloc(sizeof(ticket));
q=(ticket*)malloc(sizeof(ticket));
printf("please input the checi:");
//scanf("%s%s",&p->checi,&q->checi);// 可以用两种方法输入车次 这种麻烦需要输入两次
scanf("%s",&p->checi);
strcpy(q->checi,p->checi);
printf("plesae input the number of the seat:");//输入座位号
scanf("%d",&d);
p->seat=d;
p->next=sale;
sale=p;
q->seat=d;
q->next=back;
back=q;
//printf("/n");
}
printf("列车的情况为:/n");
display();
printf("/n");
while(flag)
{
printf("1。。。。。查询剩票数。。。。/n");
printf("2。。。。。购票。。。。。。。/n");
printf("3。。。。。退票。。。。。。。/n");
printf("4。。。。。退出。。。。。。。/n");
printf("please input your select:");
scanf("%d",&select);
switch(select)
{
case 1:
{
d=count();
printf("剩余的票数为:%d/n",d);
printf("座位剩余情况为:");
display();
printf("/n");
}
break;
case 2:
{
printf("购买车票: ");
shoupiao();
printf("/n");
display();
printf("/n");
}
break;
case 3:
{
//printf("退票!/n");
printf("请输入你要退票的车次:");
scanf("%s",&t1);
printf("请输入你要退票的座位:");
scanf("%d",&d);
tuipiao(d,t1);
printf("/n");
display();
printf("/n");
}
break;
case 4:flag=0;
break;
}
}
system("pause");
}