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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
#include <iostream>
#include <string>
using namespace std;
//Function Header
void toarabConv();
void toRom();
int main()
{
int loop = 1;
char type;
while (loop == 1)
{
cout << "This program is designed to convert Roman Numeral to Arabic numeral, or vice-versa.\n\n";
cout << "Do you want to convert to Roman or to Arabic? R for Roman, A for Arabic (Q to quit): ";
cin >> type;
type = tolower(type);
if (type == 'q') //quit option
{
loop = 0;
return loop;
}
else
{
break;
}
while ((type != 'r') || (type != 'a'))
{
cout << "\nUser Error: If you want to convert to Roman, enter an R. If you want to convert to Arabic, enter an A." << endl; //Not witty enough to make mean remarks!
cout << "Do you want to convert to Roman or to Arabic? R for Roman, A for Arabic (Q to quit): ";
cin >> type;
type = tolower(type);
if (type == 'q')
{
loop = 0;
return loop;
}
else
{
break;
}//end if/else
}//end while for type
if (type == 'a')
{
toarabConv();
break;
}
else
{
toRom();
break;
}//end else
}//end while for variable loop
}//end main
//Roman to Arabic
void toarabConv()
{
char num[5];
int sum = 0, i = 0;
cout << "Enter a Roman numeral: "; //decide on a limit? Or an equation that should work it all out?
cin >> num;
for (int i = 0; i < 5; i++)//Sum
{
num[i] = tolower(num[i]);
switch (num[i])
{
case 'm': sum += 1000; break;
case 'd': sum += 500; break;
case 'c': sum += 100; break;
case 'l': sum += 50; break;
case 'x': sum += 10; break;
case 'v': sum += 5; break;
case 'i': sum += 1; break;
default:
cout << "Error: Invalid characters encountered." << endl;
int error = 1;
sum = 0;
while (error != 0)
{
cout << "Enter a roman numeral:\t";
cin >> num;
for (int i = 0; i < 5; i++)//Sum
{
num[i] = tolower(num[i]);
switch (num[i])
{
case 'm': sum += 1000; break;
case 'd': sum += 500; break;
case 'c': sum += 100; break;
case 'l': sum += 50; break;
case 'x': sum += 10; break;
case 'v': sum += 5; break;
case 'i': sum += 1; break;
default:
cout << "Error: Invalid characters encountered." << endl;
int error = 1;
sum = 0;
}//end switch
}//end for
}//end while
}//end for
}//end while
cout << endl;
cout << "The Roman numeral, " << num << " translates to Arabic number, " << sum << "." << endl;
return;
}//end R2A function
//Arabic to Roman
void toRom()
{
int num = 0, onum = 0;
cout << "Enter an Arabic number between 1 and 5000: "; //decide on a limit?
cin >> onum;
while ((num > 5000) || (num <= 0))
{
cout << "User Error: You can only use a number between 1 and 5000." << endl;
cin >> num;
}
num = onum;
int m, d, c, l, x, v, i, n;
{
int num = 0;
if (num >= 1000)
{
m = num / 1000;
n = 0;
{
for (n; n < m; n++)
cout << "M";
}
num = num % 1000;
}
if (num >= 900)
{
cout << "CM";
num = num % 900;
}
else if (num >= 500)
{
{
d = num / 500;
n = 0;
for (n; n < d; n++)
cout << "D";
}
num = num % 500;
}
if (num >= 400)
{
cout << "CD";
num = num % 400;
}
else if (num >= 100)
{
{
c = num / 100;
n = 0;
for (n; n < c; n++)
cout << "C";
}
num = num % 100;
}
if (num >= 90)
{
cout << "XC";
num = num % 90;
}
else if (num >= 50)
{
{
l = num / 50;
n = 0;
for (n; n < l; n++)
cout << "L";
}
num = num % 50;
}
if (num >= 40)
{
cout << "XL";
num = num % 40;
}
else if (num >= 10)
{
{
x = num / 10;
n = 0;
for (n; n < x; n++)
cout << "X";
}
num = num % 10;
}
if (num >= 9)
{
cout << "IX";
num = num % 9;
}
else if (num >= 5)
{
{
v = num / 5;
n = 0;
for (n; n < v; n++)
cout << "V";
}
num = num % 5;
}
if (num >= 4)
{
cout << "IV";
num = num % 4;
}
else if (num >= 1)
{
i = num;
n = 0;
for (n; n < i; n++)
cout << "I";
}
}
cout << endl;
cout << "The Arabic number " << onum << " translates to Roman numeral " << num << "." << endl;
return;
}
|