문자열 변환 알고리즘?

nayana의 이미지

Quote:
☞ 입력
“이백원, 육백원, 삼천사백원, 오백칠십원, -구백사십원”

☞ 출력
“삼천팔백삼십원”

☞ 처리조건
* 문자열로 된 값을 입력받아, 모두 더한 값을 문자열로 출력한다.
* 마이너스 값이 올 수 있으며, 기호는 ‘-’ 문자를 사용한다.
* 최대 단위는 ‘억’ 단위이며, 그 이상은 에러처리를 한다.

제 나름대로 코딩을 해보았는데.

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <cstring>
#include <cstdlib>

int ToCurr( char *cStrPtr )
{
    char *a[ 9 ] = { "일", "이", "삼", "사", "오", "육", "칠", "팔", "구" };
    char *b[ 4 ] = { "십", "백", "천", "만" };

    int c[ 4 ] = { 10, 100, 1000, 10000 };
    int i , j, l, temp = 0, su = 0;

    // 문자열의 길이
    l = strlen( cStrPtr );

    // 변환 시작
    for( i = 0; i < l; i += 2 )
    {
        for( j = 0; j < 9; j++ )
        {
            if( strncmp( cStrPtr + i, a[ j ], 2 ) == 0 )    temp += j + 2;
        }

        for( j = 0; j < 4; j++ )
        {
            // 십,백,천 자리의 경우
            if( strncmp( cStrPtr + i, b[ j ], 2 ) == 0 && b[ j ]!= "만" )
            {
                temp *= c[ j ];
                su += temp;
                temp = 0;
            }
            // 만자리의 경우
            else if( strncmp( cStrPtr + i, b[ j ], 2 ) == 0 && b[ j] == "만" )
            {

                temp *= c[ j ];
                su   *= c[ j ];
                su   += temp;
                temp  = 0;
            }
        }
    }
    // 단자리 수를 위한 마지막 처리
    if( temp != 0 )  su += temp;

    return su;
}

int main( void )
{
    char cInStr[ 100 ] = "";
    int  iStrNumber;

    cout << "[문제2]문자열 숫자를 입력받아 합계하는 프로그램을 만드시오.\n";
    cout << "문자열 숫자 입력==>";
    cin.getline( cInStr, 100 );

    cout << "원래의 문자열= " << cInStr << endl;

    iStrNumber = ToCurr( cInStr );

    cout << "변환된 숫자값= " << iStrNumber << endl;

    return 0;
}

"마이너스" 구현 문제와 문자열로 변환시키는것이 생각보다 까다롭습니다.
같이 고민해 볼려고 이렇게 질문을 드립니다.
고수님들 조언 부탁드리겠습니다.

eminency의 이미지

'십, 백, 천'과 '만, 억'은 따로 배열로 분리하셔야 할 것 같군요. 우리나라 숫자 읽는 법은 만단위마다 십, 백, 천이 붙으니까요.

노루가 사냥꾼의 손에서 벗어나는 것 같이, 새가 그물치는 자의 손에서 벗어나는 것 같이 스스로 구원하라 -잠언 6:5

nayana의 이미지

char *b[ 4 ] = { "십", "백", "천", "만" };

Quote:
'십, 백, 천'과 '만, 억'은 따로 배열로 분리하셔야 할 것 같군요.
분류가 되어있습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.