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
|
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;
string DecimalToRoman(int);
int main() {
cout << "Lab 7 - Convert Decimal Number to Roman Numeral" << endl;
cout << "----------------------------------------" << endl;
int origNum;
cout << "Decimal Value Too Convert To Roman Numeral: ";
cin >> origNum;
if (origNum < 0 || origNum > 4999) {
cout << "Number must be between 1-4999 or 0 to End. ";
cin >> origNum;
}
while (origNum != 0) {
string result = DecimalToRoman(origNum);
cout << "" << result << " is the Roman Numeral equivalent of " << origNum << "." << endl;
cout << "Decimal Value Too Convert To Roman Numeral: ";
cin >> origNum;
}
}
string DecimalToRoman(int) {
int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
string numerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
string result = "";
int origNum;
for (int x = 0; values[x] > 0; ++x) {
while (origNum >= values[x]) {
origNum -= values[x];
result += numerals[x];
}
return result;
}
}
|