본문 바로가기

숙제

C++ 숙제 (영화 데이터를 관리하는 프로그램)

1. 영화 데이터를 관리하는 프로그램을 설계하고 구현해봅시다. 세부적인 요구사항은 아래와 같습니다.

영화는 다음 정보를 포함 합니다.

  • 제목(string 타입)
  • 평점(double 타입)

구현해야 할 기능은 아래와 같습니다.

  • 특정 평점 이상 영화 필터링
  • 영화 목록 출력
  • 영화 제목으로 검색
  • 영화 평점 기준 정렬

코드 실행시 출력되는 로그

1. 영화 목록 출력
영화 목록:
제목: Inception, 평점: 9
제목: Interstellar, 평점: 8.6
제목: The Dark Knight, 평점: 9.1
제목: Memento, 평점: 8.4

2. 영화 검색 (예: Interstellar)
영화 제목: Interstellar, 평점: 8.6

3. 평점 기준 정렬 및 출력
평점 기준 정렬된 영화 목록:
제목: The Dark Knight, 평점: 9.1
제목: Inception, 평점: 9
제목: Interstellar, 평점: 8.6
제목: Memento, 평점: 8.4

4. 평점 8.5 이상인 영화 필터링 및 출력
평점 8.5 이상인 영화 목록:
제목: The Dark Knight, 평점: 9.1
제목: Inception, 평점: 9
제목: Interstellar, 평점: 8.6

코드 구조

기본 뼈대 코드를 활용해 코드를 작성해 보세요.

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

struct Movie {
    string title;
    double rating;
};

// TODO: MovieProcessor 추상 클래스 정의
// 순수 가상 함수 process를 선언해야 합니다.
// process는 vector<Movie>&를 인자로 받아야 합니다.


// 기본 영화 관리자
class MovieManager {
private:
    vector<Movie> movies;
    map<string, double> movieMap;

public:
    MovieManager() {
        // 초기 데이터 설정
        movies = {
            {"Inception", 9.0},
            {"Interstellar", 8.6},
            {"The Dark Knight", 9.1},
            {"Memento", 8.4}
        };

        for (const auto& movie : movies) {
            movieMap[movie.title] = movie.rating;
        }
    }

    void printMovies() {
        cout << "영화 목록:\n";
        for (const auto& movie : movies) {
            cout << "제목: " << movie.title << ", 평점: " << movie.rating << "\n";
        }
    }

    void findMovie(const string& title) {
        auto it = movieMap.find(title);
        if (it != movieMap.end()) {
            cout << "영화 제목: " << it->first << ", 평점: " << it->second << "\n";
        }
        else {
            cout << "해당 영화는 목록에 없습니다.\n";
        }
    }

    // MovieProcessor를 사용하여 기능 확장
    void processMovies(MovieProcessor& processor) {
        processor.process(movies);
    }
};


// TODO: compareMovies 함수 정의
// Movies 객체의 대소를 비교하는 함수 입니다.
// STL에서 제공하는 sort 함수를 활용해서 vector<Movie>를 멤버변수 rating 기준 내림차순으로 정렬 할 수 있도록 해야 합니다. 
    
// TODO: RatingSorter 클래스 정의
// MovieProcessor를 상속받아 구현합니다.
// process 는 vector<Movie>&를 인자로 받으며 영화목록이 저장되어 있습니다.
// process는 인자로 받은 벡터는 내림차순으로 정렬하고, 정렬된 영화목록을 출력합니다.


// 구체 클래스: 특정 평점 이상의 영화 필터링
class RatingFilter : public MovieProcessor {
private:
    double minRating;

public:
    explicit RatingFilter(double minRating) : minRating(minRating) {}

    void process(vector<Movie>& movies) {
        cout << "평점 " << minRating << " 이상인 영화 목록:\n";
        for (const auto& movie : movies) {
            if (movie.rating >= minRating) {
                cout << "제목: " << movie.title << ", 평점: " << movie.rating << "\n";
            }
        }
    }
};

int main() {
    MovieManager manager;

    cout << "1. 영화 목록 출력\n";
    manager.printMovies();

    cout << "\n2. 영화 검색 (예: Interstellar)\n";
    manager.findMovie("Interstellar");

    cout << "\n3. 평점 기준 정렬 및 출력\n";
    RatingSorter sorter;
    manager.processMovies(sorter);

    cout << "\n4. 평점 8.5 이상인 영화 필터링 및 출력\n";
    RatingFilter filter(8.5);
    manager.processMovies(filter);

    return 0;
}

MovieProcessor(인터페이스)

#pragma once
#include <vector>
#include "Movie.h"

class MovieProcessor
{
public:
	virtual void process(std::vector<Movie>& movies) = 0;

