主题:模拟一个飞机票订票和退票系统
模拟一个飞机票订票和退票系统([color=FF0000]大家看看还有什么地方可以改进)[/color]
假定某民航机场有m个航次的班机,每个航次都只到达一个目的地。试为该机场售票处设计一个自动订票和退票系统。
要求系统具有下列功能:
(1) 订票。若该航次余票数大于等于乘客订票数,则在该航次的乘客表(按乘客姓氏字母顺序连接)中,插入订票乘客的信息项,并修改航次有关数据,否则,给出相应的提示信息。
(2) 退票。若该航次当前退票数小于等于乘客订票数,则在相应的乘客表中找到该乘客项,修改该航次及乘客表中的有关数据.当由于退票,使乘客的订票数为零时,要从乘客表中撤消该乘客项;否则,给出相应的提示信息.
(3) 将某航次的余票数恢复初值------该航次的最大客票数.
当客户订票数要求不能满足时,系统也可向客户提供到达同一目的地的其他航线情况.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define MaxNode 100
#define charl 100
#define MaxContainf 100
int m;//航班的次数
typedef struct TypeTime
{
int hour;
int minute;
int TimeLong;
}TypeTime;
typedef struct Passenger
{
char name[charl];
int TicketNum;
struct Passenger *next;
}Passenger;
typedef struct TypeFight
{
TypeTime StartTime;
TypeTime EndTime;
int MaxContain;
int ReadyContain;
Passenger *head;
Passenger *last;
}TypeFight;
typedef struct TypePlane
{
char ID[charl];
char StartPlace[charl];
char EndPlace[charl];
int PlaneNum;
TypeFight PlaneFight[MaxNode];
}TypePlane;
TypePlane Plane[MaxNode];
/*int rand01()
{
//随机产生01
srand( (unsigned)time( NULL ) );
return rand() % 2;
}*/
void newline(int n)
{
int i;
for(i = 1;i <= n;i++)
printf("\n");
}
void space(int n)
{
int i;
for(i = 1;i <= n;i++)
printf(" ");
}
void Input()
{
//初始输入输入
FILE *fp;
int i, j;
//freopen("outt.txt", "w", stdout);
fp = fopen("plane.txt", "r");
fscanf(fp, "%d", &m);
printf("m = %d\n", m);
for(i = 0;i < m;i++){
fscanf(fp, "%s%s%s%d", Plane[i].StartPlace,Plane[i].EndPlace,
Plane[i].ID, &Plane[i].PlaneNum);
printf("%s %s\n%s\n%d\n", Plane[i].StartPlace, Plane[i].EndPlace,
Plane[i].ID, Plane[i].PlaneNum);
for(j = 0;j < Plane[i].PlaneNum;j++){
fscanf(fp, "%d:%d", &Plane[i].PlaneFight[j].StartTime.hour,
&Plane[i].PlaneFight[j].StartTime.minute);
fscanf(fp, "%d:%d", &Plane[i].PlaneFight[j].EndTime.hour,
&Plane[i].PlaneFight[j].EndTime.minute);
printf("%d:%d ", Plane[i].PlaneFight[j].StartTime.hour,
Plane[i].PlaneFight[j].StartTime.minute);
printf("%d:%d\n", Plane[i].PlaneFight[j].EndTime.hour,
Plane[i].PlaneFight[j].EndTime.minute);
Plane[i].PlaneFight[j].MaxContain = MaxContainf;
Plane[i].PlaneFight[j].ReadyContain = 0;
Plane[i].PlaneFight[j].StartTime.TimeLong =
Plane[i].PlaneFight[j].StartTime.hour * 60 +
Plane[i].PlaneFight[j].StartTime.minute;
Plane[i].PlaneFight[j].EndTime.TimeLong =
Plane[i].PlaneFight[j].EndTime.hour * 60 +
Plane[i].PlaneFight[j].EndTime.minute;
Plane[i].PlaneFight[j].head = (Passenger *)malloc(sizeof(Passenger));
Plane[i].PlaneFight[j].head->next = NULL;
Plane[i].PlaneFight[j].last = Plane[i].PlaneFight[j].head;
}
}
fclose(fp);
}
int BookTicket()
{
char ID[charl];
char name[charl];
TypeTime BeforeTime;
int TimeLong, Remain;
int i, j, TicketNum;
int flag = 0;
int n;
Passenger *p, *s, *t;
while(1){
system("cls");
printf("请输入你要都买得的票的ID。。。。\n");
scanf("%s", ID);
for(i = 0;i < m;i++){
if(strcmp(Plane[i].ID, ID) == 0)break;
}
if(i >= m) return 0; //查询失败,找不到你要查找的ID
printf("起飞地点:%s\n", Plane[i].StartPlace);
printf("降落地点:%s\n", Plane[i].EndPlace);
printf("\n\n");
printf("清查看起飞地点和降落地点是否正确。。。。\n");
printf("\n");
printf("1.继续\n");
printf("2.重新输入ID\n");
scanf("%d", &n);
if(n == 1)break;
}
printf("请输入你要购买几时后的机票。如4:12。。。。\n");
scanf("%d:%d", &BeforeTime.hour, &BeforeTime.minute);
TimeLong = BeforeTime.hour * 60 + BeforeTime.minute;
printf("请输入你要购买多少张此机票。。。。\n");
scanf("%d", &TicketNum);
for(j = 0;j < Plane[i].PlaneNum;j++){
Remain = Plane[i].PlaneFight[j].MaxContain - Plane[i].PlaneFight[j].ReadyContain;
//printf("%d %d\n", Remain, TimeLong);
if(Plane[i].PlaneFight[j].StartTime.TimeLong >= TimeLong
&& TicketNum <= Remain){
//订票成功
printf("你可以购买此票.........\n");
printf("起飞时间:%d:%d\n", Plane[i].PlaneFight[j].StartTime.hour,
Plane[i].PlaneFight[j].StartTime.minute);
printf("降落时间:%d:%d\n", Plane[i].PlaneFight[j].EndTime.hour,
Plane[i].PlaneFight[j].EndTime.minute);
Plane[i].PlaneFight[j].ReadyContain += TicketNum;
//加入乘客对队列,按姓氏首字母顺序保存
printf("请输入你的姓名:");
scanf("%s", name);
printf("购买成功,谢谢你!请提前赶到机场。。。。。\n");
p = Plane[i].PlaneFight[j].head->next;
s = Plane[i].PlaneFight[j].head;// 父节点
while(p != NULL){
if(strcmp(name, p->name) == 1){
s = p;
p = p->next;
}
else break;
}
t = (Passenger *)malloc(sizeof(Passenger));
strcpy(t->name, name);
t->TicketNum = TicketNum;
t->next = p;
s->next = t;
flag = 1;
break;
}
}
if(flag == 0) printf("对不起你所找的航班时间已过,请明天再俩购买\n");
system("pause");
return 1;
}
void CancelTicket()
{
char ID[charl];
char name[charl];
int TicketNum;
int flag = 0;
int i, j;
int n;
Passenger *p, *s;
while(1){
system("cls");
printf("请输入你要退票的ID......\n");
scanf("%s", ID);
for(i = 0;i < m;i++){
if(strcmp(ID, Plane[i].ID) == 0)break;
}
if(i >= m){
printf("没有你订购的航班\n");
printf("1.重新输入\n");
printf("2.退出退票");
scanf("%d", &n);
if(n == 2) return;
}
else break;
}
printf("请输入你的名字。。。。\n");
scanf("%s", name);
for(j = 0;j < Plane[i].PlaneNum;j++){
p = Plane[i].PlaneFight[j].head->next;
s = Plane[i].PlaneFight[j].head;//父节点
while(p != NULL){
if(strcmp(name, p->name) == 0){
//有这个人
printf("请输入你要退的票的数量。。。。\n");
scanf("%d", &TicketNum);
if(TicketNum > p->TicketNum){
printf("对不起,你退票数大于你购买的机票,不能退票\n");
}
else{
//可以退票
p->TicketNum -= TicketNum;
if(p->TicketNum <= 0){
s->next = s->next->next;
free(p);
}
printf("退票成功\n");
system("pause");
}
flag = 1;
break;
}
p = p->next;
}
if(flag == 1) break;
}
if(flag == 0)printf("查无此人\n");
}
int main()
{
int n, temp, s;
Input();
while(1){
//n = rand01();
system("cls");
newline(10);
space(20);
printf("欢迎使用机票查询系统\n");
space(24);
printf("0.订票\n");
space(24);
printf("1.退票\n");
space(24);
printf("3.退出系统\n");
scanf("%d", &n);
if(n == 3) break;
if(n == 0){
while(1){
temp = BookTicket();
if(temp == 0){
printf("对不起,找不到你所查找的信息!\n");
printf("1.重新查找\n2.退出上一层\n");
scanf("%d", &s);
if(s == 2) break;
}
else break;
}
}
else CancelTicket();
}
return 0;
}
假定某民航机场有m个航次的班机,每个航次都只到达一个目的地。试为该机场售票处设计一个自动订票和退票系统。
要求系统具有下列功能:
(1) 订票。若该航次余票数大于等于乘客订票数,则在该航次的乘客表(按乘客姓氏字母顺序连接)中,插入订票乘客的信息项,并修改航次有关数据,否则,给出相应的提示信息。
(2) 退票。若该航次当前退票数小于等于乘客订票数,则在相应的乘客表中找到该乘客项,修改该航次及乘客表中的有关数据.当由于退票,使乘客的订票数为零时,要从乘客表中撤消该乘客项;否则,给出相应的提示信息.
(3) 将某航次的余票数恢复初值------该航次的最大客票数.
当客户订票数要求不能满足时,系统也可向客户提供到达同一目的地的其他航线情况.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define MaxNode 100
#define charl 100
#define MaxContainf 100
int m;//航班的次数
typedef struct TypeTime
{
int hour;
int minute;
int TimeLong;
}TypeTime;
typedef struct Passenger
{
char name[charl];
int TicketNum;
struct Passenger *next;
}Passenger;
typedef struct TypeFight
{
TypeTime StartTime;
TypeTime EndTime;
int MaxContain;
int ReadyContain;
Passenger *head;
Passenger *last;
}TypeFight;
typedef struct TypePlane
{
char ID[charl];
char StartPlace[charl];
char EndPlace[charl];
int PlaneNum;
TypeFight PlaneFight[MaxNode];
}TypePlane;
TypePlane Plane[MaxNode];
/*int rand01()
{
//随机产生01
srand( (unsigned)time( NULL ) );
return rand() % 2;
}*/
void newline(int n)
{
int i;
for(i = 1;i <= n;i++)
printf("\n");
}
void space(int n)
{
int i;
for(i = 1;i <= n;i++)
printf(" ");
}
void Input()
{
//初始输入输入
FILE *fp;
int i, j;
//freopen("outt.txt", "w", stdout);
fp = fopen("plane.txt", "r");
fscanf(fp, "%d", &m);
printf("m = %d\n", m);
for(i = 0;i < m;i++){
fscanf(fp, "%s%s%s%d", Plane[i].StartPlace,Plane[i].EndPlace,
Plane[i].ID, &Plane[i].PlaneNum);
printf("%s %s\n%s\n%d\n", Plane[i].StartPlace, Plane[i].EndPlace,
Plane[i].ID, Plane[i].PlaneNum);
for(j = 0;j < Plane[i].PlaneNum;j++){
fscanf(fp, "%d:%d", &Plane[i].PlaneFight[j].StartTime.hour,
&Plane[i].PlaneFight[j].StartTime.minute);
fscanf(fp, "%d:%d", &Plane[i].PlaneFight[j].EndTime.hour,
&Plane[i].PlaneFight[j].EndTime.minute);
printf("%d:%d ", Plane[i].PlaneFight[j].StartTime.hour,
Plane[i].PlaneFight[j].StartTime.minute);
printf("%d:%d\n", Plane[i].PlaneFight[j].EndTime.hour,
Plane[i].PlaneFight[j].EndTime.minute);
Plane[i].PlaneFight[j].MaxContain = MaxContainf;
Plane[i].PlaneFight[j].ReadyContain = 0;
Plane[i].PlaneFight[j].StartTime.TimeLong =
Plane[i].PlaneFight[j].StartTime.hour * 60 +
Plane[i].PlaneFight[j].StartTime.minute;
Plane[i].PlaneFight[j].EndTime.TimeLong =
Plane[i].PlaneFight[j].EndTime.hour * 60 +
Plane[i].PlaneFight[j].EndTime.minute;
Plane[i].PlaneFight[j].head = (Passenger *)malloc(sizeof(Passenger));
Plane[i].PlaneFight[j].head->next = NULL;
Plane[i].PlaneFight[j].last = Plane[i].PlaneFight[j].head;
}
}
fclose(fp);
}
int BookTicket()
{
char ID[charl];
char name[charl];
TypeTime BeforeTime;
int TimeLong, Remain;
int i, j, TicketNum;
int flag = 0;
int n;
Passenger *p, *s, *t;
while(1){
system("cls");
printf("请输入你要都买得的票的ID。。。。\n");
scanf("%s", ID);
for(i = 0;i < m;i++){
if(strcmp(Plane[i].ID, ID) == 0)break;
}
if(i >= m) return 0; //查询失败,找不到你要查找的ID
printf("起飞地点:%s\n", Plane[i].StartPlace);
printf("降落地点:%s\n", Plane[i].EndPlace);
printf("\n\n");
printf("清查看起飞地点和降落地点是否正确。。。。\n");
printf("\n");
printf("1.继续\n");
printf("2.重新输入ID\n");
scanf("%d", &n);
if(n == 1)break;
}
printf("请输入你要购买几时后的机票。如4:12。。。。\n");
scanf("%d:%d", &BeforeTime.hour, &BeforeTime.minute);
TimeLong = BeforeTime.hour * 60 + BeforeTime.minute;
printf("请输入你要购买多少张此机票。。。。\n");
scanf("%d", &TicketNum);
for(j = 0;j < Plane[i].PlaneNum;j++){
Remain = Plane[i].PlaneFight[j].MaxContain - Plane[i].PlaneFight[j].ReadyContain;
//printf("%d %d\n", Remain, TimeLong);
if(Plane[i].PlaneFight[j].StartTime.TimeLong >= TimeLong
&& TicketNum <= Remain){
//订票成功
printf("你可以购买此票.........\n");
printf("起飞时间:%d:%d\n", Plane[i].PlaneFight[j].StartTime.hour,
Plane[i].PlaneFight[j].StartTime.minute);
printf("降落时间:%d:%d\n", Plane[i].PlaneFight[j].EndTime.hour,
Plane[i].PlaneFight[j].EndTime.minute);
Plane[i].PlaneFight[j].ReadyContain += TicketNum;
//加入乘客对队列,按姓氏首字母顺序保存
printf("请输入你的姓名:");
scanf("%s", name);
printf("购买成功,谢谢你!请提前赶到机场。。。。。\n");
p = Plane[i].PlaneFight[j].head->next;
s = Plane[i].PlaneFight[j].head;// 父节点
while(p != NULL){
if(strcmp(name, p->name) == 1){
s = p;
p = p->next;
}
else break;
}
t = (Passenger *)malloc(sizeof(Passenger));
strcpy(t->name, name);
t->TicketNum = TicketNum;
t->next = p;
s->next = t;
flag = 1;
break;
}
}
if(flag == 0) printf("对不起你所找的航班时间已过,请明天再俩购买\n");
system("pause");
return 1;
}
void CancelTicket()
{
char ID[charl];
char name[charl];
int TicketNum;
int flag = 0;
int i, j;
int n;
Passenger *p, *s;
while(1){
system("cls");
printf("请输入你要退票的ID......\n");
scanf("%s", ID);
for(i = 0;i < m;i++){
if(strcmp(ID, Plane[i].ID) == 0)break;
}
if(i >= m){
printf("没有你订购的航班\n");
printf("1.重新输入\n");
printf("2.退出退票");
scanf("%d", &n);
if(n == 2) return;
}
else break;
}
printf("请输入你的名字。。。。\n");
scanf("%s", name);
for(j = 0;j < Plane[i].PlaneNum;j++){
p = Plane[i].PlaneFight[j].head->next;
s = Plane[i].PlaneFight[j].head;//父节点
while(p != NULL){
if(strcmp(name, p->name) == 0){
//有这个人
printf("请输入你要退的票的数量。。。。\n");
scanf("%d", &TicketNum);
if(TicketNum > p->TicketNum){
printf("对不起,你退票数大于你购买的机票,不能退票\n");
}
else{
//可以退票
p->TicketNum -= TicketNum;
if(p->TicketNum <= 0){
s->next = s->next->next;
free(p);
}
printf("退票成功\n");
system("pause");
}
flag = 1;
break;
}
p = p->next;
}
if(flag == 1) break;
}
if(flag == 0)printf("查无此人\n");
}
int main()
{
int n, temp, s;
Input();
while(1){
//n = rand01();
system("cls");
newline(10);
space(20);
printf("欢迎使用机票查询系统\n");
space(24);
printf("0.订票\n");
space(24);
printf("1.退票\n");
space(24);
printf("3.退出系统\n");
scanf("%d", &n);
if(n == 3) break;
if(n == 0){
while(1){
temp = BookTicket();
if(temp == 0){
printf("对不起,找不到你所查找的信息!\n");
printf("1.重新查找\n2.退出上一层\n");
scanf("%d", &s);
if(s == 2) break;
}
else break;
}
}
else CancelTicket();
}
return 0;
}