电影院管理系统C语言代码
时间: 2023-08-18 11:39:57 浏览: 137
以下是一个简单的电影院管理系统的C语言代码,仅供参考:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_MOVIES 100 // 最多支持100部电影
#define MAX_NAME_LEN 50 // 电影名称最大长度为50个字符
typedef struct {
char name[MAX_NAME_LEN];
char director[MAX_NAME_LEN];
char actors[MAX_NAME_LEN];
char date[MAX_NAME_LEN];
float price;
int seats[5][10]; // 座位信息,5排10座
} Movie;
Movie movies[MAX_MOVIES]; // 电影列表
int num_movies = 0; // 电影数量
// 显示菜单
void show_menu() {
printf("电影院管理系统\n");
printf("1. 录入电影信息\n");
printf("2. 查询电影信息\n");
printf("3. 预订电影票\n");
printf("4. 取消电影票\n");
printf("5. 打印电影票\n");
printf("6. 统计电影票销售情况\n");
printf("0. 退出系统\n");
printf("请选择操作:");
}
// 录入电影信息
void add_movie() {
if (num_movies >= MAX_MOVIES) {
printf("电影列表已满,无法添加新电影!\n");
return;
}
Movie movie;
printf("请输入电影名称:");
scanf("%s", movie.name);
printf("请输入导演名称:");
scanf("%s", movie.director);
printf("请输入演员名称:");
scanf("%s", movie.actors);
printf("请输入上映日期:");
scanf("%s", movie.date);
printf("请输入票价:");
scanf("%f", &movie.price);
memset(movie.seats, 0, sizeof(movie.seats)); // 初始化座位信息
movies[num_movies++] = movie;
printf("电影信息已录入!\n");
}
// 查询电影信息
void find_movie() {
char name[MAX_NAME_LEN];
printf("请输入要查询的电影名称:");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_movies; i++) {
if (strcmp(name, movies[i].name) == 0) {
printf("电影名称:%s\n", movies[i].name);
printf("导演名称:%s\n", movies[i].director);
printf("演员名称:%s\n", movies[i].actors);
printf("上映日期:%s\n", movies[i].date);
printf("票价:%f\n", movies[i].price);
found = 1;
break;
}
}
if (!found) {
printf("未找到该电影!\n");
}
}
// 预订电影票
void book_ticket() {
char name[MAX_NAME_LEN];
printf("请输入要预订的电影名称:");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_movies; i++) {
if (strcmp(name, movies[i].name) == 0) {
printf("请选择场次(1-5):");
int session;
scanf("%d", &session);
if (session < 1 || session > 5) {
printf("场次选择错误!\n");
return;
}
int row, col;
printf("请选择座位(行 列):");
scanf("%d %d", &row, &col);
if (row < 1 || row > 5 || col < 1 || col > 10) {
printf("座位选择错误!\n");
return;
}
if (movies[i].seats[row - 1][col - 1] == 1) {
printf("该座位已经被预订,请重新选择座位!\n");
return;
}
movies[i].seats[row - 1][col - 1] = 1;
printf("电影票预订成功!\n");
found = 1;
break;
}
}
if (!found) {
printf("未找到该电影!\n");
}
}
// 取消电影票
void cancel_ticket() {
char name[MAX_NAME_LEN];
printf("请输入要取消预订的电影名称:");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_movies; i++) {
if (strcmp(name, movies[i].name) == 0) {
int row, col;
printf("请输入要取消的座位(行 列):");
scanf("%d %d", &row, &col);
if (row < 1 || row > 5 || col < 1 || col > 10) {
printf("座位选择错误!\n");
return;
}
if (movies[i].seats[row - 1][col - 1] == 0) {
printf("该座位未被预订,无法取消!\n");
return;
}
movies[i].seats[row - 1][col - 1] = 0;
printf("电影票取消成功!\n");
found = 1;
break;
}
}
if (!found) {
printf("未找到该电影!\n");
}
}
// 打印电影票
void print_ticket() {
char name[MAX_NAME_LEN];
printf("请输入要打印电影票的电影名称:");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_movies; i++) {
if (strcmp(name, movies[i].name) == 0) {
int row, col;
printf("请输入座位号(行 列):");
scanf("%d %d", &row, &col);
if (row < 1 || row > 5 || col < 1 || col > 10) {
printf("座位选择错误!\n");
return;
}
if (movies[i].seats[row - 1][col - 1] == 0) {
printf("该座位未被预订,无法打印电影票!\n");
return;
}
printf("电影名称:%s\n", movies[i].name);
printf("上映日期:%s\n", movies[i].date);
printf("票价:%f\n", movies[i].price);
printf("座位号:%d %d\n", row, col);
found = 1;
break;
}
}
if (!found) {
printf("未找到该电影!\n");
}
}
// 统计电影票销售情况
void calc_sales() {
char start_date[MAX_NAME_LEN], end_date[MAX_NAME_LEN];
printf("请输入起始日期:");
scanf("%s", start_date);
printf("请输入结束日期:");
scanf("%s", end_date);
float total_sales = 0;
int total_customers = 0;
for (int i = 0; i < num_movies; i++) {
if (strcmp(start_date, movies[i].date) <= 0 && strcmp(end_date, movies[i].date) >= 0) {
int num_customers = 0;
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 10; k++) {
if (movies[i].seats[j][k] == 1) {
num_customers++;
}
}
}
total_sales += movies[i].price * num_customers;
total_customers += num_customers;
}
}
printf("销售情况如下:\n");
printf("总票房:%f\n", total_sales);
printf("观影人数:%d\n", total_customers);
}
int main() {
int choice;
while (1) {
show_menu();
scanf("%d", &choice);
switch (choice) {
case 0:
printf("谢谢使用!\n");
return 0;
case 1:
add_movie();
break;
case 2:
find_movie();
break;
case 3:
book_ticket();
break;
case 4:
cancel_ticket();
break;
case 5:
print_ticket();
break;
case 6:
calc_sales();
break;
default:
printf("选择错误,请重新选择操作!\n");
break;
}
}
}
```
以上是一个简单的电影院管理系统的C语言代码,可以进行基本的电影信息录入、查询、预订、取消、打印和销售情况统计等操作。但是,这个程序还有很多可以改进的地方,例如数据存储方式、输入验证等。
阅读全文