forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
13.c
58 lines (58 loc) · 1.3 KB
/
13.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
46
47
48
49
50
51
52
53
54
55
56
57
58
int romanToInt(char *s)
{
int romanToInt = 0;
for (int i = 0; i < strlen(s); i++)
{
switch (s[i])
{
case 'I':
if (i + 1 < strlen(s))
{
if (s[i + 1] == 'V' || s[i + 1] == 'X')
{
romanToInt -= 1;
break;
}
}
romanToInt += 1;
break;
case 'V':
romanToInt += 5;
break;
case 'X':
if (i + 1 < strlen(s))
{
if (s[i + 1] == 'L' || s[i + 1] == 'C')
{
romanToInt -= 10;
break;
}
}
romanToInt += 10;
break;
case 'L':
romanToInt += 50;
break;
case 'C':
if (i + 1 < strlen(s))
{
if (s[i + 1] == 'D' || s[i + 1] == 'M')
{
romanToInt -= 100;
break;
}
}
romanToInt += 100;
break;
case 'D':
romanToInt += 500;
break;
case 'M':
romanToInt += 1000;
break;
default:
break;
}
}
return romanToInt;
}