In: Computer Science
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". --------------------------------------
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:



