航班售票系统,要求完成以下功能: 1 假设该民航有若干航班,要求实现增加航班和取消某个航班的功能。 2 查询:根据用户提出的终到站查询航班号、售票情况等航班信息;也可根据 航班号,列出该航班已订票的乘客名单。 3 订票:根据航班号为客户订票,如该航班有余票,则为客户订票;如该航班 已满员,则显示相应信息。 4 退票:按乘客要求退出已预定的机票。 提示: 数据结构:可以使用链表表示航线表,对每个航班应包括以下信息:航班号、到 达港、总座位数、余票额、乘客名单等;其中乘客名单应为一个单链表,每个乘 客的信息有:乘客姓名、证件号码、座位号等,为方便查找,可考虑按乘客姓名 排序;相应数据类型示范如下: struct cnode{ char name[20]; //乘客姓名 char idcard[30]; //证件号码 int seatno; //座位号 struct cnode *clink; //下一乘客指针 }; struct pnode{ int no; //航班号 char destin[30]; //到达站 int limit,rest; //总座位数、余票额 struct cnode *clist; //乘客名单 struct pnode *plink; //下一航班指针 };请写出代码
时间: 2024-01-11 18:05:19 浏览: 100
由于这是一个完整的系统,我无法在这里直接给出完整的代码,但是我可以提供大致的思路和代码框架。
首先,我们需要定义两个结构体,分别表示乘客和航班信息:
```
struct Passenger {
char name[20];
char idcard[30];
int seatno;
struct Passenger *next;
};
struct Flight {
int no;
char destin[30];
int limit;
int rest;
struct Passenger *passenger_list;
struct Flight *next;
};
```
接下来,我们需要实现增加航班和取消某个航班的功能。这可以通过链表来实现。我们可以定义一个指向航班链表头的指针 flight_list,然后通过遍历链表来实现增加和取消:
```
struct Flight *flight_list = NULL;
// 增加航班
void add_flight(int no, char *destin, int limit) {
struct Flight *new_flight = (struct Flight *)malloc(sizeof(struct Flight));
new_flight->no = no;
strcpy(new_flight->destin, destin);
new_flight->limit = limit;
new_flight->rest = limit;
new_flight->passenger_list = NULL;
new_flight->next = flight_list;
flight_list = new_flight;
}
// 取消航班
void cancel_flight(int no) {
struct Flight *prev = NULL;
struct Flight *cur = flight_list;
while (cur != NULL) {
if (cur->no == no) {
if (prev == NULL) {
flight_list = cur->next;
} else {
prev->next = cur->next;
}
free(cur);
return;
}
prev = cur;
cur = cur->next;
}
}
```
接下来是查询功能,根据提出的终到站查询航班号、售票情况等航班信息。我们可以遍历航班链表,找到到达港和航班号匹配的航班,然后输出相关信息。对于根据航班号列出该航班已订票的乘客名单这一功能,我们可以在找到航班后遍历该航班的乘客链表,然后输出相关信息:
```
// 查询航班
void query_flight(char *destin) {
struct Flight *cur = flight_list;
while (cur != NULL) {
if (strcmp(cur->destin, destin) == 0) {
printf("Flight No.: %d\n", cur->no);
printf("Destination: %s\n", cur->destin);
printf("Total seats: %d\n", cur->limit);
printf("Rest seats: %d\n", cur->rest);
printf("Passenger list:\n");
struct Passenger *p = cur->passenger_list;
while (p != NULL) {
printf("%s %s %d\n", p->name, p->idcard, p->seatno);
p = p->next;
}
return;
}
cur = cur->next;
}
printf("No flights to %s\n", destin);
}
// 列出乘客名单
void list_passengers(int no) {
struct Flight *cur = flight_list;
while (cur != NULL) {
if (cur->no == no) {
printf("Passenger list of flight No.%d:\n", no);
struct Passenger *p = cur->passenger_list;
while (p != NULL) {
printf("%s %s %d\n", p->name, p->idcard, p->seatno);
p = p->next;
}
return;
}
cur = cur->next;
}
printf("No such flight\n");
}
```
订票和退票的功能也可以通过遍历航班链表和乘客链表来实现。对于订票,我们需要找到航班和乘客名单,如果航班还有余票,则将该乘客加入乘客链表;对于退票,我们需要找到乘客,然后将其从乘客链表中删除:
```
// 订票
void book_ticket(int no, char *name, char *idcard) {
struct Flight *cur = flight_list;
while (cur != NULL) {
if (cur->no == no) {
if (cur->rest == 0) {
printf("The flight is full\n");
return;
}
struct Passenger *p = (struct Passenger *)malloc(sizeof(struct Passenger));
strcpy(p->name, name);
strcpy(p->idcard, idcard);
p->seatno = cur->limit - cur->rest + 1;
cur->rest--;
p->next = cur->passenger_list;
cur->passenger_list = p;
printf("Ticket booked successfully\n");
return;
}
cur = cur->next;
}
printf("No such flight\n");
}
// 退票
void cancel_ticket(int no, char *name) {
struct Flight *cur = flight_list;
while (cur != NULL) {
struct Passenger *prev = NULL;
struct Passenger *p = cur->passenger_list;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
if (prev == NULL) {
cur->passenger_list = p->next;
} else {
prev->next = p->next;
}
cur->rest++;
free(p);
printf("Ticket canceled successfully\n");
return;
}
prev = p;
p = p->next;
}
cur = cur->next;
}
printf("No such passenger\n");
}
```
最后,我们需要一个主函数来驱动整个程序,接收用户的输入并调用相应的函数:
```
int main() {
while (1) {
printf("Please select an operation:\n");
printf("1. Add a flight\n");
printf("2. Cancel a flight\n");
printf("3. Query flight information\n");
printf("4. List passengers of a flight\n");
printf("5. Book a ticket\n");
printf("6. Cancel a ticket\n");
printf("7. Exit\n");
int op;
scanf("%d", &op);
if (op == 1) {
int no, limit;
char destin[30];
printf("Please enter the flight No.: ");
scanf("%d", &no);
printf("Please enter the destination: ");
scanf("%s", destin);
printf("Please enter the total seats: ");
scanf("%d", &limit);
add_flight(no, destin, limit);
printf("Flight added successfully\n");
} else if (op == 2) {
int no;
printf("Please enter the flight No.: ");
scanf("%d", &no);
cancel_flight(no);
printf("Flight canceled successfully\n");
} else if (op == 3) {
char destin[30];
printf("Please enter the destination: ");
scanf("%s", destin);
query_flight(destin);
} else if (op == 4) {
int no;
printf("Please enter the flight No.: ");
scanf("%d", &no);
list_passengers(no);
} else if (op == 5) {
int no;
char name[20], idcard[30];
printf("Please enter the flight No.: ");
scanf("%d", &no);
printf("Please enter your name: ");
scanf("%s", name);
printf("Please enter your ID card number: ");
scanf("%s", idcard);
book_ticket(no, name, idcard);
} else if (op == 6) {
int no;
char name[20];
printf("Please enter the flight No.: ");
scanf("%d", &no);
printf("Please enter your name: ");
scanf("%s", name);
cancel_ticket(no, name);
} else if (op == 7) {
break;
} else {
printf("Invalid operation\n");
}
}
return 0;
}
```
以上是一个简单的航班售票系统的代码框架,你可以根据自己的需要进行修改和完善。
阅读全文