C/연습장

로또 / 덩어리3 함수화 하기 전

joo_coding 2025. 3. 16. 23:02
/* 헤더 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 6

/* 함수 목록 */
void f_com_lotto(int com_lotto[]) //매개변수를 배열로 설정
{
    srand(time(NULL)); // 이게 없으면 값이 고정됨

    for (int i = 0; i < 7; i++) // 0 1 2 3 4 5 6 (총 7개 픽) > 마지막이 보너스
    {
        com_lotto[i] = (rand() % 45 +1); // 범위: 1~45

        for (int j=0; j < i; j++) // 중복 걸러주기
        {
            if (com_lotto[i] == com_lotto[j]) // 먼저 뽑은거랑 같지 않아야 저장
            {
                //j=0,i=0 > 0 0 비교 >
                i --; // 중복이 생기면 해당 i를 제거하고 다시 뽑기
                break;
            }
        }
    }

    for (int i = 0; i < 7; i++) {
        printf("%d ", com_lotto[i]);
    }
    printf("\n");
}

void f_user_lotto(int user_lotto[]) //유저가 번호 고르는 함수
{
    user_lotto[6]; // 유저가 고른 로또번호 (배열의 길이가 6) 0~5

    for (int i = 0; i < 6; i++) // 0 1 2 3 4 5 (총 6개 픽)
    {
        printf("로또 번호를 고르세요(1~45):");
        scanf("%d", &user_lotto[i]);

        // 범위 검사
        while (user_lotto[i] <= 0 || user_lotto[i] > 45) // 고른 숫자가 0이하, 45이상이면 다시 골라
        {
            printf("1~45 중에서만 골라.\n");
            i --; // 잘못 고르면 해당 i를 제거하고 다시 뽑기
            break; // 여기 끝내
        }

        // 중복 검사
        for (int j = 0; j < i; j++) // 중복 걸러주기
        {
            if (user_lotto[i] == user_lotto[j]) // 먼저 뽑은거랑 같지 않아야 저장
            {
                printf("중복이야. 다시 골라.\n");
                i --; // 중복이 생기면 해당 i를 제거하고 다시 뽑기
                break; // 여기 끝내
            }
        }

        // 범위도 맞고, 중복도 아니면 출력
        //printf("고른번호:%d\n", user_lotto[i]);

    } // 다 고르면 뭐골랐는지 보여주기
    printf("고른 번호는:");
    for (int k = 0; k < 6; k++)
    {
        printf ("%d ", user_lotto[k]);
    }
    printf("\n");
}

void f_reward(int same, int bonus) //등수확인 함수
{
    if (same==3)
    {
        printf("5등");
    }

    else if (same==4)
    {
        printf("4등");
    }

    else if (same==5)
    {
        if (bonus != 1)
        {
            printf("3등");
        }
        
        printf("2등");
    }

    else if (same==6)
    {
        printf("1등");
    }
    
    else
    {printf("꽝");}
}

void f_bonus(int bonus, int same, int user_lotto[], int com_lotto[])
{
    bonus = 0; // 보너스 번호
    same = 0; //번호가 같은 경우가 몇번인지 저장하는 변수
    for (int i =0; i < 6; i++) // 유저의 1번이랑
    {
        for (int j = 0; j < 6; j++) // 컴퓨터의 1~6까지 비교
        {
            if (user_lotto[i] == com_lotto[j])
            {
                same++;
            }
        }
        
        // printf("유저:%d ", user_lotto[i]);
        // printf("컴:%d\n", com_lotto[6]); //보너스번호 테스트

        if (user_lotto[i] == com_lotto[6])
        {
            bonus++;
        }
    }

    printf("당첨갯수:%d\n", same);
    printf("보너스번호:%d\n", bonus);

    f_reward(same, bonus); // 등수확인 함수
}

/* 몸통 */
int main() /* 덩어리3개=컴퓨터, 유저, 등수 */
{
    int count=0; //회차
    int user_lotto[SIZE]; // 유저가 고른 로또번호 (배열의 길이가 6) 0~5
    int com_lotto[7]; // 컴퓨터가 선정한 로또번호의 배열 - [6]은 보너스

    while (1)
    {
     /* 덩어리1 */
        /* 1. 당첨번호 선정하기 */
        f_com_lotto(com_lotto); // 함수의 배열을 메인 배열로 받아주기
    
     /* 덩어리2 */
        /* 2. 유저가 번호 고르기 */
        // 번호를 고른다 > 중복인가? > 다시고른다
        // 저장의 조건: 숫자, 1~45, 중복x, 6개
        // 문자를 고르는 경우는 아직 구현 못하겠음
        f_user_lotto(user_lotto); // 유저가 선정한 로또번호
    
     /* 덩어리3 */
        /* 3. 등수 확인하기 */
        // 유저의 번호가 당첨번호에 들어있는지 검사
    
        int bonus = 0; // 보너스 번호
        int same = 0; //번호가 같은 경우가 몇번인지 저장하는 변수
        for (int i =0; i < 6; i++) // 유저의 1번이랑
        {
            for (int j = 0; j < 6; j++) // 컴퓨터의 1~6까지 비교
            {
                if (user_lotto[i] == com_lotto[j])
                {
                    same++;
                }
            }
            
            // printf("유저:%d ", user_lotto[i]);
            // printf("컴:%d\n", com_lotto[6]); //보너스번호 테스트
    
            if (user_lotto[i] == com_lotto[6])
            {
                bonus++;
            }
        } 
        printf("당첨갯수:%d\n", same);
        printf("보너스번호:%d\n", bonus);

        f_reward(same, bonus); // 등수확인 함수
        getchar();

    /* 덩어리4 */
        /* 4. 재구매 여부 */
        char answer;
        printf("\n로또를 더 구매하시겠습니까?(y/n)");
        scanf("%c", &answer);

        if (answer == 'y')
        {
            printf("처음으로 돌아갑니다.(enter)\n");
            getchar();
            continue;
        }
        else if (answer == 'n')
        {
            printf("로또를 종료합니다.");
            break;
        }

    }/* whlie 1문 끝*/
    return 0; /* 끝 */
}