Home > Enterprise >  I need to find the integer value for the given roman number. I can't get the solution
I need to find the integer value for the given roman number. I can't get the solution

Time:09-18

I used my logic, I am confident my logic was correct,but for some reasons my code doesn't work. I need help to point out what mistake I made, also sorry for declaring my variables as random alphabets without any semantic meaning.

#include<iostream>
using namespace std;
int main()
{
        char s[15];
        cin >> s;
        char a[7] = {'I','V','X','L', 'C', 'D', 'M'};
        int b[7] = {1,5,10,50,100,500,1000};
        int count = 0,i=0,j,k;
        while(s[i]!='\0')
        {
            for(j=0;j<7;j  )
            {
                int l=0;
                if(s[i]==a[j])
                {
                    k = j;
                    if(s[i 1] == a[j])
                    {
                    l = j;
                    }
                }
                if(k<l)
                {
                    count = count   (b[l]-b[k]);
                    i =2;
                }
                else
                {
                    count = count   b[k];
                     i  ;
                }
            }
           
        }
        cout << count;
    }

CodePudding user response:

The solution:

int romanCharToInt(char c) {
    switch(c) {
        case 'I':
            return 1;
        case 'V':
            return 5;
        case 'X':
            return 10;
        case 'L':
            return 50;
        case 'C':
            return 100;
        case 'D':
            return 500;
        case 'M':
            return 1000;
    }
    return 0;
}

int romanToInt(string s) {
    int result = 0;
    
    int stored_sum = 0;
    int last_val = -1;
    
    for (char elem : s) {
        int val = romanCharToInt(elem);
        if (last_val == -1) {
            stored_sum  = val;
        } else if (last_val > val) {
            result  = stored_sum;
            stored_sum = val;
        } else if (last_val == val) {
            stored_sum  = val;
        } else {
            stored_sum = val - stored_sum;
        }
        last_val = val;
    }
    result  = stored_sum;
    stored_sum = 0;
    
    return result;
}

CodePudding user response:

This looks like a homework assignment, and while I think you should go back to the drawing board in order to figure this out, maybe a hint would suffice. When we analyze a roman integer, it has it's most significant 'digits'(letters) closer to the start of it. The only exception is when the 'digit' directly to the right of it is of higher order. You may find it easier to solve the problem from the least significant digits forward. Given this scenario, the procedure I'd follow is this:

Number : MCMXCIV
Current Roman Digit | Current Answer int
                  V | 5 (no matter what the first character is, it will always be the base)
                  I | 5 - 1 (I is smaller than V, therefore we substract it)
                  C | 5 - 1   100 
                  X | 5 - 1   100 - 10 (X is smaller than C, we substract)
                  M | 5 - 1   100 - 10   1000
                  C | 5 - 1   100 - 10   1000 - 100
                  M | 5 - 1   100 - 10   1000 - 100   1000 = 1994
  •  Tags:  
  • c
  • Related