旅游景点售票系统c语言代码
时间: 2023-07-31 12:07:36 浏览: 161
以下是一个简单的旅游景点售票系统的 C 语言代码示例,其中包含了基本的录入、查询、购票、退票等功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 20
#define MAX_TICKET_NUM 100
typedef struct {
char name[MAX_NAME_LENGTH];
int ticketNum;
float price;
} Sight;
int menu();
void addSight(Sight sights[], int *sightNum);
void querySight(Sight sights[], int sightNum);
void buyTicket(Sight sights[], int sightNum);
void refundTicket(Sight sights[], int sightNum);
int main() {
Sight sights[MAX_TICKET_NUM];
int sightNum = 0;
int choice;
do {
choice = menu();
switch (choice) {
case 1:
addSight(sights, &sightNum);
break;
case 2:
querySight(sights, sightNum);
break;
case 3:
buyTicket(sights, sightNum);
break;
case 4:
refundTicket(sights, sightNum);
break;
case 0:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice!\n");
break;
}
} while (choice != 0);
return 0;
}
int menu() {
int choice;
printf("\n1. Add sight\n");
printf("2. Query sight\n");
printf("3. Buy ticket\n");
printf("4. Refund ticket\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
return choice;
}
void addSight(Sight sights[], int *sightNum) {
if (*sightNum >= MAX_TICKET_NUM) {
printf("Too many sights! Cannot add more.\n");
return;
}
printf("Enter sight name (max %d characters): ", MAX_NAME_LENGTH - 1);
scanf("%s", sights[*sightNum].name);
printf("Enter ticket price: ");
scanf("%f", &sights[*sightNum].price);
sights[*sightNum].ticketNum = 0;
printf("Sight added successfully!\n");
(*sightNum)++;
}
void querySight(Sight sights[], int sightNum) {
char name[MAX_NAME_LENGTH];
printf("Enter sight name: ");
scanf("%s", name);
int i;
for (i = 0; i < sightNum; i++) {
if (strcmp(name, sights[i].name) == 0) {
printf("Sight name: %s\n", sights[i].name);
printf("Ticket price: %.2f\n", sights[i].price);
printf("Ticket number: %d\n", sights[i].ticketNum);
return;
}
}
printf("Sight not found!\n");
}
void buyTicket(Sight sights[], int sightNum) {
char name[MAX_NAME_LENGTH];
printf("Enter sight name: ");
scanf("%s", name);
int i;
for (i = 0; i < sightNum; i++) {
if (strcmp(name, sights[i].name) == 0) {
if (sights[i].ticketNum >= MAX_TICKET_NUM) {
printf("All tickets sold out!\n");
return;
}
sights[i].ticketNum++;
printf("Ticket bought successfully!\n");
printf("Ticket number: %d\n", sights[i].ticketNum);
return;
}
}
printf("Sight not found!\n");
}
void refundTicket(Sight sights[], int sightNum) {
char name[MAX_NAME_LENGTH];
printf("Enter sight name: ");
scanf("%s", name);
int i;
for (i = 0; i < sightNum; i++) {
if (strcmp(name, sights[i].name) == 0) {
if (sights[i].ticketNum <= 0) {
printf("No ticket to refund!\n");
return;
}
sights[i].ticketNum--;
printf("Ticket refunded successfully!\n");
printf("Ticket number: %d\n", sights[i].ticketNum);
return;
}
}
printf("Sight not found!\n");
}
```
这个示例中使用了结构体来存储景点的名称、门票数量和价格信息,同时定义了一些函数来实现各种功能。在 `main()` 函数中,通过循环调用 `menu()` 函数来显示菜单,根据用户的选择来调用不同的函数。
在 `addSight()` 函数中,用户可以输入景点的名称和门票价格,同时会自动将门票数量初始化为 0,然后将这个景点的信息存储到数组中。如果数组已经满了,则无法添加新的景点。
在 `querySight()` 函数中,用户可以输入景点的名称来查询这个景点的信息,包括门票价格和已售门票数量。
在 `buyTicket()` 函数中,用户可以输入景点的名称来购买门票。如果这个景点的门票已经全部售出,则无法购买。如果购买成功,则会将这个景点的已售门票数量加 1。
在 `refundTicket()` 函数中,用户可以输入景点的名称来退票。如果这个景点的已售门票数量为 0,则无法退票。如果退票成功,则会将这个景点的已售门票数量减 1。
需要注意的是,这个示例中没有对用户的输入进行检查和错误处理,实际应用中需要根据具体情况添加相应的代码。
阅读全文