-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathromanToInteger.c
More file actions
71 lines (69 loc) · 1.55 KB
/
Copy pathromanToInteger.c
File metadata and controls
71 lines (69 loc) · 1.55 KB
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
#include <stdio.h>
#include <string.h>
int romanToInt(char * s){
int answer = 0;
int len = strlen(s);
int i = 0;
while (i < len) {
if(s[i] == 'I') {
if (s[i + 1] == 'V') {
answer += 4;
i += 2;
} else if (s[i+1] == 'X') {
answer += 9;
i += 2;
} else {
answer += 1;
i += 1;
}
}
else if (s[i] == 'X') {
if (s[i+1] == 'L') {
answer += 40;
i += 2;
} else if (s[i+1] == 'C') {
answer += 90;
i += 2;
} else {
answer += 10;
i += 1;
}
}
else if (s[i] == 'C') {
if (s[i+1] == 'D') {
answer += 400;
i += 2;
} else if (s[i+1] == 'M') {
answer += 900;
i += 2;
} else {
answer += 100;
i += 1;
}
}
else if (s[i] == 'V') {
answer += 5;
i++;
}
else if (s[i] == 'L') {
answer += 50;
i++;
}
else if (s[i] == 'D') {
answer += 500;
i++;
} else {
answer += 1000;
i++;
}
}
return answer;
}
int main() {
char s[15];
printf("Insert Roman numeral:\n");
scanf("%s", s);
printf("\nRoman: %s\n", s);
int i = romanToInt(s);
printf("Integer: %i\n", i);
}