Question

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

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM CODE:

#include <iostream>
using namespace std;

//function to convert decimal to roman
int romanToDecimal(string roman)
{
int decimalNumber = 0;
int previousNumber = 0;
int currentNumber = 0;
for(int i=roman.length()-1;i>=0; i-- )
{
switch(roman.at(i))
{
case 'I': currentNumber = 1;
if(previousNumber > currentNumber)
decimalNumber -= currentNumber;
else decimalNumber += currentNumber;
previousNumber = 1;
break;
case 'V': currentNumber = 5;
if(previousNumber > currentNumber)
decimalNumber -= currentNumber;
else decimalNumber += currentNumber;
previousNumber = 5;
break;
case 'X': currentNumber = 10;
if(previousNumber > currentNumber)
decimalNumber -= currentNumber;
else decimalNumber += currentNumber;
previousNumber = 10;
break;
case 'L': currentNumber = 50;
if(previousNumber > currentNumber)
decimalNumber -= currentNumber;
else decimalNumber += currentNumber;
previousNumber = 50;
break;
case 'C': currentNumber = 100;
if(previousNumber > currentNumber)
decimalNumber -= currentNumber;
else decimalNumber += currentNumber;
previousNumber = 100;
break;
case 'D': currentNumber = 500;
if(previousNumber > currentNumber)
decimalNumber -= currentNumber;
else decimalNumber += currentNumber;
previousNumber = 500;
break;
case 'M': currentNumber = 1000;
if(previousNumber > currentNumber)
decimalNumber -= currentNumber;
else decimalNumber += currentNumber;
previousNumber = 1000;
break;
}
}
return decimalNumber;
}

int main() {
cout<<"XVII: ";
int num1 = romanToDecimal("XVII");
cout<<num1<<endl;
cout<<"XXXX: ";
int num2 = romanToDecimal("XXXX");
cout<<num2<<endl;
return 0;
}

OUTPUT:

XVII: 17
XXXX: 40
Add a comment
Know the answer?
Add Answer to:
Write up a detailed solution to the problem: Design a program to convert a Roman numeral...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers....

    (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers. Write a program that converts a positive integer into the Roman number system. The Roman number system has digits I 1 V 5 X 10 L 50 C 100 D 500 M 1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately....

  • C++ Write a program that converts anumber entered in Roman numerals to decimal.

    In C++ Write a program that converts a number entered in Roman numerals to decimal. Your program should consist of a class, say,romanType. An object of typeromanTypeshould do the following:a. Store the number as a Romannumeral.b. Convert and store the number into decimalform.c. Print the number as a Roman numeral ordecimal number as requested by the user.The decimal values of the Roman numerals are:M 1000D 500C 100L 50X 10V 5I 1d. Test your program using the followingRoman numerals: MCXIV, CCCLIX,...

  • I'm having trouble getting a certain output with my program Here's my output: Please input a...

    I'm having trouble getting a certain output with my program Here's my output: Please input a value in Roman numeral or EXIT or quit: MM MM = 2000 Please input a value in Roman numeral or EXIT or quit: mxvi Illegal Characters. Please input a value in Roman numeral or EXIT or quit: MXVI MXVI = 969 Please input a value in Roman numeral or EXIT or quit: EXIT Illegal Characters. Here's my desired output: Please input a value in...

  • i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due...

    i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due Sunday, 19 May 2019, 11:59 PM Due Friday, 24 May 2019, 11:59 PM Part B: Minimum Submission Requirements Ensure that your Lab4 folder contains the following files (note the capitalization convention): o Diagram.pdf o Lab4. asm O README.txt Commit and push your repository Lab Objective In this lab, you will develop a more detailed understanding of how...

  • Program Info: Write a program that accepts a year written as a four-digit Arabic (ordinary) numeral...

    Program Info: Write a program that accepts a year written as a four-digit Arabic (ordinary) numeral and outputs the year written in Roman numerals. Important Roman numerals are V for 5, X for 10, L for 50, C for 100, D for 500, and M for 1,000. Recall that some numbers are formed by using a kind of subtraction of one Roman “digit”; for example, IV is 4 produced as V minus I, XL is 40, CM is 900, and...

  • Can someone code this asap? Use any language that you want. 2. Ancestral Names Given a...

    Can someone code this asap? Use any language that you want. 2. Ancestral Names Given a list of strings comprised of a name and a Roman numeral, sort the list first by name, then by decimal value of the Roman numeral. In Roman numerals, a value is not repeated more than three times. At that point, a smaller value precedes a larger value to indicate subtraction. For example, the letter I represents the number 1, and Vrepresents 5. Reason through...

  • Lab 4: Java Fundamentals, Part IV In this assignment, you solve a conversion problem similar to...

    Lab 4: Java Fundamentals, Part IV In this assignment, you solve a conversion problem similar to Programming Challenge 1 of Chapter 3 of your text, page 184 (187 in Edition 5). Given one of the Roman numerals I, II, III, IV, V, VI, VII, VIII, IX, X, XI, XII, XIII, XIV, XV as an input your program must determine and display the corresponding decimal digit 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15....

  • /** * This program Performs various number base conversions. It also verifies if * a number...

    /** * This program Performs various number base conversions. It also verifies if * a number is valid in its base. * Author: M. Rahman * Date: 06 September 2018 */ public class NumberConversion { public static String dec2any(String dec, int base) { /** * Converts a decimal value to a target base * inputs: * dec: the decimal value to be converted * base: the target base * output: 256-base as dotted decimal, hex as usual, bases * over...

  • Looking for help understanding how this example program flows and works. Can you write comments next...

    Looking for help understanding how this example program flows and works. Can you write comments next to each line of code explaining what it does? Why does it use a switch? why not set the roman numerals as constants instead of a switch? Thank you!! //CPP program for converting roman into integers #include <iostream> #include <fstream> #include<cctype> #include<cstdlib> #include<string.h> using namespace std; int value(char roman) { switch(roman) { case 'I':return 1; case 'V':return 5; case 'X':return 10; case 'L':return 50;...

  • In this homework, you will design a program to perform the following task: Write a program...

    In this homework, you will design a program to perform the following task: Write a program that would allow a user to enter student names and Final grades (e.g. A,B,C,D,F) from their courses. You do not know how many students need to be entered. You also do not know how many courses each of the students completed. Design your program to calculate the Grade Point Average (GPA) for each student based on each of their final grades. The program should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT