C语言程序设计运动场馆管理系统

设计一个运动场馆管理系统的C语言程序需要涉及多个方面,包括场馆信息的管理、预约功能、用户管理等。下面是一个简单的运动场馆管理系统的实现示例。为了简化示例,这个程序将包括以下功能:

  1. 添加场馆:添加新场馆信息。
  2. 显示场馆:显示所有场馆的信息。
  3. 预约场馆:用户可以预约场馆。
  4. 取消预约:用户可以取消预约。

主要数据结构

  1. 场馆信息:场馆名称、地点、最大容纳人数等。
  2. 预约信息:场馆名称、预约时间、用户信息等。

程序设计

1. 数据结构

首先,我们定义结构体来存储场馆信息和预约信息。

c
#include <stdio.h> #include <string.h> #define MAX_VENUES 100 #define MAX_BOOKINGS 100 typedef struct { char name[50]; char location[100]; int capacity; } Venue; typedef struct { char venueName[50]; char userName[50]; char bookingTime[20]; // Format: YYYY-MM-DD HH:MM } Booking; Venue venues[MAX_VENUES]; Booking bookings[MAX_BOOKINGS]; int venueCount = 0; int bookingCount = 0;

2. 添加场馆

c
void addVenue() { if (venueCount >= MAX_VENUES) { printf("Venue limit reached!\n"); return; } Venue newVenue; printf("Enter venue name: "); scanf("%s", newVenue.name); printf("Enter location: "); scanf(" %[^\n]s", newVenue.location); // To include spaces in location printf("Enter capacity: "); scanf("%d", &newVenue.capacity); venues[venueCount++] = newVenue; printf("Venue added successfully!\n"); }

3. 显示场馆

c
void displayVenues() { if (venueCount == 0) { printf("No venues available.\n"); return; } for (int i = 0; i < venueCount; i++) { printf("Venue Name: %s\n", venues[i].name); printf("Location: %s\n", venues[i].location); printf("Capacity: %d\n\n", venues[i].capacity); } }

4. 预约场馆

c
void bookVenue() { char venueName[50]; printf("Enter venue name to book: "); scanf("%s", venueName); // Check if the venue exists int venueIndex = -1; for (int i = 0; i < venueCount; i++) { if (strcmp(venues[i].name, venueName) == 0) { venueIndex = i; break; } } if (venueIndex == -1) { printf("Venue not found!\n"); return; } if (bookingCount >= MAX_BOOKINGS) { printf("Booking limit reached!\n"); return; } Booking newBooking; strcpy(newBooking.venueName, venueName); printf("Enter your name: "); scanf("%s", newBooking.userName); printf("Enter booking time (YYYY-MM-DD HH:MM): "); scanf("%s", newBooking.bookingTime); bookings[bookingCount++] = newBooking; printf("Booking successful!\n"); }

5. 取消预约

c
void cancelBooking() { char venueName[50]; char userName[50]; printf("Enter venue name to cancel booking: "); scanf("%s", venueName); printf("Enter your name: "); scanf("%s", userName); int bookingIndex = -1; for (int i = 0; i < bookingCount; i++) { if (strcmp(bookings[i].venueName, venueName) == 0 && strcmp(bookings[i].userName, userName) == 0) { bookingIndex = i; break; } } if (bookingIndex == -1) { printf("No matching booking found!\n"); return; } for (int i = bookingIndex; i < bookingCount - 1; i++) { bookings[i] = bookings[i + 1]; } bookingCount--; printf("Booking cancelled successfully!\n"); }

6. 主菜单

c
int main() { int choice; do { printf("\n--- Sports Venue Management System ---\n"); printf("1. Add Venue\n"); printf("2. Display Venues\n"); printf("3. Book Venue\n"); printf("4. Cancel Booking\n"); printf("5. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: addVenue(); break; case 2: displayVenues(); break; case 3: bookVenue(); break; case 4: cancelBooking(); break; case 5: printf("Exiting...\n"); break; default: printf("Invalid choice. Please try again.\n"); } } while (choice != 5); return 0; }

说明

  1. 数据存储:本示例中的数据是保存在内存中的,这意味着程序关闭后数据会丢失。实际应用中,可以考虑将数据存储到文件或数据库中。
  2. 错误处理:代码中的错误处理比较基础。实际应用中,应该加入更多的错误检查和用户输入验证。
  3. 界面改进:这里使用了简单的命令行界面。实际应用可以使用图形用户界面(GUI)库,如 GTK 或 Qt。

这个示例程序展示了基本的功能和结构,可以根据需求进一步扩展和优化。