#include <assert.h>
/*
I             1
V             5
X             10
L             50
C             100
D             500
M             1000
*/
#define S2

#ifdef S1
int romanToInt(char *s)
{
    int ret = 0;

    while (*s != '\0')
    {
        char next = *(s + 1);
        switch (*s)
        {
        case 'I':
            if (next == 'V')
            {
                ret += 4;
                s++;
            }
            else if (next == 'X')
            {
                ret += 9;
                s++;
            }
            else
                ret += 1;
            break;
        case 'V':
            ret += 5;
            break;
        case 'X':
            if (next == 'L')
            {
                ret += 40;
                s++;
            }
            else if (next == 'C')
            {
                ret += 90;
                s++;
            }
            else
                ret += 10;
            break;
        case 'L':
            ret += 50;
            break;
        case 'C':
            if (next == 'D')
            {
                ret += 400;
                s++;
            }
            else if (next == 'M')
            {
                ret += 900;
                s++;
            }
            else
                ret += 100;
            break;
        case 'D':
            ret = 500;
            break;
        case 'M':
            ret += 1000;
            break;
        default:
            break;
        }
        s++;
    }

    return ret;
}
#endif

#ifdef S2

int getValue(const 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;
    default:
        return 0;
    }
}

int romanToInt(char *s)
{
    int ret = 0;
    int preNum = getValue(*s);
    for (int i = 1; s[i] != '\0'; i++)
    {
        int num = getValue(s[i]);
        if (preNum < num)
            ret -= preNum;
        else
            ret += preNum;
        preNum = num;
    }
    ret += preNum;
    return ret;
}

#endif

int main()
{
    char r1[] = "III";
    char r2[] = "IV";
    char r3[] = "IX";
    char r4[] = "LVIII";
    char r5[] = "MCMXCIV";

    assert(romanToInt(r1) == 3);
    assert(romanToInt(r2) == 4);
    assert(romanToInt(r3) == 9);
    assert(romanToInt(r4) == 58);
    assert(romanToInt(r5) == 1994);

    return 0;
}
