-
Notifications
You must be signed in to change notification settings - Fork 0
/
13. Roman to Integer.c
46 lines (44 loc) · 966 Bytes
/
13. Roman to Integer.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
int toint(char c){
int ans=0;
switch (c){
case 'I':
ans=1;
break;
case 'V':
ans=5;
break;
case 'X':
ans=10;
break;
case 'L':
ans=50;
break;
case 'C':
ans=100;
break;
case 'D':
ans=500;
break;
case 'M':
ans=1000;
break;
}
return ans;
}
int romanToInt(char* s) {
int sum=0;
while (*s!='\0'){
if ((toint(*s)<toint(*(s+1))) && (*(s+1)!='\0')){
sum+=toint(*(s+1))-toint(*s);
s+=2;
}else if ((toint(*s)>=toint(*(s+1))) && (*(s+1)!='\0')){
sum+=toint(*s);
s++;
}else{
sum+=toint(*s);
s++;
}
//s+=2;
}
return sum;
}