	~MovieProcessor() {};
};

Movie

#pragma once
#include <string>

struct Movie
{

	std::string title;
	double rating;
};

MovieManager(숙제에서 준 코드 선언부, 구현부만 나눔)

// MovieManager.h

#pragma once
#include "Movie.h"
#include "MovieProcessor.h"
#include <map>

class MovieManager
{
	std::vector<Movie> movies;
	std::map<std::string, double> movieMap;

public:

	MovieManager()
	{
		// 초기 데이터 설정
		movies =
		{
			{"Inception", 9.0},
			{"Interstellar", 8.6},
			{"The Dark Knight", 9.1},
			{"Memento", 8.4}
		};
		for (const auto& movie : movies)
		{
			movieMap[movie.title] = movie.rating;
		}
	}

	void printMovies();
	void findMovie(const std::string& title);
	void processMovies(MovieProcessor& processor);
};
// MovieManager.cpp

#include "MovieManager.h"
#include <iostream>

using namespace std;

void MovieManager::printMovies()
{
    cout << "영화 목록:" << endl;

    for (const auto& movie : movies)
    {
        cout << "제목: " << movie.title << ", 평점: " << movie.rating << "\n";
    }
}

void MovieManager::findMovie(const string& title)
{
    auto it = movieMap.find(title);

    if (it != movieMap.end())
    {
        cout << "영화 제목: " << it->first << ", 평점: " << it->second << "\n";
    }
    else {
        cout << "해당 영화는 목록에 없습니다.\n";
    }
}

// MovieProcessor를 사용하여 기능 확장
void MovieManager::processMovies(MovieProcessor& processor)
{
    processor.process(movies);
}

RatingFilter(숙제에서 준 코드 선언부, 구현부만 나눔)

//RatingFilter.h

#pragma once
#include "MovieProcessor.h"

class RatingFilter : public MovieProcessor
{
	double minRating;

public:

	RatingFilter(double minRating) : minRating(minRating) {};
	virtual void process(std::vector<Movie>& movies) override;
};
//RatingFilter.cpp

#include "RatingFilter.h"
#include <iostream>

using namespace std;

void RatingFilter::process(vector<Movie>& movies)
{
    cout << "평점 " << minRating << " 이상인 영화 목록:\n";

    for (const auto& movie : movies)
    {
        if (movie.rating >= minRating)
        {
            cout << "제목: " << movie.title << ", 평점: " << movie.rating << "\n";
        }
    }
}

RatingSorter(직접 구현한 부분)

//RatingSorter.h

#pragma once
#include "MovieProcessor.h"

class RatingSorter : public MovieProcessor
{
public:

	virtual void process(std::vector<Movie>& movies) override;
	static bool compareMovies(const Movie& a, const Movie& b);
};
//RatingSorter.cpp
//내가 구현해야 할 기능은 벡터의 데이터를 평점기준으로 내림차순 출력하기

#include "RatingSorter.h"
#include <algorithm>
#include <iostream>

using namespace std;

void RatingSorter::process(vector<Movie>& movies)
{
	sort(movies.begin(), movies.end(), compareMovies);

	for (auto m : movies)
	{
		cout << "제목: " << m.title << ", 평점: " << m.rating << endl;
	}
}

bool RatingSorter::compareMovies(const Movie& a, const Movie& b)
{
	return a.rating > b.rating;
}

main (숙제에서 준 코드)

//main.cpp

#include "MovieManager.h"
#include "RatingSorter.h"
#include "RatingFilter.h"
#include <iostream>

using namespace std;

int main()
{
    MovieManager manager;

    cout << "1. 영화 목록 출력\n";
    manager.printMovies();

    cout << "\n2. 영화 검색 (예: Interstellar)\n";
    manager.findMovie("Interstellar");

    cout << "\n3. 평점 기준 정렬 및 출력\n";
    RatingSorter sorter;
    manager.processMovies(sorter);

    cout << "\n4. 평점 8.5 이상인 영화 필터링 및 출력\n";
    RatingFilter filter(8.5);
    manager.processMovies(filter);

    return 0;
}

결과 (사실상 3번만 구현한 것)


숙제를 완료 하고 보니 내가 직접 구현할 것은 별로 없었지만 숙제에서 제공한 코드가 어떤 뜻인지, 함수에 인자로 받은 값이 어떤 클래스랑 연결이 되어있나 다 이해를 하려고 하나씩 읽어보고 이해가 안되는 것을 gpt에 물어보면서 하니 시간이 꽤 오래걸렸다. (3시간정도)

 

숙제에서 요구한 것은 출력 기준 3번인 평점 기준 정렬 및 출력 뿐이였는데 숙제를 할 때는 그것을 몰랐고 다 하고나서 깨달았다.