serie10/main.cpp
int roman2int(string s) {
    int total = 0;
    const char* letters = s.c_str();
    int len = strlen(letters);
    for (int i = 0; i < len; i++) {
        if (s[i] == 'I') {
            if (i < len && letters[i + 1] == 'V') {
                total += 4;
                ++i;
            }
            else if (i < len && letters[i + 1] == 'X') {
                total += 9;
                ++i;
            }
            else {
                total += 1;
            }
        }
        else if (s[i] == 'V') {
            total += 5;
        }
        else if (s[i] == 'X') {
            if (i < len && letters[i + 1] == 'L') {
                total += 40;
                ++i;
            }
            else if (i < len && letters[i + 1] == 'C') {
                total += 90;
                ++i;
            }
            else {
                total += 10;
            }
        }
        else if (s[i] == 'L') {
            total += 50;
        }
        else if (s[i] == 'C') {
            if (i < len && letters[i + 1] == 'D') {
                total += 400;
                ++i;
            }
            else if (i < len && letters[i + 1] == 'M') {
                total += 900;
                ++i;
            }
            else {
                total += 100;
            }
        }
        else if (s[i] == 'D') {
            total += 500;
        }
        else if (s[i] == 'M') {
            total += 1000;
        }
    }

    return total;
}
/*
    Converts a roman number to its decimal representation.
*/
Converts a roman number to its decimal representation.
Converts a roman number to its decimal representation .
int roman2int(string s) {
    int total = 0;
    const char* letters = s.c_str();
    int len = strlen(letters);
    for (int i = 0; i < len; i++) {
        if (s[i] == 'I') {
            if (i < len && letters[i + 1] == 'V') {
                total += 4;
                ++i;
            }
            else if (i < len && letters[i + 1] == 'X') {
                total += 9;
                ++i;
            }
            else {
                total += 1;
            }
        }
        else if (s[i] == 'V') {
            total += 5;
        }
        else if (s[i] == 'X') {
            if (i < len && letters[i + 1] == 'L') {
                total += 40;
                ++i;
            }
            else if (i < len && letters[i + 1] == 'C') {
                total += 90;
                ++i;
            }
            else {
                total += 10;
            }
        }
        else if (s[i] == 'L') {
            total += 50;
        }
        else if (s[i] == 'C') {
            if (i < len && letters[i + 1] == 'D') {
                total += 400;
                ++i;
            }
            else if (i < len && letters[i + 1] == 'M') {
                total += 900;
                ++i;
            }
            else {
                total += 100;
            }
        }
        else if (s[i] == 'D') {
            total += 500;
        }
        else if (s[i] == 'M') {
            total += 1000;
        }
    }
    return total;
}
int roman2int ( string s ) { int total = 0 ; const char * letters = s . c_str ( ) ; int len = strlen ( letters ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( s [ i ] == ' ' ) { if ( i < len && letters [ i + 1 ] == ' ' ) { total += 4 ; ++ i ; } else if ( i < len && letters [ i + 1 ] == ' ' ) { total += 9 ; ++ i ; } else { total += 1 ; } } else if ( s [ i ] == ' ' ) { total += 5 ; } else if ( s [ i ] == ' ' ) { if ( i < len && letters [ i + 1 ] == ' ' ) { total += 40 ; ++ i ; } else if ( i < len && letters [ i + 1 ] == ' ' ) { total += 90 ; ++ i ; } else { total += 10 ; } } else if ( s [ i ] == ' ' ) { total += 50 ; } else if ( s [ i ] == ' ' ) { if ( i < len && letters [ i + 1 ] == ' ' ) { total += 400 ; ++ i ; } else if ( i < len && letters [ i + 1 ] == ' ' ) { total += 900 ; ++ i ; } else { total += 100 ; } } else if ( s [ i ] == ' ' ) { total += 500 ; } else if ( s [ i ] == ' ' ) { total += 1000 ; } } return total ; }
Converts a roman number to its decimal representation.
Converts a roman number to its decimal representation .