I need to be able to sum Roman numerals in the most simple way, not necessarily proper numerals. Somebody please tell me what I'm doing wrong.
#include <iostream>
using namespace std;
int main()
{
{
int M = 1000;
int D = 500;
int C = 100;
int L = 50;
int X = 10;
int V = 5;
int I = 1;
}
{
const int A_ROMAN = 50;
int sum[A_ROMAN];
cout << "Enter your first Roman Numeral";
cin >> [A_ROMAN];
{
for (int a = 0; a < 5; a++)
{
sum += A_ROMAN[a];
}
}
}
{
const int B_ROMAN = 50;
int sum[B_ROMAN];
cout << "Enter your second Roman Numeral";
cin >> B_ROMAN[b];
{
for (int b = 0; b < 5; b++)
{
sum += B_ROMAN[b];
}
}
}
}
I think you'll find that adding numbers in a roman numeral representation is a relatively complex operation. You'll find it easier to convert to a regular number, add and convert back to roman numerals.
Does this work at all?
#include <iostream>
using namespace std;
int main
{
switch (ROMAN_NUM)
{
case 'M': 1000;
case 'D': 500;
case 'C': 100;
case 'L': 50;
case 'X': 10;
case 'V': 5;
case 'I': 1;
}
const int A_ROMAN = 50
int sum[A_ROMAN];
cout << "Enter the first Roman numeral.";
cin >> [A_ROMAN];
{
for (int a = 0; a < A_ROMAN; a++)
total += A_ROMAN[a];
}
cout << "The decimal of the first Roman numeral is" << total << endl;
To convert Roman→decimal, I recommend the following approach:
For each 'match', first-to-last in the following list, left-to-right in the roman numeral string, add the associated value:
M → 1000
CM → 900
D → 500
CD → 400
C → 100
XC → 90
L → 50
XL → 40
X → 10
IX → 9
V → 5
IV → 4
I → 1
The first tricky part is the order that you do things, which must be exact.
The other tricky part is, of course, that a match may be more than one letter. So, for example
MCMLXXIV
matches as
M CM L X X IV
which is the sum:
1000 + 900 + 50 + 10 + 10 + 4
or 1974.
When you are ready to convert back, you use the table above, but work with the arrows pointing in the other direction. So
1974 - 1000 (M) = 974
974 - 900 (CM) = 74
74 - 50 (L) = 24
24 - 10 (X) = 14
14 - 10 (X) = 4
4 - 4 (IV) = 0
giving you MCMLXXIV.
Oh, and 0 is "N" or "NULLA" in Roman numerals. You'll need a special case to check for that.