ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C Language] 16. switch 문 - C 언어
    CSE/C Language 2015. 6. 13. 10:15
    1. 사용방법 
     switch() 문의 형식은 아래와 같다.


     switch (수식)

     {

        case 상수1:

    수행문;

    수행문;

    ...

    break;


        case 상수2:

    수행문;

    수행문;

    ...

    break;


        default:

    수행문;

    수행문;

    ...

    break;


     }

     switch (operator)

     {

        case 1:

    puts("1번 case");

    break;


        case 2:

    puts("2번 case");

    break;


        default:

    puts("error");

     

     }





     switch()에 사용된 수식의 결과가 case 문과 비교가 된다. 위에서는 수식의 결과값이 1 인 경우와 2 인 경우를 검사하여 각각에 맞게 출력하는 예제이다. 
     break 문을 사용하지 않아도 문법적인 에러를 유발하진 않지만 논리적인 에러를 유발할 위험성이 있으므로 항상 사용하는 것이 좋다.
     default 는 수식의 결과값이 case의 어디에도 걸리지 않으면 수행되는 문이다.

     
     아래 예제는 입력된 숫자에서 각각의 갯수를 구하는 예제이다.


     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    /*
     * countNum.c
     *
     *  Created on: 2015. 5. 12.
     *      Author: root
     */
     
    #include <stdio.h>
     
    int main(void) {
     
        int i;
        int ch;
        int count_digit[10= {0, };
     
        while ((ch = getchar())!= '\n') {
            switch (ch) {
            case '0' : count_digit[ch - '0']++break;
            case '1' : count_digit[ch - '0']++break;
            case '2' : count_digit[ch - '0']++break;
            case '3' : count_digit[ch - '0']++break;
            case '4' : count_digit[ch - '0']++break;
            case '5' : count_digit[ch - '0']++break;
            case '6' : count_digit[ch - '0']++break;
            case '7' : count_digit[ch - '0']++break;
            case '8' : count_digit[ch - '0']++break;
            case '9' : count_digit[ch - '0']++break;
     
            }
        }
     
        for (i = 0; i < 10; i++)
            printf("[%d] %d\n", i, count_digit[i]);
     
        return 0;
    }
    cs

     









    다음은 16진수에서 2진수로 변환하는 예제이다.





     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    /*
     * haxtobin.c
     *
     *  Created on: 2015. 5. 12.
     *      Author: root
     */
     
    #include <stdio.h>
     
    int main(void) {
     
        char *data = "3AF7";
     
        int i, j;
        char buf[32];
     
        // string copy
        strcpy(buf, "");
     
        for (i = 0; i < 8; i++) {
            switch (data[i]) {
            case 'A':
                strcat(buf, "1010");
                continue;
            case 'B':
                strcat(buf, "1011");
                continue;
            case 'C':
                strcat(buf, "1100");
                continue;
            case 'D':
                strcat(buf, "1101");
                continue;
            case 'E':
                strcat(buf, "1110");
                continue;
            case 'F':
                strcat(buf, "1111");
                continue;
            }
     
            for (j = 0; j < 4; j++)
                if ((((data[i] - '0'<< j) & 0x8> 0)
                    strcat(buf, "1");
                else
                    strcat(buf, "0");
        }
     
        buf[ 31= '\0';
        puts(buf);
        return 0;
    }
    cs

     










     

     

     

    * Programming in C 서적을 참고하여 작성하였습니다 


    댓글

Designed by Tistory.