旅游景点售票系统c语言
时间: 2023-12-23 22:04:08 浏览: 20
下面是一个简单的旅游景点售票系统的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100
struct Ticket {
char name[MAX_LEN];
int price;
int count;
};
struct Ticket tickets[] = {
{"Adult Ticket", 100, 100},
{"Child Ticket", 50, 50},
{"Student Ticket", 80, 80},
};
int ticket_count = sizeof(tickets) / sizeof(tickets[0]);
void print_ticket_info() {
printf("Ticket Info:\n");
for (int i = 0; i < ticket_count; i++) {
printf("%d. %s, Price: %d, Count: %d\n", i + 1, tickets[i].name, tickets[i].price, tickets[i].count);
}
}
void buy_ticket() {
printf("Please select the ticket you want to buy:\n");
int ticket_index;
scanf("%d", &ticket_index);
if (ticket_index < 1 || ticket_index > ticket_count) {
printf("Invalid ticket index!\n");
return;
}
struct Ticket* ticket = &tickets[ticket_index - 1];
printf("Please enter the number of tickets you want to buy:\n");
int buy_count;
scanf("%d", &buy_count);
if (buy_count > ticket->count) {
printf("Not enough tickets!\n");
return;
}
int total_price = ticket->price * buy_count;
printf("Total price: %d\n", total_price);
ticket->count -= buy_count;
printf("Ticket purchased successfully!\n");
}
int main() {
printf("Welcome to the Ticket System!\n");
while (1) {
printf("Please select an operation:\n");
printf("1. Print Ticket Info\n");
printf("2. Buy Ticket\n");
printf("3. Exit\n");
int op;
scanf("%d", &op);
switch (op) {
case 1:
print_ticket_info();
break;
case 2:
buy_ticket();
break;
case 3:
printf("Goodbye!\n");
exit(0);
default:
printf("Invalid operation!\n");
break;
}
}
return 0;
}
```
这份代码实现了以下功能:
1. 打印门票信息
2. 购买门票
3. 退出系统
在打印门票信息时,会输出每种门票的名称、价格和数量。在购买门票时,用户可以选择要购买的门票种类和数量,系统会计算出总价并提示用户是否购买成功。
这只是一个简单的示例代码,实际的旅游景点售票系统需要考虑更多的细节和功能,例如门票种类的添加和删除、门票数量的自动更新等等。
阅读全文