-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman.cpp
112 lines (100 loc) · 2.19 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<iostream>
#include<string>
using namespace std;
string ToRoman(int input);
string ToRoman2(int input);
int main(int argc, char* argv[])
{
int input;
cout<<"Enter NUmber"<<endl;
cin>>input;
string roman;
roman = ToRoman2(input);
cout<<endl<<roman<<endl;
return 0;
}
//SOme issue with function:
string ToRoman (int input)
{
if(input >= 1000) return "M" + ToRoman(input - 1000);
if(input >= 900) return "CM" + ToRoman(input - 900);
if(input >= 500) return "D" + ToRoman(input - 500);
if(input >= 400) return "CD" + ToRoman(input - 400);
if(input >= 100) return "C" + ToRoman(input - 100);
if(input >= 90) return "XC" + ToRoman(input - 90);
if(input >= 50) return "L" + ToRoman(input - 50);
if(input >= 40) return "XL" + ToRoman(input - 40);
if(input >= 10) return "X" + ToRoman(input - 10);
if(input >= 5) return "V" + ToRoman(input - 5);
if(input >= 1) return "I" + ToRoman(input - 1);
}
string ToRoman2 (int input)
{
string roman="";
while(input >= 1000)
{
roman += "M";
input -= 1000;
}
while(input >= 900)
{
roman += "CM";
input -= 900;
}
while(input >= 500)
{
roman += "D";
input -= 500;
}
while(input >= 400)
{
roman += "CD";
input -= 400;
}
while(input >= 100)
{
roman += "C";
input -= 100;
}
while(input >= 90)
{
roman += "XC";
input -= 90;
}
while(input >= 50)
{
roman += "L";
input -= 50;
}
while(input >= 40)
{
roman += "XL";
input -= 40;
}
while(input >= 10)
{
roman += "X";
input -= 10;
}
while(input >= 9)
{
roman += "IX";
input -= 9;
}
while(input >= 5)
{
roman += "V";
input -= 5;
}
while(input >= 4)
{
roman += "IV";
input -= 4;
}
while(input >= 1)
{
roman += "I";
input -= 1;
}
return roman;
}