偶然翻到了大一C语言的课设,貌似是叫火车售票管理系统吧,记不清了,放在这里吧。
#include
#include
#include
#include
#include
#include
//结构体定义=====================================
typedef struct node
{
char name[12]; //乘客姓名
char IDcard[20]; //身份证号码
int TicketNum; //订票数
double FareSum; //总票价
struct node *next; //指针成员
}Node;
struct DTime //发车时间
{
int year;
int month;
int day;
int hour;
int minute;
};
struct TrainRoute
{
char Terminal[20]; //终点站
char sequence[20]; //车次
struct DTime Time; //发车时间
double fare; //票价
int SpareTicketMum; //余票数
struct node *head; //指针成员,指向下一个成员
}train[6]= //火车信息初始化
{
{NULL},
{ "烟台","UJN-001",{2021,1,1,1,1},200,200,NULL},
{ "青岛","UJN-002",{2021,1,1,1,1},300,0,NULL},
{ "北京","UJN-003",{2021,1,1,1,1},500,0,NULL},
{ "巴黎","UJN-004",{2021,1,1,1,1},2000,5,NULL},
{ "火星","UJN-005",{2021,2,30,1,1},99999,1,NULL}
};
int StartTickets[6]={train[1].SpareTicketMum,train[1].SpareTicketMum,train[2].SpareTicketMum,train[3].SpareTicketMum,train[4].SpareTicketMum,train[5].SpareTicketMum};
//建立五个链表(分别对应五条线路)================
Node *head1 = (Node *)malloc(sizeof(Node));
Node *head2 = (Node *)malloc(sizeof(Node));
Node *head3 = (Node *)malloc(sizeof(Node));
Node *head4 = (Node *)malloc(sizeof(Node));
Node *head5 = (Node *)malloc(sizeof(Node));
//函数预定义=====================================
void ShowList(); //展示菜单
void TrainRouteInquiry(); //查询功能
void TrainRouteInquiry1(); // 1.1输出线路全部信息
void TrainRouteInquiry2(); // 1.2输出余票数为零的火车线路
void TrainRouteInquiry3(); // 1.3通过终点站查询火车线路
void TrainRouteInquiry4(); // 1.4通过时间查询
void TrainRouteInquiry5(); // 1.5输出该车次的订票数和余票数
void QueryRestTickets(); //2.查询余票数
int InsertNewnode(Node *head,double fare,int number);//插入节点
void TravelLink(); //通过姓名查询
void DeleteLink(); //6.退订车票
void LookLink(); //输出全部订票信息
void ChangeTickets(); //修改订票信息
void deletenode(Node *head,char name[]);//删除某个节点
//主函数 ========================================
int main()
{
int select;
//将头节点的所有指针赋空值
head1->next=NULL;
head2->next=NULL;
head3->next=NULL;
head4->next=NULL;
head5->next=NULL;
//打印菜单
ShowList();
while(true)
{
printf("请选择您的操作:");
scanf("%d",&select);
switch(select)
{
case 1://1.查询功能
TrainRouteInquiry();
continue;
case 2://2.查询余票数
QueryRestTickets();
continue;
case 3://3.查询订票信息
TravelLink();
continue;
case 4://4.输出全部订票信息
LookLink();
continue;
case 5://修改车票信息
ChangeTickets();
continue;
case 6://退订车票
DeleteLink();
continue;
case 7:
ShowList();
continue;
case 0://退出系统
printf("\n感谢您的使用!谢谢再见.");
exit(0);
default:
printf("输入有误!请重新输入正确的选项代码!\n\n");
continue;
}
}
return 0;
}
//菜单打印=======================================
void ShowList()
{
printf("\n------------欢迎使用车票管理系统------------\n");
printf("============================================\n");
printf("||========================================||\n");
printf("||1.火车线路查询 2.查询余票数 ||\n");
printf("||3.查询乘客订购信息 4.查询功能 ||\n");
printf("||5.修改车票信息 6.退订车票 ||\n");
printf("||7.重新显示菜单栏 0.退出系统 ||\n");
printf("||========================================||\n");
printf("============================================\n");
}
//1.查询功能
void TrainRouteInquiry()
{
printf("\n请选择您要查询的信息:\n");
printf("1.输出线路全部信息\n");
printf("2.输出余数为零的火车线路\n");
printf("3.通过终点站查询火车线路\n");
printf("4.通过发车时间查询火车线路\n");
printf("5.输出该车次的订票数和余票数\n");
int n;
while(1)
{
scanf("%d",&n);
if(n<1||n>5)
{
printf("输入有误!请输入正确的选择代码");
continue;
}
else
break;
}
switch(n)
{
case 1:
TrainRouteInquiry1();
break;
case 2:
TrainRouteInquiry2();
break;
case 3:
TrainRouteInquiry3();
break;
case 4:
TrainRouteInquiry4();
break;
case 5:
TrainRouteInquiry5();
}
}
//1.1输出线路全部信息
void TrainRouteInquiry1()
{
int n,select;
while(true)
{
printf("请输入您要查询的火车线路:");
scanf("%d",&n);
if(n<1 || n>5)
{
printf("输入错误!请重新输入!\n");
continue;
}
else
{
printf("目的地:%s 车次:%s 发车时间:%d:%d:%d:%d:%d 票价:%.0lf\n\n",
train[n].Terminal,train[n].sequence,train[n].Time.year,train[n].Time.month,
train[n].Time.day,train[n].Time.hour,train[n].Time.minute,train[n].fare);
printf("请继续选择您的操作(1:继续查询路线 2:返回主页面): ");
scanf("%d",&select);
if(select==1)
continue;
else if(select==2)
{
printf("\n已返回主页面!\n");
break;
}
}
}
}
//1.2输出余票数为零的火车线路
void TrainRouteInquiry2()
{
int temp=0;
for(int n=1;n<=5;n++)
{
if(train[n].SpareTicketMum==0)
{
temp++;
printf("目的地:%s 车次:%s 票价:%.2lf 余票数:%d\n",
train[n].Terminal,train[n].sequence,train[n].fare,train[n].SpareTicketMum);
}
}
if(temp==0)
printf("对不起!所有路线均有余票\n");
printf("将返回主页面:\n\n");
}
//1.3通过终点站查询火车线路
void TrainRouteInquiry3()
{
char c[30];
int count=0;
getchar();
printf("请输入您要查询的目的地:");
gets(c);
for(int n=1;n<=5;n++)
{
if(strcmp(train[n].Terminal,c)==0)
{
count++;
printf("目的地:%s 车次:%s 票价:%.0lf 余票数:%d\n",
train[n].Terminal,train[n].sequence,train[n].fare,train[n].SpareTicketMum);
printf("将返回主页面\n\n");
}
}
if(count==0)
printf("对不起!未查询到您要查询的信息\n将返回主页面");
}
//1.4通过时间查询
void TrainRouteInquiry4()
{
int count=0,a,b,c,d,e;
printf("请输入您要查询发车时间(包括年 月 日 小时 分钟,中间用空格分隔):");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
for(int n=1;n<=5;n++)
{
if(train[n].Time.year==a&&train[n].Time.month==b&&train[n].Time.day==c&&train[n].Time.hour==d&&train[n].Time.minute==e)
{
count++;
printf("目的地:%s 车次:%s 票价:%.0lf 发车时间:%d:%d:%d:%d:%d\n",
train[n].Terminal,train[n].sequence,train[n].fare,train[n].Time.year,train[n].Time.month,
train[n].Time.day,train[n].Time.hour,train[n].Time.minute);
}
}
if(count==0)
printf("对不起!未查询到您要查询的信息");
printf("将返回主页面\n\n");
}
//1.5输出该车次的订票数和余票数
void TrainRouteInquiry5()
{
int n,select;
while(true)
{
printf("请输入您要查询的火车线路:");
scanf("%d",&n);
if(n<1 || n>5)
{
printf("输入错误!请重新输入!\n");
continue;
}
else
{
printf("目的地:%s 车次:%s 订票数:%d 余票数:%d\n\n",
train[n].Terminal,train[n].sequence,StartTickets[n]-train[n].SpareTicketMum,train[n].SpareTicketMum);
printf("请继续选择您的操作(1:继续查询路线 2:返回主页面): ");
scanf("%d",&select);
if(select==1)
continue;
else if(select==2)
{
printf("\n已返回主页面!\n");
break;
}
}
}
}
//2.查询余票数 ===================================
void QueryRestTickets()
{
int n,select,m;
while(1)
{
printf("请输入您要查询的路线:");
scanf("%d",&n);
if(n<1 || n>5)
{
printf("输入错误!请重新输入!\n");
continue;
}
else
{
printf("目的地:%s 余票数:%d\n\n",train[n].Terminal,train[n].SpareTicketMum);
if(train[n].SpareTicketMum>0)
{
printf("请继续选择您的操作(1:订购此票 2:继续查询路线 3:返回主页面): ");
scanf("%d",&select);
if(select==1)
{
switch(n)
{
case 1:
m=InsertNewnode(head1,train[1].fare,train[1].SpareTicketMum);
train[1].SpareTicketMum-=m;
break;
case 2:
m=InsertNewnode(head2,train[2].fare,train[2].SpareTicketMum);
train[2].SpareTicketMum-=m;
break;
case 3:
m=InsertNewnode(head3,train[3].fare,train[3].SpareTicketMum);
train[3].SpareTicketMum-=m;
break;
case 4:
m=InsertNewnode(head4,train[4].fare,train[4].SpareTicketMum);
train[4].SpareTicketMum-=m;
break;
case 5:
m=InsertNewnode(head5,train[5].fare,train[5].SpareTicketMum);
train[5].SpareTicketMum-=m;
break;
}
break;
}
else if(select==2)
continue;
else if(select==3)
break;
}
else
{
printf("此票已售罄! 请继续选择您的操作(1:继续查询路线 2:返回主页面):");
scanf("%d",&select);
if(select==1)
continue;
else if(select==2)
{
printf("\n已返回主页面!\n");
break;
}
}
}
}
}
//****插入(购票)节点 ==============================
int InsertNewnode(Node *head,double fare,int number)
{
Node *newnode=(Node*)malloc(sizeof(Node));
newnode->next=NULL;
printf("请输入您的姓名:");
scanf("%s",&newnode->name);
printf("请输入您的身份证号码:");
scanf("%s",&newnode->IDcard);
printf("请输入您的订票数:");
scanf("%d",&newnode->TicketNum);
if(newnode->TicketNum<=number)
{
newnode->FareSum=fare*newnode->TicketNum;
newnode->next=head->next;
head->next=newnode;
printf("订票成功!将返回主页面\n\n");
return newnode->TicketNum;
}
else
{
printf("余票数不足!订票失败!返回主页面\n\n");
return 0;
}
}
//3.查询订票信息===================================
void TravelLink()
{
Node *p = (Node *)malloc(sizeof(Node));
char name[100];
int n,count=0;
printf("请输入您的车号:");
scanf("%d",&n);
printf("请输入您的姓名:");
scanf("%s",&name);
switch(n)
{
case 1:
p = head1 -> next;
break;
case 2:
p = head2 -> next;
break;
case 3:
p = head3 -> next;
break;
case 4:
p = head4 -> next;
break;
case 5:
p = head5 -> next;
break;
}
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
printf("\n您的订票信息为:\n姓名:%s 身份证号:%s 订票数:%d 总票价:%.2lf\n\n",p->name,p->IDcard,p->TicketNum,p->FareSum);
count++;
break;
}
p=p->next;
}
if(count==0)
printf("\n对不起!未查询到您的订票信息\n\n已返回主页面");
}
//4.1:输出全部订票信息============================
void LookLink()
{
Node *p= (Node *)malloc(sizeof(Node));
int select;
printf("请输入您要查询的路线:");
scanf("%d",&select);
switch(select)
{
case 1:
p = head1 -> next;
break;
case 2:
p = head2 -> next;
break;
case 3:
p = head3 -> next;
break;
case 4:
p = head4 -> next;
break;
case 5:
p = head5 -> next;
break;
}
while(p != NULL)
{
printf("姓名:%s 身份证号:%s 订票数:%d\n",p->name,p->IDcard,p->TicketNum);
p=p->next;
}
printf("\n");
}
//5.修改车票信息
void ChangeTickets()
{
Node *p = (Node *)malloc(sizeof(Node));
char name[100];
int n,count=0;
printf("请输入您的车号:");
scanf("%d",&n);
printf("请输入您的姓名:");
scanf("%s",&name);
switch(n)
{
case 1:
p = head1 -> next;
break;
case 2:
p = head2 -> next;
break;
case 3:
p = head3 -> next;
break;
case 4:
p = head4 -> next;
break;
case 5:
p = head5 -> next;
break;
}
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
int temp;
printf("请输入您要修改的选项(1.姓名 2.身份证号码):");
scanf("%d",&temp);
switch(temp)
{
case 1://修改姓名
char newname[100];
printf("请输入您修改后的姓名:");
scanf("%s",&newname);
strcpy(p->name,newname);
break;
case 2://修改身份证号码
char newIDcard[100];
printf("请输入您修改后的身份证号:");
scanf("%s",&newIDcard);
strcpy(p->IDcard,newIDcard);
break;
}
count++;
printf("\n修改成功!将返回主页面\n\n");
break;
}
p=p->next;
}
if(count==0)
printf("对不起!未查询到您的订票信息,请继续选择您的操作(1:重新修改信息 2:返回主页面)\n");
}
//6.退订车票========================================
void DeleteLink()
{
int n,count=0,temp;
char name[100];
Node *p = (Node *)malloc(sizeof(Node));
Node *current = (Node *)malloc(sizeof(Node));
current->next=NULL;
printf("请输入您的车号:");
scanf("%d",&n);
temp=n;
printf("请输入您的姓名:");
scanf("%s",&name);
switch(n)
{
case 1:
p = head1 -> next;
current=head1;
break;
case 2:
p = head2 -> next;
current=head2;
break;
case 3:
p = head3 -> next;
current=head3;
break;
case 4:
p = head4 -> next;
current=head4;
break;
case 5:
p = head5 -> next;
current=head5;
break;
}
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
int select;
printf("\n您的订票信息为:\n姓名:%s 身份证号:%s 订票数:%d\n",p->name,p->IDcard,p->TicketNum);
printf("请选择您的操作(1.退订全部车票 2.退订部分车票):");
while(true)
{
scanf("%d",&select);
if(select==1)
{
train[n].SpareTicketMum+=p->TicketNum;
deletenode(current,name);
}
else if(select==2)
{
int number;
printf("请输入您要退订的订票数量:");
while(true)
{
scanf("%d",&number);
if(number>p->TicketNum)
{
printf("\n您输入的票数大于您已订票的数量!退订失败!请重新输入!");
continue;
}
else if(number==p->TicketNum)
{
train[n].SpareTicketMum+=number;
deletenode(current,name);
break;
}
else if(number<=0)
{
printf("\n您输入的票数小于您已订票的数量!退订失败!请重新输入!");
continue;
}
else
{
p->TicketNum-=number;
train[n].SpareTicketMum+=number;
p->FareSum-=number*train[n].fare;
printf("退订成功!将返回主页面\n\n");
break;
}
}
}
else
{
printf("选择有误!请输入正确的选择代码\n");
continue;
}
count++;
break;
}
p=p->next;
}
if(count==0)
{
printf("\n对不起!未查询到您的订票信息 已返回主页面\n\n");
}
}
}
//删除节点***//
void deletenode(Node *head,char name[])
{
Node *p1=NULL,*p2=NULL;
p1=head;
while((strcmp(p1->name,name)!=0)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->name,name)==0)
{
p2->next=p1->next;
free(p1);
printf("结点已删除!\n");
}
}