숫자에 3자리마다 , 찍기

데브피아에 이 주제로 글이 올라왔다..
답글은 별로 없었지만..
근데 갑자기 duff's device가 떠올랐다
(http://zsoo.net/prg/cpp/duffs_device.html)
그래서 그냥 간단하게 만들었다..

생각해본 다음에 소스를 열어보세요..


#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
const char* str = "1000000000000";
size_t len = strlen(str);
char* nstr = new char[(len * 4 / 3) + 1];
char* new_str = nstr;

switch (len % 3)
{
while (len >= 3)
{
*new_str++ = ',';
case 0:
*new_str++ = *str++;
len--;
case 2:
*new_str++ = *str++;
len--;
case 1:
*new_str++ = *str++;
len--;
}
}
*new_str = 0;

cout << nstr << endl;
}

버그로 인해 소스를 수정했는데
duff's device랑 조금 달라진것 같다..

by kiho | 2005/08/08 02:19 | 프로그래밍 팁 | 트랙백 | 덧글(2)

트랙백 주소 : http://kiho.egloos.com/tb/450573
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]
Commented by kiho at 2005/10/07 10:45
버그가 있다 -_-;
Commented by kiho at 2005/10/07 23:22
버그 수정함

:         :

:

비공개 덧글

◀ 이전 페이지          다음 페이지 ▶