Question

In: Computer Science

Write a Python program to implement a form of a Roman numeral calculator. We are using...

Write a Python program to implement a form of a Roman numeral calculator. We are using the purely additive form of Roman numerals. By that, we mean that a number is simply the sum of its digits; for example, 4 equals IIII, in our additive notation. This means that we are NOT using IV for 4. Each Roman numeral must start with the digit of highest value and ends with the digit of smallest value. That is 9 is VIIII and NOT IIIIV. Your program continually (in a loop) inputs 2 Roman numbers and an arithmetic operator and prints the result of the operation as a Roman number. The values of the Roman digits (upper case letters only) are as follows:

Roman Digit Value of Roman Digit I 1 V 5 X 10 L 50 C 100 D 500 M 1000 So, the Roman number MMVIIII represents 2009. The arithmetic operators that your program must recognize in the input are +, -, *, and //. These should perform the Python integer operations of addition, subtraction, multiplication, and division, respectively. Your program must loop, processing 2 Roman numbers with an operator, finishing when end of file is reached. You do not have to ensure that the input is in purely additive form, that is, digits are followed only by digits of the same or lower value. Your program does NOT have to check for this. You can assume that only positive numbers will be entered as input and you don't have to check for negative numbers. If the result is negative, you must print out a minus sign followed by the absolute value of the result printed as a Roman Numeral. See the sample runs below. If the result is zero, print the word "zero". --------------------------------------

Solutions

Expert Solution

Please find the python program to print the ROMAN value after the operation and added necessary comments in the program.

Program:

#read the inputs one by one
num1=str(input("Enter the ROMAN number 1: "))
num2=str(input("Enter the ROMAN number 2: "))
op=str(input("Enter the operation[+ or - or / or *]: "))

#valid the operators
if op != "-" and op != "+" and op != "/" and op != '*':
print("Invalid operation. Please try again. Quiting..")
  
#store the values of ROMAN number in dictionary
#I 1 V 5 X 10 L 50 C 100 D 500 M 100
romanDict = {'I' : 1, 'V': 5, 'X': 10, 'L':50, 'C': 100, 'D':500, 'M':1000}

#convert the ROMAN number 1 to integer
len1=len(num1)
numValue1=0;
for i in range(len1):
if num1[i].upper() in romanDict:
key=num1[i].upper()
#read from dictionary and add its value to numValue1
numValue1 = numValue1 + romanDict[key]

#convert the ROMAN number 2 to integer
len2=len(num2)
numValue2=0;
for i in range(len2):
if num2[i].upper() in romanDict:
key=num2[i].upper()
#read from dictionary and add its value to numValue2
numValue2 = numValue2 + romanDict[key]
  
#based on operation do the calcualtion
if op == '+':
totalValue = numValue1 + numValue2
elif op == '-':
totalValue = numValue1 - numValue2
elif op == '*':
totalValue = numValue1 * numValue2
else:
totalValue = int (numValue1 / numValue2)

#uncomment the following line for debugging
#print(numValue1, numValue2, totalValue)

#if the totalValue is in negative, print "-" and multiply it with
#"-1" to make positive
# TODO: write code...
if totalValue < 0:
print("-",end="")
totalValue=totalValue * -1
  
#print zero if the totalValue is 0
if totalValue == 0:
print("zero")
else:
#continue the loop until totalValue becomes 0
while totalValue > 0:
#based on condition print the ROMAN value
if totalValue >= 1000:
print('M', end="")
totalValue = totalValue -1000
elif totalValue >= 500:
print('D', end="")
totalValue = totalValue -500
elif totalValue >= 100:
print('C', end="")
totalValue = totalValue -100
elif totalValue >= 50:
print('L', end="")
totalValue = totalValue -50
elif totalValue >= 10:
print('X', end="")
totalValue = totalValue -10
elif totalValue >= 5:
print('V', end="")
totalValue = totalValue -5
elif totalValue >= 1:
print('I', end="")
totalValue = totalValue -1
  
  

Output:

