使用c语言编写一个电影院管理系统,要求有用户登录,用户注册,用户登录后可以查看电影,购买电影票,退票操作,管理员登录,可以对电影进行增删改查,对用户管理,
时间: 2023-09-28 12:09:51 浏览: 47
好的,这是一个比较复杂的项目,需要设计数据库、用户界面等多个方面。以下是一个简单的电影院管理系统的C语言代码示例,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_MOVIES 100
#define MAX_USERS 100
struct movie {
char title[50];
char director[50];
int year;
int rating;
int tickets;
};
struct user {
char username[50];
char password[50];
int type;
};
struct movie movies[MAX_MOVIES];
int num_movies = 0;
struct user users[MAX_USERS];
int num_users = 0;
void add_movie() {
if (num_movies >= MAX_MOVIES) {
printf("Error: movie database is full\n");
return;
}
struct movie m;
printf("Enter movie title: ");
fgets(m.title, 50, stdin);
printf("Enter movie director: ");
fgets(m.director, 50, stdin);
printf("Enter movie year: ");
scanf("%d", &m.year);
printf("Enter movie rating (out of 10): ");
scanf("%d", &m.rating);
m.tickets = 50;
movies[num_movies] = m;
num_movies++;
printf("Movie added successfully\n");
}
void delete_movie() {
char title[50];
printf("Enter movie title to delete: ");
fgets(title, 50, stdin);
for (int i = 0; i < num_movies; i++) {
if (strcmp(movies[i].title, title) == 0) {
for (int j = i; j < num_movies - 1; j++) {
movies[j] = movies[j+1];
}
num_movies--;
printf("Movie deleted successfully\n");
return;
}
}
printf("Error: movie not found\n");
}
void edit_movie() {
char title[50];
printf("Enter movie title to edit: ");
fgets(title, 50, stdin);
for (int i = 0; i < num_movies; i++) {
if (strcmp(movies[i].title, title) == 0) {
printf("Enter new movie title (or press enter to skip): ");
fgets(movies[i].title, 50, stdin);
printf("Enter new movie director (or press enter to skip): ");
fgets(movies[i].director, 50, stdin);
printf("Enter new movie year (or press 0 to skip): ");
scanf("%d", &movies[i].year);
printf("Enter new movie rating (or press 0 to skip): ");
scanf("%d", &movies[i].rating);
printf("Movie edited successfully\n");
return;
}
}
printf("Error: movie not found\n");
}
void display_movies() {
printf("Title\t\tDirector\tYear\tRating\tTickets\n");
for (int i = 0; i < num_movies; i++) {
printf("%s\t%s\t%d\t%d\t%d\n", movies[i].title, movies[i].director, movies[i].year, movies[i].rating, movies[i].tickets);
}
}
void purchase_ticket() {
char title[50];
printf("Enter movie title to purchase ticket: ");
fgets(title, 50, stdin);
for (int i = 0; i < num_movies; i++) {
if (strcmp(movies[i].title, title) == 0) {
if (movies[i].tickets > 0) {
movies[i].tickets--;
printf("Ticket purchased successfully\n");
return;
} else {
printf("Error: no tickets left for this movie\n");
return;
}
}
}
printf("Error: movie not found\n");
}
void return_ticket() {
char title[50];
printf("Enter movie title to return ticket: ");
fgets(title, 50, stdin);
for (int i = 0; i < num_movies; i++) {
if (strcmp(movies[i].title, title) == 0) {
if (movies[i].tickets < 50) {
movies[i].tickets++;
printf("Ticket returned successfully\n");
return;
} else {
printf("Error: all tickets already returned\n");
return;
}
}
}
printf("Error: movie not found\n");
}
void add_user() {
if (num_users >= MAX_USERS) {
printf("Error: user database is full\n");
return;
}
struct user u;
printf("Enter username: ");
fgets(u.username, 50, stdin);
printf("Enter password: ");
fgets(u.password, 50, stdin);
printf("Enter user type (0 for regular user, 1 for administrator): ");
scanf("%d", &u.type);
users[num_users] = u;
num_users++;
printf("User added successfully\n");
}
void login() {
char username[50];
char password[50];
printf("Enter username: ");
fgets(username, 50, stdin);
printf("Enter password: ");
fgets(password, 50, stdin);
for (int i = 0; i < num_users; i++) {
if (strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) {
if (users[i].type == 0) {
printf("Welcome, regular user\n");
printf("1. Display movies\n");
printf("2. Purchase ticket\n");
printf("3. Return ticket\n");
int choice;
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
display_movies();
break;
case 2:
purchase_ticket();
break;
case 3:
return_ticket();
break;
default:
printf("Error: invalid choice\n");
}
} else if (users[i].type == 1) {
printf("Welcome, administrator\n");
printf("1. Add movie\n");
printf("2. Delete movie\n");
printf("3. Edit movie\n");
printf("4. Display movies\n");
printf("5. Add user\n");
int choice;
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_movie();
break;
case 2:
delete_movie();
break;
case 3:
edit_movie();
break;
case 4:
display_movies();
break;
case 5:
add_user();
break;
default:
printf("Error: invalid choice\n");
}
}
return;
}
}
printf("Error: invalid username or password\n");
}
int main() {
while (1) {
printf("1. Login\n");
printf("2. Register\n");
printf("Enter choice: ");
int choice;
scanf("%d", &choice);
getchar();
switch (choice) {
case 1:
login();
break;
case 2:
add_user();
break;
default:
printf("Error: invalid choice\n");
}
}
return 0;
}
```
需要注意的是,这只是一个简单的示例,实际上电影院管理系统需要更多的功能和更完善的错误处理。此外,还需要考虑如何设计数据库以及如何实现用户界面等问题。