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

Write up a detailed solution to the problem: Design a program to convert a Roman

ID: 3807169 • Letter: W

Question

Write up a detailed solution to the problem:

Design a program to convert a Roman numeral to a decimal
number. The program should read a Roman numeral. You
may read it as a string or one character at a time. Do
the conversion and then output the decimal number.

Here are the “letters” you need to know:

Symbol =   Value
I   = 1
V =   5
X =   10
L =   50
C =   100
D =   500
M   = 1,000

  

1. Code the pseudocode design as written into C++.

2. Develop test data to prove whether or not the design works. You may use the Roman Numeral-Decimal calculator at this website to create your test data http://www.csgnetwork.com/csgromancnv.html (Links to an external site.) Include test data that tests for all the subtractive exception only values ( 4, 9, 40, 90, 400, and 900, which are written as IV, IX, XL, XC, CD, and CM respectively ) as well as the additive cases. Summarize your results - the summary can be very simple. "I coded and tested your design it (does or doesn't work)."

4. Submit the source code, test data that shows if the code works or not, and your summary for the design

If the design does not work :

1. explain what changes you would have to make to the design to make it work

2. incorporate the changes into the pseudocode.

3. code the new design and use your test data to prove it works.

--------------------------------------------------------------------------------------------

Problem

Design a program to convert a Roman numeral to a decimal number. The program should read a Roman numeral. You may read it as a string or one character at a time. Do the conversion and then output the decimal number.

Here are the “letters” you need to know:

Symbol                 Value

I                          1

V                        5

X                         10

L                         50

C                         100

D                        500

M                       1,000

-------

Design #1

Start

string romanNumeral

num decimalNumber equals 0

num previousNumber equals 0

num currentNumber equals 0

             

output “Enter number: ”

input romanNumeral

loop from counter equals (romanNumeral length - 1) to 0

              test romanNumeral at counter

                           first case : I

                                         set currentNumber equal to 1

                                                       if previousNumber is greater than currentNumber

                                                       decimalNumber equals decimalNumber minus currentNumber

                                                       else

                                                       decimalNumber equals decimalNumber plus currentNumber

                                         set previousNumber equal to 1

                           second case : V

                                         set currentNumber equal to 5

                                                       if previousNumber is greater than currentNumber

                                                       decimalNumber equals decimalNumber minus currentNumber

                                                       else

                                                       decimalNumber equals decimalNumber plus currentNumber

                                         set previousNumber equal to 5

                           third case : X

                                         set currentNumber equal to 10

                                                       if previousNumber is greater than currentNumber

                                                       decimalNumber equals decimalNumber minus currentNumber

                                                       else

                                                       decimalNumber equals decimalNumber plus currentNumber

                                         set previousNumber equal to 10

                           fourth case : L

                                         set currentNumber equal to 50

                                                       if previousNumber is greater than currentNumber

                                                       decimalNumber equals decimalNumber minus currentNumber

                                                       else

                                                       decimalNumber equals decimalNumber plus currentNumber

                                         set previousNumber equal to 50

                           fifth case : C

                                         set currentNumber equal to 100

                                                       if previousNumber is greater than currentNumber

                                                       decimalNumber equals decimalNumber minus currentNumber

                                                       else

                                                       decimalNumber equals decimalNumber plus currentNumber

                                         set previousNumber equal to 100

                           sixth case : D

                                         set currentNumber equal to 500

                                                       if previousNumber is greater than currentNumber

                                                       decimalNumber equals decimalNumber minus currentNumber

                                                       else

                                                       decimalNumber equals decimalNumber plus currentNumber

                                         set previousNumber equal to 500

                           seventh case : M

                                         set currentNumber equal to 1000

                                                       if previousNumber is greater than currentNumber

                                                       decimalNumber equals decimalNumber minus currentNumber

                                                       else

                                                       decimalNumber equals decimalNumber plus currentNumber

                                         set previousNumber equal to 1000

Force decimalNumber to show decimal point and set the precision to the desired number of decimal places

Output decimalNumber

end

-------

Design #11

Variables:

string enteredNumerals

char numeral

int value1

int value2

int result = 0

Output a prompt to enter the number in Roman numerals

Read the input into enteredNumerals

Assign the first character in the string to numeral

Use a switch statement to assign the correct integer value to value1 based

on the character that was entered

While a counter (starting at 1) is less than the length of the string

              Read the next character into numeral

             

              Use another switch statement to convert the numeral to an integer and

              assign it to value2

              If value2 is greater than value 1

                           result = result + (value2 - value1)

              end if

              else

                           result = result + value1

                           if counter is equal to the length of the string - 1

                                         result = result + value2

                           end if

              end else

              value1 = value2

              increment counter

endwhile

if result = 0

              output value1

end if

else

              output result

end else

Explanation / Answer

try this

#include <iostream>
#include <string>

using namespace std;

struct Roman_Number
{
public:
   string get()
   {
       return roman_Number;
   }

   void show()
   {
       cout << "Converted Roman Number is " << roman_Number << endl;
   }

   Roman_Number(const string &input)
   {
       roman_Number = input;
   }

   int convert_value()
   {
       int length = roman_Number.length();
       int previous = 0;
       bool error = false;
       int nIndex = 0;

       sum = 0;

       while( (error == false) && (nIndex < length) )
       {
           switch(roman_Number[nIndex])
           {
           case 'M':
               sum += 1000;
               if(previous < 1000)
               {
                   sum -= 2 * previous;
               }
               previous = 1000;
               break;
           case 'D':
               sum += 500;
               if(previous < 500)
               {
                   sum -= 2 * previous;
               }
               previous = 500;
               break;
           case 'C':
               sum += 100;
               if(previous < 100)
               {
                   sum -= 2 * previous;
               }
               previous = 100;
               break;
           case 'L':
               sum += 50;
               if(previous < 50)
               {
                   sum -= 2 * previous;
               }
               previous = 50;
               break;
           case 'X':
               sum += 10;
               if(previous < 10)
               {
                   sum -= 2 * previous;
               }
               previous = 10;
               break;
           case 'V':
               sum += 5;
               if(previous < 5)
               {
                   sum -= 2 * previous;
               }
               previous = 5;
               break;
           case 'I':
               sum += 1;
               if(previous < 1)
               {
                   sum -= 2 * previous;
               }
               previous = 1;
               break;
           default:
               cout << roman_Number[nIndex] << " is not a Entry Romal Numeral" << endl;
               error = true;
               sum = 0;
           }

           nIndex++;

       }
       return sum;
   }

   int length()
   {
       return roman_Number.length();
   }

private:
   string roman_Number;
   int sum;
};

int main()
{
   string myInput;
   int value;
   cout << "Please enter the Roman Numeral to convert : ";
   cin >> myInput;
   Roman_Number myRoman_Number(myInput);
   value = myRoman_Number.convert_value();

   cout << "Roman Numeral " << myInput << " equals " << value <<endl;

}