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

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

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!