본문 바로가기
<ALGORITHM>/NOTE

20210208(월)

by CodeGrimie 2021. 2. 8.

쓰레드 & 리스트 연습

#include <cstdio>
#include <clocale>
#include <thread>
#include <list>

// 

using std::thread;

typedef struct _Book {
    unsigned int code;
    wchar_t name[20];
}Book;

std::list<Book> bookList;

void PushBook()
{
    Book book;
    wprintf(L"책 데이터를 (코드 제목) 순서로 입력하세요.\n");
    wprintf(L"데이터 : ");
    wscanf_s(L"%d %s", &(book.code), book.name, 20);
    bookList.push_back(book);
}

void PopBook()
{
    bookList.pop_front();
}

void PrintBooks()
{
    wprintf(L"  코드 제목 \n");
    for (auto i = bookList.begin(); i != bookList.end(); i++)
    {
        wprintf(L"   %d   %s \n", (*i).code, (*i).name);
    }
    wprintf(L"\n");
}

void mainThread()
{
    unsigned int inputCode = 0;
    while (1)
    {
        wprintf(L"[1 : PUSH] [2 : POP] [3 : PRINT]\n");
        wprintf(L"업무 코드 : ");
        wscanf_s(L"%d", &inputCode);
        wprintf(L"\n");

        switch (inputCode)
        {
        case 1:
        {
            PushBook();
            inputCode = 0;
        }
        break;

        case 2:
        {
            PopBook();
        }
        break;

        case 3:
        {
            PrintBooks();
        }

        break;

        default:
            wprintf(L"정상적이지 않은 업무 코드입니다.\n");
            break;

        }
    }
}

int main()
{
    _wsetlocale(LC_ALL, L"Korean");

    thread THREAD_MAIN(mainThread);
    THREAD_MAIN.join();

    return (0);
}

 

탐색 알고리즘(STL)

 

#include <iostream>
#include <vector>
#include <thread>
#include <string>

using std::thread;
using namespace std;

typedef struct _Fruit {
    string name;
    unsigned int amount;
}Fruit;

vector<Fruit> fruitList;

bool checkFruitStock(Fruit fruit)
{
    for (auto i = fruitList.begin(); i != fruitList.end(); i++)
    {
        if ((*i).name.compare(fruit.name) != 0)
        {
            return (false);
        }
    }
    return (true);
}

void ReceiveFruit()
{
    Fruit fruit;
    cout << "과일 데이터를 (이름 수량) 순서로 입력하세요.\n";
    cout << "과일 이름 : ";
    //getline(cin, fruit.name);
    cin >> fruit.name;
    cout << "과일 수량 : ";
    cin >> fruit.amount;

    vector<Fruit>::iterator it;
    it = find_if(fruitList.begin(), fruitList.end(), checkFruitStock);
    if (it != fruitList.end())
    {
        (*it).amount += fruit.amount;
    }
    else
    {
        fruitList.push_back(fruit);
    }
}

void ReleaseFruit()
{
    Fruit fruit;
    cout << "과일 데이터를 (이름 수량) 순서로 입력하세요.\n";
    cout << "과일 이름 : ";
    //getline(cin, fruit.name);
    cin >> fruit.name;
    cout << "과일 수량 : ";
    cin >> fruit.amount;

    vector<Fruit>::iterator it;
    it = find_if(fruitList.begin(), fruitList.end(), checkFruitStock);
    if (it != fruitList.end())
    {
        (*it).amount -= fruit.amount;
        if ((*it).amount < 0)
        {
            fruitList.erase(it);
        }
    }
}

void PrintFruit()
{
    cout << "과일   수량\n";
    for (auto i = fruitList.begin(); i != fruitList.end(); i++)
    {
        cout << (*i).name << "  " << (*i).amount << "\n";
    }
}

void MainThread()
{
    unsigned int inputCode = 0;
    while (1)
    {
        cout << "[1 : RECEIVE] [2 : RELEASE] [3 : PRINT]\n";
        cout << "업무 코드 : ";
        cin >> inputCode;
        cout << "\n";

        switch (inputCode)
        {
        case 1:
        {
            ReceiveFruit();
            inputCode = 0;
        }
        break;

        case 2:
        {
            ReleaseFruit();
        }
        break;

        case 3:
        {
            PrintFruit();
        }

        break;

        default:
            wprintf(L"정상적이지 않은 업무 코드입니다.\n");
            break;

        }
    }
}

int main()
{

    thread THREAD_MAIN(MainThread);
    THREAD_MAIN.join();

    return (0);
}

'<ALGORITHM> > NOTE' 카테고리의 다른 글

20210203(수)  (0) 2021.02.03
20210201(월)  (0) 2021.02.01
20210127(수)  (0) 2021.01.27
20210121(목)  (0) 2021.01.21
20210120(수)  (0) 2021.01.20

댓글