2008년 1학기 시스템 프로그래밍 과제

 

3. AtoI & ItoA

Write a C program to convert a string of numbers (e.g. “3456”)

into a binary (unsigned) and vice verse.

The length of the string can be arbitrary.

 

 

이는 10진수를 바꾸는 것이라 자리수를 맞추고자 제곱을 계산하는 함수를 만들었습니다.

그 외에 좋은 방법은 떠오르지 않더군요.OTL....

 

 

코드

/*
*     Ascii_to_Integer (AtoI)
*    
*     This function is converting from string(ASCII) to integer.
*
*     - Argument
*     input_str : user input text
*
*     - Return
*     unsigned int : converted from text to number
*
*
*     str_size is length of input_str.
*
*     1. get byte at input_str
*     2. if byte is located in '0' to '9' then it's number string.
*     3. 2 is true then byte - '0' because '1' 's ascii code value is '0' + 1.
*     and muply the 10's power because it decimal number.
*     I calculate the cipher use integer_power function base is 10 and exponent is 'str_size - for_loop_i - 1'.
*     Because for_loop_i grow up then cipher go down.
*     4. Finally return the reslut
*
*     ASCII CODE TABLE
*     Decimal     ASCII
*     48          0
*     49          1
*     50          2
*     51          3
*     52          4
*     53          5
*     54          6
*     55          7
*     56          8
*     57          9
*
*
*     Made by Bak JinYeong, 2008.03.13
*     Edited by Bak JinYeong, 2008.03.14*/
unsigned int Ascii_to_Integer(char* input_str)
{
    int for_loop_i = 0; // integer variable use loop for loop sentence
    int str_size = My_strlen(input_str); // length of input_str
    unsigned int result_value = 0; // unsigned int variable use result value. It intialize 0.

    for(for_loop_i = 0 ; for_loop_i < str_size ; for_loop_i++) // looping length of input_str times.
    {
        if('0' <= input_str[for_loop_i] && input_str[for_loop_i] <= '9') // if input_str's byte is in '0' to '9'
        {
            result_value += (input_str[for_loop_i] - '0') * integer_power(10, (str_size - for_loop_i - 1)); // get number and set cipher
        }
        else // input_str's byte is not in '0' to '9'
        {
            return 0; // I just use return 0; rather than other good error handle.
        }
    }

    return result_value; // for loop sentence finished, then return result_value.
}

/*
*     Integer_to_Ascii (ItoA)
*
*     This function is converting from integer to string(ASCII).
*
*     - Argument
*     input_int : user input integer
*     output_str : result
*
*     - Return
*     void(because I write string at output_str)
*
*
*     number_size is cipher. (e.g. 123456's number_size is 6)
*     divided by 10 and count the number of times.
*
*     First get most significant number (e.g. 123456's most significant number is 1)
*     to divide 10 multiple (e.g. 123456 / 100000 = 1.23456 and int has integer type so it remains 1)
*     and calculate the remainder by 10. so I get the one number (e.g. 1 % 10 = 1)
*     and add '0' to convert number to ASCII code (e.g. 1 -> '1')
*
*     go to the First get next number to divided by 10 and count the number of times. (e.g. 123456 / 10000 = 12.3456 -> 12)
*     and calculate the remainder by 10. so I get the one number (e.g. 12 % 10 = 2)
*    Last add '0' to convert number to ASCII code (e.g. 2 -> '2')
*
*
*    Made by Bak JinYeong, 2008.03.13
*    Edited by Bak JinYeong, 2008.03.15*/
void Integer_to_Ascii(unsigned int input_int, char* output_str)
{
    int for_loop_i = 0; // integer variable use loop for loop sentence
    int number_size = 0; // number size. Calculate cipher from it.
    unsigned int copy_input_int = input_int; // When get number_size, input_int is changed. So I backup the input_int.

    while(copy_input_int > 0) // copy_input_int is greater than 0
    {
        number_size++; // number_size = number_size + 1
        copy_input_int /= 10; // copy_input_int = copyt_input_int / 10.
    }

    for(for_loop_i = 0 ; for_loop_i < number_size ; for_loop_i++) // looping number's cipher times.
    {
        copy_input_int = input_int / (unsigned int) integer_power(10, number_size - for_loop_i - 1); // get number.
        copy_input_int %= 10;
        output_str[for_loop_i] = copy_input_int + '0'; // '1' is 1 + '0' in ASCII. convert number to ascii code
    }
    output_str[for_loop_i] = '\0'; // Least significant byte of string must have \0.
}

/*
*     integer_power
*
*     This function calculate power.
*
*     - Argument
*     base_number : base number (e.g. 2^3's base number is 2)
*     exponent_number : exponent number (e.g. 2^3's exponent number is 3)
*
*     - return
*     unsigned int : result (e.g. base 2, exponent 3 -> return 8)
*
*
*     Made by Bak JinYeong, 2008.03.13*/
unsigned int integer_power(int base_number, int exponent_number)
{
    int for_loop_i = 0; // integer variable use loop for loop sentence
    unsigned int result_value = 1; // result value. It initialize 1, because 1 is identity element of multiplication.
    for(for_loop_i = 0 ; for_loop_i < exponent_number ; for_loop_i++) // looping exponent times
    {
        result_value *= base_number; // multiply base_number
    }
    return result_value; // return
}

크리에이티브 커먼즈 라이선스
Creative Commons License

글에 잘못된 점, 다른 점, 부족한 점이 있다면 지적해주세요.
댓글, 트랙백, 메일 모두 고맙습니다.

트랙백 주소 :: http://nosyu.pe.kr/trackback/1455

댓글을 달아 주세요

[로그인][오픈아이디란?]