Why my converter doesn't count the last digit?

39 views Asked by At

This program is about converting Roman number to decimal number. The program can convert the alphabet to number but it can not process the last roman digit. I think my flow is alright but the output is not right. Can any body give me a helping hand?

#include <stdint.h>
#include <stdio.h>
#include <string.h>

int roman_to_int(const char s[], int length) {
    // Please complete the function body
    
    int ans = 0, value[length];
    
    for (int i = 0; i < length; i++) {
        
        switch (s[i]) {
        
            case 'I': value[i] = 1; break;
            case 'V': value[i] = 5; break;
            case 'X': value[i] = 10; break;
            case 'L': value[i] = 50; break;
            case 'C': value[i] = 100; break;
            case 'D': value[i] = 500; break;
            case 'M': value[i] = 1000; break;
        }
    }
    
    for (int i = 0; i < length - 1; i++) {
     
        if (value[i] >= value[i+1])
            ans += value[i];
        else {
            ans = ans + value[i+1] - value[i];
            i++;
        }
    }
    return ans;
}

int main() {
    char roman_num[] = "III";
    char roman_num_2[] = "CXXIII";
    char roman_num_3[] = "MMMCDLIX";

    printf("roman_to_int(%s) = %d\n", roman_num,
           roman_to_int(roman_num, strlen(roman_num)));
    printf("roman_to_int(%s) = %d\n", roman_num_2,
           roman_to_int(roman_num_2, strlen(roman_num_2)));
    printf("roman_to_int(%s) = %d\n", roman_num_3,
           roman_to_int(roman_num_3, strlen(roman_num_3)));
}
1

There are 1 answers

0
chqrlie On

You should add the value of the last roman digit after the end of the second loop.

As an alternative, you could make value on entry longer than n and set the last entry to 0 so you won't need the make a special case of the last roman digit.

Note that you should also handle the case of unrecognised roman digits: either by ignoring them or by returning an error code, such as a negative value -1.

It is also simpler for roman_to_int to take a null terminated C string and compute the length there.

Here is a modified version:

#include <stdio.h>
#include <string.h>

int roman_to_int(const char s[]) {
    // Please complete the function body
    int length = strlen(s);
    int ans = 0, value[length + 1];
    
    for (int i = 0; i < length; i++) {
        switch (s[i]) {
            case 'I': value[i] = 1; break;
            case 'V': value[i] = 5; break;
            case 'X': value[i] = 10; break;
            case 'L': value[i] = 50; break;
            case 'C': value[i] = 100; break;
            case 'D': value[i] = 500; break;
            case 'M': value[i] = 1000; break;
            default: return -1;
        }
    }
    value[length] = 0;
    
    for (int i = 0; i < length; i++) {
        if (value[i] >= value[i + 1])
            ans += value[i];
        else
            ans -= value[i];
    }
    return ans;
}

int main() {
    char roman_num[] = "III";
    char roman_num_2[] = "CXXIII";
    char roman_num_3[] = "MMMCDLIX";
    char roman_num_4[] = "MMMCDLIZ";  // error

    printf("roman_to_int(%s) = %d\n", roman_num, roman_to_int(roman_num));
    printf("roman_to_int(%s) = %d\n", roman_num_2, roman_to_int(roman_num_2));
    printf("roman_to_int(%s) = %d\n", roman_num_3, roman_to_int(roman_num_3));
    printf("roman_to_int(%s) = %d\n", roman_num_4, roman_to_int(roman_num_4));
    return 0;
}