-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.c
45 lines (42 loc) · 1.19 KB
/
answer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <string.h>
int romanToInt(char* s) {
int i;
int result = 0;
// Loop through characters
for (i = 0; i < strlen(s); i++){
// Switch statements for the basic numeral values
// Multiples of 10 are special as they can be subtracted
switch (s[i]){
case 'M':
result += 1000;
break;
case 'D':
result += 500;
break;
case 'C':
if (s[i+1] == 'D' || s[i+1] == 'M') result -= 100;
else result += 100;
break;
case 'L':
result += 50;
break;
case 'X':
if (s[i+1] == 'L' || s[i+1] == 'C') result -= 10;
else result += 10;
break;
case 'V':
result += 5;
break;
case 'I':
if (s[i+1] == 'V' || s[i+1] == 'X') result--;
else result++;
break;
}
}
return result;
}
//------------------------------------------------------------------------------
int main(){
printf("%d\n", romanToInt("XXX"));
}