본문 바로가기

Programming language/C++

[C++] Recipe Book 구현

C++에서 콘솔 기반 레시피 북을 만드는 것은 레시피를 관리하고 사용자에게 보여주며 특정 레시피를 검색할 수 있도록 하는 것입니다. 간단한 예는 다음과 같습니다:

 

프로그램 흐름

1. 이 프로그램은 레시피 추가, 레시피 보기, 종료의 세 가지 옵션을 제공합니다.
2. 사용자가 레시피를 추가하기로 결정하면(옵션 1), 이름, 재료(라인당 하나씩), 사용법(라인당 하나씩)을 제공하며, 프로그램은 레시피를 지도에 저장합니다.
3. 사용자가 레시피를 보기로 선택한 경우(옵션 2), 자신이 보고 싶은 레시피의 이름을 입력합니다. 프로그램은 레시피가 존재할 경우 해당 레시피를 검색하여 표시합니다.
4. 프로그램은 사용자가 종료를 선택할 때까지 계속 실행됩니다(옵션 3).

 

사용방법
프로그램을 사용하는 방법은 다음과 같습니다:
1. 프로그램을 실행합니다.
2. 옵션 1을 선택하여 레시피를 추가하고 레시피의 이름, 재료, 설명을 제공합니다.
3. 레시피를 보려면 option 2를 선택하고 보고 싶은 레시피의 이름을 입력합니다.
4. 2단계와 3단계를 반복하여 레시피를 추가하고 확인합니다.
5. 옵션 3을 선택하여 프로그램을 종료합니다.

이 간단한 레시피 북은 레시피를 추가하고, 보고, 관리할 수 있으며, 레시피 카테고리, 기존 레시피 편집, 재료별 레시피 검색 등의 기능을 추가하여 확장할 수 있습니다.

#include <iostream>
#include <map>
#include <vector>
using namespace std;

// Structure to represent a recipe
struct Recipe {
    string name;
    vector<string> ingredients;
    string instructions;
};

int main() {
    cout << "Recipe Book" << endl;

    // Create a map to store recipes (name as key, Recipe as value)
    map<string, Recipe> recipeBook;

    while (true) {
        cout << "\nOptions:" << endl;
        cout << "1. Add Recipe" << endl;
        cout << "2. View Recipe" << endl;
        cout << "3. Exit" << endl;
        cout << "Enter your choice (1, 2, or 3): ";

        int choice;
        cin >> choice;
        cin.ignore();

        if (choice == 1) {
            Recipe recipe;
            cout << "Enter the name of the recipe: ";
            getline(cin, recipe.name);

            cout << "Enter the ingredients (one per line, type 'done' to finish):\n";
            while (true) {
                string ingredient;
                getline(cin, ingredient);
                if (ingredient == "done") {
                    break;
                }
                recipe.ingredients.push_back(ingredient);
            }

            cout << "Enter the instructions (type 'done' on a new line to finish):\n";
            while (true) {
                string instruction;
                getline(cin, instruction);
                if (instruction == "done") {
                    break;
                }
                recipe.instructions += instruction + "\n";
            }

            recipeBook[recipe.name] = recipe;
            cout << "Recipe added successfully!" << endl;
        } else if (choice == 2) {
            cout << "Enter the name of the recipe you want to view: ";
            string recipeName;
            getline(cin, recipeName);

            if (recipeBook.find(recipeName) != recipeBook.end()) {
                Recipe recipe = recipeBook[recipeName];
                cout << "\nRecipe: " << recipe.name << endl;
                cout << "Ingredients:\n";
                for (const string& ingredient : recipe.ingredients) {
                    cout << "- " << ingredient << endl;
                }
                cout << "Instructions:\n" << recipe.instructions << endl;
            } else {
                cout << "Recipe not found." << endl;
            }
        } else if (choice == 3) {
            break; // Exit the program
        } else {
            cout << "Invalid choice. Please select 1, 2, or 3." << endl;
        }
    }

    return 0;
}