Enter the ROMAN number 1: M
Enter the ROMAN number 2: M
Enter the operation[+ or - or / or *]: -
zero

Enter the ROMAN number 1: MC
Enter the ROMAN number 2: MI
Enter the operation[+ or - or / or *]: +
MMCI

Enter the ROMAN number 1: C
Enter the ROMAN number 2: M
Enter the operation[+ or - or / or *]: -
-DCCCC

Screen Shot:


Related Solutions

Write a simple java program to list roman numeral for a given range of numbers. Roman...
Write a simple java program to list roman numeral for a given range of numbers. Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M. I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000 Roman numerals are usually written largest to smallest from left to right. But a number like 4 is not written as IIII. It is written as IV. Because...
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack...
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack • When an operand is read, push it on statck • When an operator is read i.e +, *. /, - – Pop two from the top of the stack and apply the operator and push the result on stack if there is one value instead of two display an error message • Keep repeating until an equal sign, = is read; pop from...
Write a Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...
Conversion of numeral to roman number: Create a FLOWCHART and C++ PROGRAM that prompts the user...
Conversion of numeral to roman number: Create a FLOWCHART and C++ PROGRAM that prompts the user to enter a number (from 1-3000 only) then output the number and its corresponding roman numeral number. ***validate the input (it must be 1-3000 only) SAMPLE OUTPUT: Enter a Number: 0 Number should be from 1-3000 only (Terminated) Enter a Number: -5 Number should be from 1-3000 only (Terminated) Enter a Number: 3500 Number should be from 1-3000 only (Terminated) Enter a Number: 1994...
Problem Statement Create a program that will convert a decimal value into roman numeral, and output...
Problem Statement Create a program that will convert a decimal value into roman numeral, and output the result. The program will ask for an integer input, assume all inputs are valid, and the program should not end unless the user tells you to. Refactoring Once your program is running correctly, put your conversion algorithm into a function named d_to_r(), which takes a string as parameter and return an int. You are allowed to create more helper functions if needed. Link...
Write a recursive function that converts Old Roman Numeral to base 10 values.
C++ program Write a recursive function that converts Old Roman Numeral to base 10 values. Print out the Old Roman Number and it base 10 equivalents. You will read all the digits of a Roman number into an array of characters. You will process each Roman digit in the array using ONLY POINTERS. Putting a ‘\0’ at the end of the input characters allows you to print the string easier. Use the following input to test your function:    DVIII, MMMDCCCCLXXXXIIII,...
in Java Write a function that inputs a 4 digit year and outputs the ROMAN numeral...
in Java Write a function that inputs a 4 digit year and outputs the ROMAN numeral year M is 1,000 C is 100 L is 50 X is 10 I is 1 Test with 2016 and 1989
Integer value calculator: Write a program using C++ that acts as a calculator. The program should...
Integer value calculator: Write a program using C++ that acts as a calculator. The program should ask the user for two numbers and an operator. Then the program must print the result using the two input values and the operator. Prompts and input requirements. Ask the user for two integer values and a value that indicates the operation. Likely you will want to choose the normal operators: + - * / %. Output Requirements: Make your output easy to understand,...
Using PHP, design a function that given a Roman numeral (Links to an external site.)Links to...
Using PHP, design a function that given a Roman numeral (Links to an external site.)Links to an external site. in input, is able to compute the modern Hindu–Arabic numeral (Links to an external site.)Links to an external site. system representation (aka, 0123456789). For example: Input Output VI 6 IV 4 MCMXC 1990 IX 9 Be sure to add all the necessary checks and at least one testing function. Try to split the logic in more small functions.
Using PHP, design a function that given a Roman numeral (Wikipedia link.), in input, is able...
Using PHP, design a function that given a Roman numeral (Wikipedia link.), in input, is able to compute the modern Hindu–Arabic numeral (wikipedia link), system representation (aka, 0123456789). For example: Input Output VI 6 IV 4 MCMXC 1990 IX 9 Be sure to add all the necessary checks and at least one testing function. Try to split the logic in more small functions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT