2017-09-16 1 views
1

로마 숫자를 C++의 소수로 변환하려고합니다.로마 숫자를 십진수로 변환 C++

그래서 내 코드는 로마자를 십진수로 변환해야하지만 완전히 작동하지는 않습니다. 예를 들어

는, VI는 4이며, IV는 6
되고 MCMXLVI 1946을 양보해야하지만, 나는 오른쪽에서 왼쪽으로 가면 나는 왼쪽에서 오른쪽으로 가면 나는 -998을 얻고, 내가 공을 받고 있어요.

저는 주로 내 사고 과정이 올바른지 알고 싶습니다.

의사 :

total = 0 
    max_value_so_far = 0 
    for each character in the input string, going from right to left: 
     if character converted to decimal >= max_value_so_far 
      add character converted to decimal to total 
      update max_value_so_far 
     otherwise subtract character converted to decimal from total 

코드 : 당신의 if 테스트 각각에 대해

#include "std_lib_facilities_5.h" 

string convert_string(string input){ 

    for(int i=0; i < input.length(); i++){ 
     input[i] = toupper(input[i]); 
    } 

    return input; 
} 

int roman_to_int(string RomanChars){ 

    RomanChars = convert_string(RomanChars); 

    int total = 0; 
    int max_value= 0; 
    int M,D,C,L,X,V,I; 
    M = 1000; 
    D = 500; 
    C = 100; 
    L = 50; 
    X = 10; 
    V = 5; 
    I = 1; 

    double StringLength =RomanChars.length(); 

    for(int i = 0; i < StringLength; i++){ 

     if(RomanChars[i] == 'M') { 
      if (M >= max_value) { 
      total += M; 
      max_value = M; 
      } else { 
      total -= M; 
      } 
     } 
     if(RomanChars[i] == 'D') { 
      if (D >= max_value) { 
       total += D; 
       max_value = D; 
      } else { 
       total -= D; 
      } 
     } 
     if(RomanChars[i] == 'C') { 
      if (C >= max_value) { 
       total += C; 
       max_value = C; 
      } else { 
       total -= C; 
      } 
     } 
     if(RomanChars[i] == 'L') { 
      if (L >= max_value) { 
       total += L; 
       max_value = L; 
      } else { 
       total -= L; 
      } 
     } 
     if(RomanChars[i] == 'X') { 
      if (X >= max_value) { 
       total += X; 
       max_value = X; 
      } else { 
       total -= X; 
      } 
     } 
     if(RomanChars[i] == 'V') { 
      if (V >= max_value) { 
       total += V; 
       max_value = V; 
      } else { 
       total -= V; 
      } 
     } 
     if(RomanChars[i] == 'I') { 
      if (I >= max_value) { 
       total += I; 
       max_value = I; 
      } else { 
       total -= I; 
      } 
     } 
    } 
    return total; 
} 

int main() { 

    string character; 
    int conversion = 0; 

    while(cin >> character){ 

     conversion = roman_to_int(character); 
     cout << conversion <<endl; 
    } 
    return 0; 
} 
+0

* 어떻게 작동하지 않습니까? 빌드 오류가 발생합니까? 충돌? 예기치 않은 결과가 있습니까? 제발 좀 더 자세히 설명해주세요. 그리고 [좋은 질문을하는 법을 읽어보십시오.] (http://stackoverflow.com/help/how-to-ask). 또한 Eric Lippert의 [작은 프로그램 디버깅 방법] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)을 읽고 디버거를 사용하여 단계별 실행 방법을 배우십시오. 당신의 코드. –

+0

그게 내 부정적인 대답을주는 –

+0

나는 내 생각 프로세스가 맞는지 알고 싶다. –

답변

1

, 당신 때문에 당신이 그것을 수행 생각할 때 실행되지 않는 당신의 else 블록에 오류가 발생합니다.

 if(RomanChars[i] == 'M' && M >= max_value){ 
      total += M; 
      max_value = M; 
     } else { 
      total -= M; // this got executed for each character 
         // in the input sequence which is *not* M! 
     } 

 if (RomanChars[i] == 'M') { 
      if (M >= max_value) { 
       total += M; 
       max_value = M; 
      } else { 
       total -= M; 
      } 
     } 

그리고 더 나은 아직이어야합니다 더 나은 아직

switch (RomanChars[i]) 
{ 
case 'M': 
    if (M >= max_value) { 
     max_value = M; 
     total += M; 
    } else { 
     total -= M; 
    } 
    break; 
// ... 

그리고 당신의 교수에 의해 제공되는

#include<map> 
const std::map<char, unsigned> romans_definition = { 
    { 'I', 1 }, 
    { 'V', 5 }, 
    { 'X', 10 }, 
    { 'L', 50 }, 
    // ... 
}; 
2

알고리즘과 코드를 리팩토링이다 권리.

몇 가지 힌트 :

for(double i = 0; i < RomanChars.length(); i++){ 
  • 한 루프에서 왼쪽에서 오른쪽으로. 잘못된 방향.
  • double? 루핑 변수로? 그러지 마. (문제의 원인은 아니지만 바드)

다른 답변은 이미 다른 사람이 잘못되었다는 것을 지적했습니다. 내가 너라면이 사본을 대체하고 난교를 붙여 우아한 것을 찾는 것에 대해 생각할 것이다.

영감을 얻으려면 std :: map에 로마 숫자 리터럴 값을 저장하면 변환 루프가 다음과 비슷할 것입니다 (예제의 std :: map은 std::map<char, int> conversion입니다) :

int roman_to_int(std::string RomanChars){ 

    RomanChars = convert_string(RomanChars); 

    int total = 0; 
    int max_value= 0; 

    for(size_t i = RomanChars.length()-1; i != std::string::npos; --i){ 

     auto val = conversion.find(RomanChars[i]); 
     if(val != conversion.end()) 
     { 
      if(val->second >= max_value) 
      { 
       total += val->second; 
       max_value = val->second; 
      } else { 
       total -= val->second; 
      } 
     } 
    } 
    return total; 
} 
+0

나는 c/p 난교에 관하여 당신의 충고에 동의한다;) 내 upvote를 가지고있다 – YSC

+0

왜냐하면 내게 암묵적인 변환 손실을 말하기 때문에 오류가 발생하기 때문이다. –

+0

@PapeSowTraore는'std :: size_t'를 사용한다. – YSC

관련 문제