Question
C++ Question int roman_char_to_number is an extra function that is not allowed. Modify the code to exclude : int roman_char_to_number(char c); from the class, but
C++ Question
int roman_char_to_number is an extra function that is not allowed.
Modify the code to exclude :
int roman_char_to_number(char c);
from the class, but keep the same functionality in the program (read file containing roman numerals and display its value if valid roman numeral).
No vectors! Rating helpful working solution thumb up.
Code:
class roman
{
public:
roman();
void set_roman(string);
string get_roman() const;
int get_arabic();
private:
string roman_numeral;
bool valid;
int roman_char_to_number(char c);
};
roman::roman() //Constructor
{
valid = false;
}
void roman::set_roman(string text)
{
bool temp=true;
for (int i = 0; i < text.length(); ++i)
{
if(roman_char_to_number(text[i]) == -1)
{
temp = false;
break;
}
}
valid = temp;
if(valid)
roman_numeral = text;
}
int roman::roman_char_to_number(char c)
{
switch(c)
{
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
default:
return -1;
}
}
string roman::get_roman() const
{
return roman_numeral;
}
int roman::get_arabic()
{
int result = 0;
if(roman_numeral.length() == 0)
return 0;
for (int i = 0; i < roman_numeral.length(); ++i)
{
int s1 = roman_char_to_number(roman_numeral[i]);
if(i + 1 < roman_numeral.length())
{
int s2 = roman_char_to_number(roman_numeral[i+1]);
if(s1 >= s2)
{
result = result + s1;
}
else
{
result = result + s2 - s1;
i++;
}
}
else
{
result = result + s1;
i++;
}
}
return result;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started