旅游景点售票系统C语言代码
时间: 2023-08-18 20:10:07 浏览: 101
基于Vue+SpringBoot+MySQL实现的旅游票务预订系统源代码+数据库,含景点档案、景点评分、景点收藏、酒店及美食模块
很抱歉,我之前的回答有误。虽然C语言不是常用的web开发语言,但是我们可以使用C语言开发命令行界面的旅游景点售票系统。下面是一个简单的C语言代码示例,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TICKET_NUM 100 // 最大门票数量
#define MAX_ORDER_NUM 50 // 最大订单数量
// 门票结构体
typedef struct {
char name[20]; // 名称
float price; // 价格
int amount; // 数量
} Ticket;
// 订单结构体
typedef struct {
char name[20]; // 姓名
char ticket_name[20]; // 门票名称
int ticket_amount; // 门票数量
float price; // 总价
} Order;
// 全局变量
Ticket tickets[MAX_TICKET_NUM]; // 门票列表
int ticket_num = 0; // 门票数量
Order orders[MAX_ORDER_NUM]; // 订单列表
int order_num = 0; // 订单数量
// 显示菜单
void show_menu() {
printf("旅游景点售票系统\n");
printf("1. 查看门票\n");
printf("2. 购买门票\n");
printf("3. 退票\n");
printf("4. 查看订单\n");
printf("5. 退出\n");
}
// 查看门票
void show_tickets() {
printf("门票列表:\n");
printf("名称\t\t价格\t数量\n");
for (int i = 0; i < ticket_num; i++) {
printf("%s\t%.2f\t%d\n", tickets[i].name, tickets[i].price, tickets[i].amount);
}
}
// 购买门票
void buy_ticket() {
char name[20];
printf("请输入您的姓名:");
scanf("%s", name);
printf("门票列表:\n");
printf("序号\t名称\t\t价格\t数量\n");
for (int i = 0; i < ticket_num; i++) {
printf("%d\t%s\t%.2f\t%d\n", i + 1, tickets[i].name, tickets[i].price, tickets[i].amount);
}
int index = 0;
printf("请选择门票序号:");
scanf("%d", &index);
if (index < 1 || index > ticket_num) {
printf("门票序号错误!\n");
return;
}
index--;
int amount = 0;
printf("请输入购买数量:");
scanf("%d", &amount);
if (amount <= 0 || amount > tickets[index].amount) {
printf("购买数量错误!\n");
return;
}
float price = tickets[index].price * amount;
if (order_num >= MAX_ORDER_NUM) {
printf("订单已满,购买失败!\n");
return;
}
orders[order_num].price = price;
orders[order_num].ticket_amount = amount;
strcpy(orders[order_num].name, name);
strcpy(orders[order_num].ticket_name, tickets[index].name);
tickets[index].amount -= amount;
order_num++;
printf("购买成功!\n");
}
// 退票
void return_ticket() {
char name[20];
printf("请输入您的姓名:");
scanf("%s", name);
int index = -1;
for (int i = 0; i < order_num; i++) {
if (strcmp(orders[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到订单!\n");
return;
}
for (int i = 0; i < ticket_num; i++) {
if (strcmp(tickets[i].name, orders[index].ticket_name) == 0) {
tickets[i].amount += orders[index].ticket_amount;
break;
}
}
for (int i = index; i < order_num - 1; i++) {
orders[i] = orders[i + 1];
}
order_num--;
printf("退票成功!\n");
}
// 查看订单
void show_orders() {
printf("订单列表:\n");
printf("姓名\t门票名称\t数量\t总价\n");
for (int i = 0; i < order_num; i++) {
printf("%s\t%s\t%d\t%.2f\n", orders[i].name, orders[i].ticket_name, orders[i].ticket_amount, orders[i].price);
}
}
int main() {
// 初始化门票
strcpy(tickets[0].name, "门票A");
tickets[0].price = 100.0;
tickets[0].amount = 50;
strcpy(tickets[1].name, "门票B");
tickets[1].price = 50.0;
tickets[1].amount = 100;
ticket_num = 2;
int choice = 0;
while (1) {
show_menu();
printf("请选择菜单:");
scanf("%d", &choice);
switch (choice) {
case 1:
show_tickets();
break;
case 2:
buy_ticket();
break;
case 3:
return_ticket();
break;
case 4:
show_orders();
break;
case 5:
exit(0);
default:
printf("菜单选择错误!\n");
break;
}
}
return 0;
}
```
以上代码实现了基本的门票购买、退票和订单查询功能。但是需要注意的是,此代码并不完整,仅供参考,还需要根据实际需求进行修改和完善。
阅读全文