Do You Coding?
[library] itoa ๊ตฌํํ๊ธฐ ๋ณธ๋ฌธ

๐ ๋งค๋ด์ผ (in subject)
Function name
do_itoa
Prototype
char *do_itoa(int n);
Parameters
n: the integer to convert
: n: ๋ณํํ ์ ์
Return value
The string representing the integer. NULL if the allocation fails.
: ์ ์๋ฅผ ๋ํ๋ด๋ ๋ฌธ์์ด. ํ ๋น์ ์คํจํ๋ฉด NULL
External functs
malloc
Description
Allocates (with malloc(3)) and returns a string representing the integer received as an argument. Negative numbers must be handled.
: malloc์ ์ฌ์ฉํ์ฌ ํ ๋นํ๊ณ ์ธ์๋ก ๋ฐ์ ์ ์๋ฅผ ๋ํ๋ด๋ ๋ฌธ์์ด์ ๋ฐํ. ์์ ์ฒ๋ฆฌํด์ผ ํจ.
๐ ์์ฑ ์ฝ๋
static void do_negative(int *n, char **str)
{
if ((*n) < 0 && *n != -2147483648)
{
(*str)[0] = '-';
(*n) = (*n) * (-1);
}
if ((*n) == -2147483648)
{
(*str)[0] = '-';
(*str)[1] = '2';
(*n) = 147483648;
}
}
static int do_digit(int n, int *minus)
{
int d;
d = 1;
if (n == -2147483648)
{
*minus = 2;
d = 9;
return (d);
}
if (n < 0)
{
n = n * (-1);
*minus = 1;
}
else
*minus = 0;
while (n >= 10)
{
n = n / 10;
d ++;
}
return (d);
}
char *do_itoa(int n)
{
char *str;
int minus;
int i;
int digit;
int nb;
i = 1;
digit = do_digit(n, &minus);
str = (char *)malloc(sizeof(char) * (digit + minus + 1));
if (str == NULL)
return (NULL);
do_negative(&n, &str);
while (i <= digit)
{
nb = n % 10 + 48;
n = n / 10;
str[digit + minus - i] = nb;
i ++;
}
str [digit + minus] = '\0';
return (str);
}
๐ ์ฝ๋ ๋ฆฌ๋ทฐ
itoa ํจ์๋ int to alphabet ์ผ๋ก, ์ ์๋ก ๋ฐ์ ์ธ์๋ฅผ ๋ฌธ์์ด ํํ๋ก ๋ณํํ์ฌ ๋ฐํํด์ฃผ๋ ํจ์์ด๋ค.
์์๋ก 12345๋ผ๋ ์ ์๋ฅผ ๋ฐ์ผ๋ฉด, "12345"์ ํํ๋ฅผ ๊ฐ์ง๋ ๋ฌธ์์ด์ ๋ฐํ๋ฐ๋ ๊ฒ์ด๋ค.
> ์์ธ์ ์ผ๋ก ๊ณ ๋ คํด์ผํ ์ฌํญ
1. ์ต์๊ฐ์ธ -2147483648 ์ ์์ธ ( * (-1) ์ ํ๋ฉด overflow ๋๋ฏ๋ก ์ฒ๋ฆฌ ํ์)
2. ์์ ์ฒ๋ฆฌ (- ๋ถํธ ๋ถ์ด๊ธฐ)
์ผ๋จ, ์๋ฆฟ์๊ฐ ํ์ํ๋ค.
do_digit ํจ์๊ฐ ์๋ฆฟ์๋ฅผ ๊ตฌํ๋ ์ญํ ์ ํ๊ฒ ๋๋๋ฐ, ์ผ๋จ ์์์ ์ธ๊ธํ ์์ธ์ ์ธ ์ต์๊ฐ์ ๋จผ์ ์ฒ๋ฆฌํ๋ค.
minus๋ผ๋ ๋ณ์๋ฅผ ์ฌ์ฉํ๋๋ฐ, ์ด๋ ์์์ '-' ๋ถํธ๋ฅผ ๋ถํ ์๋ฆฌ๋ฅผ ๋ง๋ จํ๊ธฐ ์ํ ์ญํ ์ผ๋ก,
0 ์ด๋ฉด ์์ ์ ์ / 1์ด๋ฉด ์ผ๋ฐ ์์ ์ ์ / 2์ด๋ฉด ์ต์๊ฐ์ด ๋๋ค.
์ต์๊ฐ์ minus ๋ณ์๊ฐ์ด 2๊ฐ ๋๋ ๊ฒ์ -2147483648 ์์, ์์ '-2' ๊ณต๊ฐ์ ๊ฐ๊ฐ ๋์ ํ๊ณ ,
๋๋จธ์ง '147483648'์ ๋ค๋ฅธ ์ ์์ฒ๋ผ ์ฒ๋ฆฌํ๊ธฐ ์ํด -์ 2์ ๊ณต๊ฐ์ ๋ง๋ จํ๋ ๊ฒ์ผ๋ก ํ์ฉํ๋ค.
๊ทธ๋ฆฌํ์ฌ ์ต์๊ฐ์ ์๋ฆฟ์ d : 9 , minus : 2 ์ธ ์ํ๋ก return ๋ ๊ฒ์ด๊ณ ,
๊ทธ ์ธ์, ์์์ด๋ฉด -1์ ๊ณฑํด ์์ ์ ์๋ก ๋ง๋ค๊ณ , minus : 1, ์์์ด๋ฉด minus : 0 ์ผ๋ก ๋ง๋ค๊ณ ๋์ด๊ฐ๋ค.
๊ทธ ํ์ 10์ฉ ๋๋ ์ ์๋ฆฟ์๋ฅผ ๊ณ์ฐํ์ฌ return ํ๊ณ , ๋ค์ do_itoa ํจ์๋ก ๋์์จ๋ค.
๊ทธ๋ฆฌํ์ฌ, ๊ตฌํ digit + minus ์ '\0' ์๋ฆฌ๋ฅผ ํ๋ ๋ํด ๋์ ํ ๋นํด์ค๋ค.
๋ค์์ do_negative ํจ์๊ฐ ์คํ๋๋ค.
์ด ํจ์๋ ์์๋ ๋ฌธ์์ด์ 0๋ฒ์งธ๋ฅผ '-'๋ฅผ ๋ฃ์ด์ฃผ๊ณ , ์์๋ฅผ ์์๋ก ๋ฐ๊พธ๋ ๊ณผ์ ์ ์ทจํ๊ณ ,
์ต์๊ฐ์ ๋ฌธ์์ด 0๋ฒ์งธ์ '-', 1๋ฒ์งธ์ '2'๋ฅผ ๋ฃ์ ๋ค, ๋จ์ ์ซ์๋ฅผ '1473648'์ธ ์ฒ๋ฆฌ๊ฐ๋ฅํ ์ซ์๋ก ๋ง๋ค์ด์ค๋ค.
์ดํ๋ ๋ฐ๋ณต๋ฌธ์ด๋ค.
10์ผ๋ก ๋๋ ๋๋จธ์ง์ 48(= '0') ์ ๋ํ ๊ฐ์ nb์ ๋ฃ์ด์ฃผ๋๋ฐ, ์ด๋ ๊ฐ์ฅ ๋ฎ์ ์๋ฆฟ์์ ์ซ์๋ฅผ ๋ฌธ์ํํ ๊ฒ์ด๋ค.
๋จ์ ์ซ์๋ค์ ์ฒ๋ฆฌํ๊ธฐ์ํด n์ 10์ผ๋ก ๋๋ ์ n์ ๋ค์ ์ ์ฉ์์ผ์ฃผ๊ณ ,
str[digit + minus - i] ๋ถํฐ ์ฐจ๋ก๋๋ก ๋ค์์๋ถํฐ nb๋ก ์ฑ์ด๋ค.
๊ทธ๋ผ ์ฐ๋ฆฌ๊ฐ ์ํ๋ ์ ์๋ฅผ ๋ฌธ์์ด๋ก ๋ฐ๊พผ str์ด ๋ง๋ค์ด์ง๋ค.
+) ์ต์๊ฐ ์ฒ๋ฆฌ๊ฐ ํฌ๋ฐํ๊ฒ ๋ง๋ค์ด์ง ์ ์ด ์กฐ๊ธ ์์ฌ์ด๋ฐ, ๋ค๋ฅธ ๋ฐฉ๋ฒ์ด ์๋์ง ๊ณ ๋ฏผํด๋ณผ ํ์๊ฐ ์๋ค.
'CS & Engineering > C' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [function] pthread_create ํจ์ ์์๋ณด๊ธฐ (5) | 2024.09.07 |
|---|---|
| [utility] get_next_line ๊ตฌํํ๊ธฐ (3) | 2024.08.31 |
| [library] split ๊ตฌํํ๊ธฐ (0) | 2024.06.29 |
| [library] strtrim ๊ตฌํํ๊ธฐ (0) | 2024.06.27 |
| [library] strjoin ๊ตฌํํ๊ธฐ (0) | 2024.06.26 |