Write a C++ program that converts a string representing a number in Roman numera
ID: 3632317 • Letter: W
Question
Write a C++ program that converts a string representing a number in Roman numeral form to decimal form. The symbols used in the Roman numeral system and their equivalents are given below: For example, the following are Roman numbers (and their corresponding decimal values): XII (12); CII (102); XL (40). In converting the Roman numerals to decimal, you must add up the numeric value of each symbol with one exception. If a smaller roman symbol appears before a larger one in the string, then it must be subtracted in decimal conversion. This is why XL is 40. IV is 4. and XCI is 91. Make sure the program is able to handle lower case letters. Test your program for CIV, MDI, V, DXCV, CDXLIV, XLIX, CMXCIX, DCCCLXXXVIII, and VII, XLIV, CXLIVExplanation / Answer
my roman2decimal function is kinda long but it works...
#include <iostream>
#include <string>
#include <algorithm> //needed to convert string to uppercase
using namespace std;
int main()
{
//PROTOTYPES
string decimal2roman(int input); //for testing purpose
int roman2decimal(string input);
//LOCAL DECLARATIONS
int number = 0;
string roman = "";
int r2n = 0;
int eCount = 0;
//PROCEDURES
//**************check***************
cout << "**************check*************** ";
for (number = 1; number < 4000; ++number)
{
roman = decimal2roman(number);
r2n = roman2decimal(roman);
if (r2n == number)
cout << " OK : " << r2n << " ";
else
{
cout << "ERROR: " << number << " != " << r2n << " ";
eCount++;
}
}
cout << "Errors count: " << eCount << endl;
cout << "*************end check*********** ";
//*************end check***********
//main procedure start from here
cout << endl;
for ( ; ; )
{
cout << "Enter Roman Number: ";
cin >> roman;
transform(roman.begin(), roman.end(), roman.begin(), ::toupper);
cin.ignore();
cout << "Decimal value: " << roman2decimal(roman) << endl << endl;
}
cin.get();
return 0;
}
//--------------------------------------------------
//FUNCTION DEFINITIONS
//--------------------------------------------------
// This is an extra function for converting 0-3999 to
//roman numbers i got from Wikipedia.
// This function is used for test mode only.
//--------------------------------------------------
string decimal2roman(int input)
{
const string ROMAN[13] = {
"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"
};
const int DECIMAL[13] = {
1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1
};
string romanValue = "";
for (int i = 0; i < 13; i++)
{
while (input >= DECIMAL[i])
{
input -= DECIMAL[i];
romanValue += ROMAN[i];
}
}
return romanValue;
}
//--------------------------------------------------
// This function convert a string represent a roman
//number to decimal value.
// This function can read the UPPERCASE input only.
//Convert input in main() to upper to make sure before
//call this function
// This function DOES NOT check if input is in a
//correct Roman number format. It ASSUMES input is
//already in a CORRECT FORMAT.
//--------------------------------------------------
int roman2decimal(string input)
{
const char ROMAN[7] = {
'I', 'V', 'X', 'L', 'C', 'D', 'M'
};
const int DECIMAL[7] = {
1, 5, 10, 50, 100, 500, 1000
};
int decimalValue = 0;
int len = input.length();
for (int i = 0; i < len; i++)
{
int r; //r romanIndex
int m; //m if this char is I X C, need to check minus
bool posM = false; //possible minus
for (r = 0; r < 7; r++)
{
if (input[i] == ROMAN[r])
break;
}
for (m = 0; m < 5; m += 2)
{
if (input[i] == ROMAN[m])
{
posM = true;
break;
}
}
if (posM)
{
if (i + 3 < len)
{
if (input[i+3] == ROMAN[m+1])
{
decimalValue -= 3 * DECIMAL[r];
decimalValue += DECIMAL[m+1];
i += 3;
}
else if (input[i+3] == ROMAN[m+2])
{
decimalValue -= 3 * DECIMAL[r];
decimalValue += DECIMAL[m+2];
i += 3;
}
else if (input[i+2] == ROMAN[m+1])
{
decimalValue -= 2 * DECIMAL[r];
decimalValue += DECIMAL[m+1];
i += 2;
}
else if (input[i+2] == ROMAN[m+2])
{
decimalValue -= 2 * DECIMAL[r];
decimalValue += DECIMAL[m+2];
i += 2;
}
else if (input[i+1] == ROMAN[m+1])
{
decimalValue -= DECIMAL[r];
decimalValue += DECIMAL[m+1];
i++;
}
else if (input[i+1] == ROMAN[m+2])
{
decimalValue -= DECIMAL[r];
decimalValue += DECIMAL[m+2];
i++;
}
else
{
decimalValue += DECIMAL[r];
}
}
else if (i + 2 < len)
{
if (input[i+2] == ROMAN[m+1])
{
decimalValue -= 2 * DECIMAL[r];
decimalValue += DECIMAL[m+1];
i += 2;
}
else if (input[i+2] == ROMAN[m+2])
{
decimalValue -= 2 * DECIMAL[r];
decimalValue += DECIMAL[m+2];
i += 2;
}
else if (input[i+1] == ROMAN[m+1])
{
decimalValue -= DECIMAL[r];
decimalValue += DECIMAL[m+1];
i++;
}
else if (input[i+1] == ROMAN[m+2])
{
decimalValue -= DECIMAL[r];
decimalValue += DECIMAL[m+2];
i++;
}
else
{
decimalValue += DECIMAL[r];
}
}
else if (i + 1 < len)
{
if (input[i+1] == ROMAN[m+1])
{
decimalValue -= DECIMAL[r];
decimalValue += DECIMAL[m+1];
i++;
}
else if (input[i+1] == ROMAN[m+2])
{
decimalValue -= DECIMAL[r];
decimalValue += DECIMAL[m+2];
i++;
}
else
{
decimalValue += DECIMAL[r];
}
}
else
{
decimalValue += DECIMAL[r];
}
}
else
{
decimalValue += DECIMAL[r];
}
}
return decimalValue;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.