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
|
//Roman Numeral Calculator
// vincent
#include <iostream>
#include <cstdlib>
using namespace std;
void convertNumeral( string, int&);
void convertDecimalPrint(int);
void calculateSum(int, char, int, int&);
int main()
{
string numeral, numeral1, numeral2;
int value1, value2, decimalValue, decimalSum;
char arithmetic;
cout << "This program will calculate 2 roman numeral numbers" << endl
<< "and print out the decimal values and sum in roman numerals." << endl << endl;
cout << "Input first roman numeral number: ";
cin >> numeral;
cout << endl << numeral;
numeral1 = numeral;
convertNumeral(numeral, decimalValue);
cout << "test";
value1 = decimalValue;
cout << endl << "The first number is " << value1 << endl;
cout << "Input second roman numeral number: ";
cin >> numeral;
cout << endl << numeral;
numeral2 = numeral;
convertNumeral(numeral, decimalValue);
value2 = decimalValue;
cout << endl << "The second number is " << value2 << endl;
cout << "Enter the desired arithmetic operation : ";
cin >> arithmetic;
cout << endl << arithmetic;
calculateSum(value1, arithmetic, value2, decimalSum);
cout << numeral1 << " " << arithmetic << " " << numeral2 << " = ";
convertDecimalPrint(decimalSum);
cout << " (" << decimalSum << ")";
system("pause");
return 0;
}
//**************************************************************************
// convert numeral to decimal number
void convertNumeral(/*in*/ string numeral,
/*out*/ int& decimal)
{
char singleNumeral;
int iterateCount;
iterateCount = 0; // integer to make .at function go to next character
decimal = 0;
singleNumeral = numeral.at(iterateCount);
while(singleNumeral != ' ')
{
cout << decimal;
cout << singleNumeral;
if(singleNumeral == 'M')
decimal = decimal + 1000;
if(singleNumeral == 'D')
decimal = decimal + 500;
if(singleNumeral == 'C')
decimal = decimal + 100;
if(singleNumeral == 'L')
decimal = decimal + 50;
if(singleNumeral == 'X')
decimal = decimal + 10;
if(singleNumeral == 'V')
decimal = decimal + 5;
if(singleNumeral == 'I')
decimal = decimal + 1;
iterateCount++;
singleNumeral = numeral.at(iterateCount);
cout << decimal;
}
}
//***************************************************************************
// convert decimal number to numeral (prints out)
void convertDecimalPrint(/*in*/ int decimalSum)
{
while(decimalSum >= 1000)
{
cout << "M";
decimalSum = decimalSum - 1000;
}
while(decimalSum >= 500)
{
cout << "D";
decimalSum = decimalSum - 500;
}
while(decimalSum >= 100)
{
cout << "C";
decimalSum = decimalSum - 100;
}
while(decimalSum >= 50)
{
cout << "L";
decimalSum = decimalSum - 50;
}
while(decimalSum >= 10)
{
cout << "X";
decimalSum = decimalSum - 10;
}
while(decimalSum >= 5)
{
cout << "V";
decimalSum = decimalSum - 5;
}
while(decimalSum >= 1)
{
cout << "I";
decimalSum = decimalSum - 1;
}
}
//*****************************************************************************
// calculate the numbers
void calculateSum(int value1,
char arithmetic,
int value2,
int& decimalSum)
{
if(arithmetic == '+')
decimalSum = value1 + value2;
if(arithmetic == '-')
decimalSum = value1 - value2;
if(arithmetic == '*')
decimalSum = value1 * value2;
if(arithmetic == '/')
decimalSum = value1 / value2;
}
|