-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman.cpp
75 lines (70 loc) · 1.82 KB
/
roman.cpp
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
// do// do I need to check for string length or that it has the right letters?
#include <string>
#include <cstdio>
#include <iostream>
int romanToInt(std::string s);
int getcurrent(char c);
int romanToInt(std::string s) {
int charnum = s.length();
int current, cumulative, prev;
prev = 0;
cumulative = 0;
for (int i=0; i<charnum; ++i){
current = getcurrent(s[i]);
if((current==5 || current==10) && prev==1){
current = current - prev;
cumulative = cumulative - prev;
std::cout<<"in first if \n";
}
if ((current==50 || current==100) && prev==10){
current = current - prev;
cumulative = cumulative - prev;
std::cout<<"in second if \n";
}
if ((current==500 || current==1000) && prev==100){
current = current - prev;
cumulative = cumulative - prev;
std::cout<<"in third if \n";
}
std::cout<<"index: "<<i<< " current val: "<<current<<std::endl;
cumulative = cumulative + current;
prev = current;
}
return cumulative;
}
int getcurrent(char c){
int current;
switch(c){
case 'I':
current = 1;
break;
case 'V':
current = 5;
break;
case 'X':
current = 10;
break;
case 'L':
current = 50;
break;
case 'C':
current = 100;
break;
case 'D':
current = 500;
break;
case 'M':
current = 1000;
break;
}
return current;
}
int main(){
std::string mystr;
int ans;
std::cout<<"enter a roman numeral\n";
std::cin>> mystr;
ans = romanToInt(mystr);
std::cout<<"your answer is: "<<ans<<std::endl;
return 0;
}