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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!