Movie Manager
🔫 문제: 특정한 주제의 목록을 관리하는 프로그램
- 구조체 배열로 (자유 주제의)목록 생성
- 구조체 배열을 초기화 할 때 3개를 미리 입력
- 구조체 멤버로 열거형, 문자열, 숫자각 1개 이상 포함
- 목록 출력 기능
- 목록 추가 기능(사용자 입력)
- 루프 안에서 출력/추가/종료를 선택
🔫 풀이:
main()
#include <stdio.h>
#include <string.h>
enum MovieRating {
GENERAL = 0,
PG12 = 12,
R15 = 15,
R18 = 18
};
struct Movie {
char genre[30];
char movieName[50];
char releaseDate[15];
int audienceCount;
enum MovieRating rating;
};
void printMovieList(struct Movie movies[], int size);
void addMoive(struct Movie movies[], int *size);
int main() {
struct Movie movies[100] = {
{"Musical", "The Wizard of Oz", "1939-08-05", 13078, GENERAL},
{"Anti-Hero", "Venom", "2018-10-03", 3888096, R15},
{"Comedy Horror", "Beetlejuice Beetlejuice", "2024-09-04", 119861, PG12}
};
int count = 3;
int choice;
while(1){
printf("\nMovie Manager\n");
printf("1. Show Movies\n");
printf("2. Add New Movie\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar();
switch (choice)
{
case 1: printMovieList(movies, count); break;
case 2: addMoive(movies, &count); break;
case 3: printf("Exiting program. Goodbye!\n"); return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
printMovieList()
void printMovieList(struct Movie movies[], int size){
for (int i = 0; i < size; i++) {
printf("Genre: %s\n", movies[i].genre);
printf("Moive Name: %s\n", movies[i].movieName);
printf("Release Date: %s\n", movies[i].releaseDate);
printf("Audience Count: %d\n", movies[i].audienceCount);
printf("Rating: ");
switch (movies[i].rating)
{
case GENERAL: printf("General Audience (G)\n"); break;
case PG12: printf("Parental Guidance (12+)\n"); break;
case R15: printf("Restricted (15+)\n"); break;
case R18: printf("Restricted (18+)\n"); break;
}
printf("\n");
}
return;
}
addMoive()
void addMoive(struct Movie movies[], int *size){
if (*size >= 100) {
printf("Error: Movie list is full. Cannot add more movies.\n");
return;
}
struct Movie newMovie;
printf("Enter genre: ");
fgets(newMovie.genre, sizeof(newMovie.genre), stdin);
newMovie.genre[strcspn(newMovie.genre, "\n")] = '\0';
printf("Enter movie name: ");
fgets(newMovie.movieName, sizeof(newMovie.movieName), stdin);
newMovie.movieName[strcspn(newMovie.movieName, "\n")] = '\0';
printf("Enter release date (YYYY-MM-DD): ");
fgets(newMovie.releaseDate, sizeof(newMovie.releaseDate), stdin);
newMovie.releaseDate[strcspn(newMovie.releaseDate, "\n")] = '\0';
printf("Enter audience count: ");
scanf("%d", &newMovie.audienceCount);
getchar();
printf("Enter rating (0: GENERAL, 12: PG12, 15: R15, 18: R18): ");
int ratingInput;
scanf("%d", &ratingInput);
getchar();
newMovie.rating = (enum MovieRating)ratingInput;
movies[*size] = newMovie;
(*size)++;
return;
}