-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE13_Roman2Int.c
83 lines (75 loc) · 1.71 KB
/
E13_Roman2Int.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<stdio.h>
#include<stdlib.h>
#define M 1000
#define D 500
#define C 100
#define L 50
#define X 10
#define V 5
int romanToInt(char* s) {
int res = 0;
while( *s != '\0') {
switch(*s) {
case 'I':
if( *(s+1) == 'V') {
res += 4;
++s;
} else if ( *(s+1) == 'X') {
res += 9;
++s;
} else {
res +=1;
}
break;
case 'V':
res += 5;
break;
case 'X':
if( *(s+1) == 'L' ) {
res +=40;
++s;
} else if( *(s+1) == 'C' ){
res += 90;
++s;
}
else {
res += 10;
}
break;
case 'L':
res += 50;
break;
case 'C':
if( *(s+1) == 'D') {
res += 400;
++s;
break;
} else if( *(s+1) == 'M') {
res += 900;
++s;
} else {
res += 100;
}
break;
case 'D':
res += 500;
break;
case 'M':
res += 1000;
break;
default:
break;
}
++s;
}
return res;
}
int main()
{
char input[100];
int res = 0;
fgets(input, sizeof(input), stdin);
res = romanToInt(input);
printf("%s = %d\n", input, res);
return 0;
}