본문 바로가기

Language/algorithm

[임시] 구구단

#include <stdio.h>

int step = 2;
char action = ' ';

int multiplication(int n){
    return step * n;
}

void printTableStyle(){
    int s = 2, i = 1;

    for( i = 1; 9 >= i; ++i ){
        // 2 ~ 5 단 출력
        for( s = 2; 5 >= s; ++s ){
            step = s;
            printf("%d x %d = %d", s, i, multiplication( i ) );
            if( 5 == s ){
                printf("\n");
            }else{
                printf("\t");
            }
        }
    }

    printf("\n");

    for( i = 1; 9 >= i; ++i ){
    // 6 ~ 9단
        for( s = 6; 9 >= s; ++s ){
            step = s;
            printf("%d x %d = %d", s, i, multiplication( i ) );
            if( 9 == s ){
                printf("\n");
            }else{
                printf("\t");
            }
        }
    }
   
}

void printStepStyle(){
    int i = 0;
    for( i = 1; 9 >= i; ++i ){
        printf("%d x %d = %d \n", step, i, multiplication( i ) );
    }
}

int main(){
   
    for( ; 'q' != action ; ){
        printf("help : \n");
        printf(" q : 종료\n" );
        printf(" s : 구구단 단수 입력\n");
        printf(" t : 구구단 테이블 형태로 표시\n" );
        printf(" c : 화면 지우기\n");
        printf("명령어 입력 : " );
        scanf("%c", &action );

        if( 's' == action ){
            printf("단수 입력 : ");
            scanf("%d", &step );
            printStepStyle();
        }else if('t' == action){
            printTableStyle();
        }else if( 'c' == action ){
            system("cls");
        }else{
            printf("잘못된 명령어를 입력하였습니다.\n");
        }

    }
   
    return 0;
}