#include <iostream>
#include <windows.h>
#include <thread>
#include <string>
#include <list>
#include <random>
#include <cassert>
#include <time.h>

using namespace std;

void sleepForSeconds(int second);

void countDown(int second);

string getMember(int index, list<string> members);

int main(void)
{
    const int COUNT_SECONDS = 5;
    const int SLEEP_SECONDS = 1;

    int index = 0;
    string challenge_word;
    list<string> uniqueMembers;
    string current_line;
    string previous_line = "ERROR";
    bool is_time = true;
    bool is_name = false;
    bool is_comment = false;
    int luckyIndex;
    string luckyMember;

    cout << "도전 키워드를 입력해주세요\n\n";

    cin >> challenge_word;

    cout << "\n이번 이벤트의 도전 키워드는 \"" << challenge_word << "\" 입니다.\n";
    cout << challenge_word << "를 댓글에 입력해주세요\n\n";

    cout << "댓글 내용을 붙여넣고 마지막에 \"quit\"을 입력 후 엔터를 입력해주세요.\n\n";

    getline(cin, current_line);
    while (!(is_time && current_line.compare("quit") == 0)) {
        if (current_line.compare("") == 0) {
            is_time = true;
            is_name = false;
            is_comment = false;
        }
        else if (is_time) {
            is_time = false;
            is_name = true;
        }
        else if (is_name) {
            is_name = false;
            is_comment = true;
        }
        else if (is_comment) {
            is_comment = false;
            if (current_line.find(challenge_word) != string::npos) {
                uniqueMembers.remove(previous_line);
                uniqueMembers.push_back(previous_line);
            }
        }
        previous_line = current_line;
        getline(cin, current_line);
    }

    cout << "\n입력이 완료되었습니다.\n\n";

    sleepForSeconds(SLEEP_SECONDS);

    if (uniqueMembers.size() == 0) {
        cout << "참가자가 없습니다.\n";
        cout << "프로그램을 종료합니다.\n";
        return 1;
    }

    cout << "이번 이벤트에 " << uniqueMembers.size() << "분께서 참여해주셨습니다.\n";
    sleepForSeconds(SLEEP_SECONDS);
    cout << "성원에 감사드립니다.\n\n";
    sleepForSeconds(SLEEP_SECONDS);

    cout << "================참가자  명단================\n";
    index = 0;
    for (list<string>::const_iterator member = uniqueMembers.begin(); member != uniqueMembers.end(); ++member) {
        if (index > 0) {
            cout << ", ";
        }
        if (index % 5 == 0) {
            cout << "\b\b";
        }
        cout << "<" << member->c_str() << ">님";
        if (index % 5 == 4) {
            cout << "\n";
        }
        index++;
    }
    cout << "\n============================================\n\n";
    sleepForSeconds(SLEEP_SECONDS);

    cout << "추첨을 시작하겠습니다.\n";

    countDown(COUNT_SECONDS);
    
    srand(time(NULL));
    luckyIndex = rand() % uniqueMembers.size();
    luckyMember = getMember(luckyIndex, uniqueMembers);

    cout << "\n" << luckyMember.c_str() << " 님 축하드립니다.\n\n";

    sleepForSeconds(SLEEP_SECONDS);

    do {
        cout << "프로그램을 종료하시려면 q를 입력하고 엔터를 입력해주세요.\n";
    } while (cin.get() != 'q');

    cout << "프로그램을 종료합니다.\n";

    return 0;
}

void sleepForSeconds(int second)
{
    Sleep(second * 1000);
}

void countDown(int second)
{
    for (int i = second; i > 0; --i) {
        cout << i;
        for (int j = 0; j < 5; ++j) {
            Sleep(200);
            cout << ".";
        }
        cout << "\n";
    }
}

string getMember(int index, list<string> members)
{
    int i = 0;
    for (list<string>::const_iterator member = members.begin(); member != members.end(); ++member) {
        if (i++ == index) {
            return member->c_str();
        }
    }
    assert(1);
    return "ERROR";
}

컴파일 방법

gcc -o server server.c -lpthread

gcc -o client client.c -lpthread

 

 

채팅 프로그램.zip
0.00MB

'프로그래밍 강의 > C 언어' 카테고리의 다른 글

추첨 프로그램 소스  (0) 2021.12.04

CRC 구하는 프로젝트 2개 첨부합니다.

개발 환경은 visual studio 2017입니다.

CRC 예제 프로그램.zip
0.00MB
CRC 완전 기본만.zip
0.00MB

시리얼 통신 프로그램 예제 첨부 하였습니다.

비주얼 스트디오 2017로 프로젝트 생성 했습니다.

시리얼 통신 프로그램 예제.zip
0.01MB

'프로그래밍 강의 > 모드버스' 카테고리의 다른 글

모드버스 CRC 예제 프로젝트 2개  (0) 2021.03.07

+ Recent posts