2、火车票销售 (1)问题描述 试编制一个简单的火车票销售系统,可完成售票、退票、车票剩余情况查询等功能。每张车票包含车次、座位信息。 (2)基本要求 在售票、退票、查询剩余票等环节中,都必须显示出车票的信息,即车次、座位情况。为简单起见,在此假设所有出售的车票均为同一车次的车票。退票时,必须是车站售出的列车票才能退,否则视为无效票,不能办理退票业务。C语言代码
时间: 2024-03-23 07:41:12 浏览: 20
好的,我可以为你提供一个简单的火车票销售系统的C语言实现。以下是示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SEATS 100 // 假设每辆火车最多有100个座位
typedef struct {
char train_no[10]; // 车次
int seat[MAX_SEATS]; // 座位数组,数组元素为0表示该座位未被售出,为1表示已售出
} Train;
// 初始化一个火车
void init_train(Train *train, const char *train_no) {
strcpy(train->train_no, train_no);
int i;
for (i = 0; i < MAX_SEATS; i++) {
train->seat[i] = 0; // 初始化所有座位为未售出状态
}
}
// 显示车票信息
void show_ticket(const Train *train) {
printf("车次:%s\n", train->train_no);
printf("座位情况:\n");
int i;
for (i = 0; i < MAX_SEATS; i++) {
if (train->seat[i] == 0) {
printf("%d号座位:未售出\n", i + 1);
} else {
printf("%d号座位:已售出\n", i + 1);
}
}
}
// 售票
void sell_ticket(Train *train, int seat_no) {
if (train->seat[seat_no - 1] == 1) {
printf("该座位已被售出,请选择其他座位。\n");
} else {
train->seat[seat_no - 1] = 1;
printf("恭喜您,购票成功!\n");
}
}
// 退票
void refund_ticket(Train *train, int seat_no) {
if (train->seat[seat_no - 1] == 0) {
printf("该座位未被售出,无法退票。\n");
} else {
train->seat[seat_no - 1] = 0;
printf("退票成功!\n");
}
}
int main() {
Train train;
init_train(&train, "K1234"); // 初始化车次为K1234的火车
while (1) {
printf("\n请选择您要进行的操作:\n");
printf("1、查看车票信息\n");
printf("2、购买车票\n");
printf("3、退票\n");
printf("4、退出系统\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
show_ticket(&train);
break;
case 2:
printf("请输入您要购买的座位号:\n");
int seat_no;
scanf("%d", &seat_no);
sell_ticket(&train, seat_no);
break;
case 3:
printf("请输入您要退的座位号:\n");
int refund_seat_no;
scanf("%d", &refund_seat_no);
refund_ticket(&train, refund_seat_no);
break;
case 4:
printf("感谢您使用本系统,再见!\n");
exit(0);
default:
printf("无效的选项,请重新输入!\n");
}
}
return 0;
}
```
在这个实现中,我们定义了一个 `Train` 结构体来表示火车,包含车次信息和座位数组。`init_train` 函数用来初始化一个火车,将其车次信息和所有座位的售出状态都设为未售出。`show_ticket` 函数用来显示车票信息,包括车次和所有座位的售出状态。`sell_ticket` 函数用来售票,如果所选座位已被售出,则提示购票失败,否则将该座位的售出状态设为已售出。`refund_ticket` 函数用来退票,如果所选座位未被售出,则提示退票失败,否则将该座位的售出状态设为未售出。在 `main` 函数中,我们不断循环,根据用户的选择执行相应的操作,直到用户选择退出系统。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)