Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that converts a number etered in Roman numerals to a decimal. Yo

ID: 3550853 • Letter: W

Question

Write a program that converts a number etered in Roman numerals to a decimal.  Your program should consist of a class, say, romanType.  An object of type romanType should do the following:

a/  Store the number as a Roman numeral

b.  Convert and store the number into decimal form

c,  Print the number as a Roman numeral or deicmal number as requested by the user.

d.  the decimal values of the Roman numeral are

  M   1000

  D  500

  C  100

  L   50

  X  10

  V  5

  I  `

e. Test your program using the following Roman numerals MCXIV, CCCLIX, MDC:XVI.

  

Explanation / Answer

#include <iostream>

#include <string>


int roman_2_decimal(const std::string& i_string)

{

static const std::string roman[] = {"M", "CM", "CD", "D", "C", "XC", "XL", "L", "X", "IX", "IV", "V", "I"};

static const int decimal[] = { 1000, 900, 400, 500, 100, 90, 40, 50, 10, 9, 4, 5, 1};

static const size_t size = sizeof(decimal)/sizeof(decimal[0]);


int value = 0;

size_t index = 0;

for(int i = 0; i<size; ++i)

{

while(i_string.substr(index, roman[i].length()) == roman[i])

{

value += decimal[i];

index += roman[i].length();

}

}

return value;

}


int main()

{

std::cout << "MCXIV" << ": " << roman_2_decimal("MCXIV") << std::endl;

std::cout << "CCCLIX" << ": " << roman_2_decimal("CCCLIX") << std::endl;

std::cout << "MDCLXVI" << ": " << roman_2_decimal("MDCLXVI") << std::endl;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote