Question
Level 1: Units-only numbers write a C++ program that converts a single Roman number in the range I (1) to IX (9) to Arabic form.
Level 1: Units-only numbers
write a C++ program that converts a single Roman number in the range I (1) to IX (9) to Arabic form. The program should read a single string from standard input and print the corresponding value to standard output. Hint: use an array of strings to represent the Roman units digits, organised so that the string index corresponds to the digit value. For example, the array would have the string "IV" at index position 4 and the string "VII" at index position 7. You can then convert the input string by searching through the array until you find a match. The index where you find the match will be the digit value you need. Note that for reasons explained below, it is easiest to begin at the '9' end of the array and work downwards. Extend the program so that it works correctly when the input consists of either lower case or upper case Roman letters. The simplest approach is to convert each character in the input word into uppercase before trying to find a match. Run a loop over the characters in the string using the index/subscript operator ([]) to access each character and use the toupper function (you will need to include the cctype header file) to get the corresponding uppercase value.
Level 2: Multiple numbers
Extend the program so that it can deal with single digit numbers of any value. A single digit number is one that consists only of thousands, hundreds, tens, or units. Thus LXX (70) and CD (400) are single digit numbers, but XIV (14) and MC (1100) are not. Use the same approach as for units digits, but with 4 different arrays, one each for the thousands, hundreds, tens, and units digits. Try looking for thousands digits first, then for hundreds, and so on. When you find a match in one of the arrays, print the corresponding value and stop. Modify the program so that it reads and converts all input numbers until end of file (eof) on standard input. You will probably be able to do this by simply adding an appropriate reading loop around the code that reads a single line.
Level 3: Multi-digit numbers
Extend the program so that it can handle multi-digit numbers. The idea is to look for prefixes of the Roman number that correspond to valid Roman digits, beginning with the thousands digits and working from the '9' down to the '1' end. If you find a matching prefix, remove it and add the corresponding value to a progressive total, then move on to the next digit position. Since each digit is unique, and since larger digits are represented by longer strings than smaller ones, this process will always find the correct value. When you have considered all possible digits, the progressive total is the answer you need. Make sure the program behaves correctly even if the input does not consist of a valid Roman number. In such cases, convert the longest valid prefix and ignore any remaining characters. If no prefix characters are valid, display zero.
Level 4: Convert either way
Extend the program so that if the input is an Arabic number, the output is the corresponding Roman number. Use the first character in the input word to decide which way to convert. If it is an Arabic digit, convert as much as possible of the word to Roman. If it is a Roman digit letter, convert as much as possible to Arabic. If it is neither, output zero. Use the same digit arrays to convert both to and from Roman numbers.
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