你如何制作计算器?
How do you make a calculator?
我正在制作罗马计算器:输入要求进行 ROMAN 计算(如 I + IV),输出是罗马数字的答案。如果输出超过 1000,则输出“错误”。这是我的代码:
def toRoman (x):
roman= ""
hundreds = x//100
remainder = x%100 # xx=532, d=532//100=5, x=532%100=32
if hundreds == 1:
roman+="C"
elif hundreds == 2:
roman+="CC"
elif hundreds == 3:
roman+="CCC"
elif hundreds == 4:
roman+="CD"
elif hundreds == 5:
roman+="D"
elif hundreds == 6:
roman+="DC"
elif hundreds == 7:
roman+="DCC"
elif hundreds == 8:
roman+="DCCC"
elif hundreds == 9:
roman+="CM"
tens = remainder//10# d=32//10=3, x= 32%10=2
ones = remainder % 10
if tens == 1:
roman+="X"
elif tens == 2:
roman+="XX"
elif tens == 3:
roman+="XXX"
elif tens == 4:
roman+="XL"
elif tens == 5:
roman+="L"
elif tens == 6:
roman+="LX"
elif tens == 7:
roman+="LXX"
elif tens == 8:
roman+="LXXX"
elif tens == 9:
roman+="XC"
if ones == 1:
roman+="I"
elif ones == 2:
roman+="II"
elif ones == 3:
roman+="III"
elif ones == 4:
roman+="IV"
elif ones == 5:
roman+="V"
elif ones == 6:
roman+="VI"
elif ones == 7:
roman+="VII"
elif ones == 8:
roman+="VIII"
elif ones == 9:
roman+="IX"
return roman
def toDecimal (s):
old= 100000
t = 0
v=0
for i in range(0, len(s)):
if s[i] == "I":
v = 1
elif s[i] == "V":
v = 5
elif s[i] == "X":
v = 10
elif s[i] == "L":
v = 50
elif s[i] == "C":
v = 100
elif s[i] == "D":
v = 500
elif s[i] == "M":
v = 1000
if v > old:
t = v-t
else:
t = t + v
old = v
return t
我对如何组合函数和制作计算器有点困惑。谁能帮忙?谢谢
这里有一个可能的方法来缩短你的函数并使它们更具可读性:
H = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']
T = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC']
O = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX']
V = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 100}
def to_roman(x):
return H[x // 100] + T[(x % 100) // 10] + O[(x % 100) % 10]
def to_decimal(x):
result = 0
i = 0
while i < len(x):
s1 = V[x[i]]
if i + 1 < len(x):
s2 = V[x[i+1]]
if s1 >= s2:
result += s1
i += 1
else:
result += s2 - s1
i += 2
else:
result += s1
i += 1
return result
以下是实现计算器的方法:
number1 = to_decimal(input()) # input a Roman number
operator = input() # input +, -, * or /
number2 = to_decimal(input()) # input a Roman number
if operator == '+':
result = number1 + number2
elif operator == '-':
result = number1 - number2
elif operator == '*':
result = number1 * number2
else:
result = number1 // number2
print(f'Result: {to_roman(result)}')
你仍然应该做一些工作来处理特殊情况(结果不是整数的除法、负数、大于 1000 的数字等)。
我正在制作罗马计算器:输入要求进行 ROMAN 计算(如 I + IV),输出是罗马数字的答案。如果输出超过 1000,则输出“错误”。这是我的代码:
def toRoman (x):
roman= ""
hundreds = x//100
remainder = x%100 # xx=532, d=532//100=5, x=532%100=32
if hundreds == 1:
roman+="C"
elif hundreds == 2:
roman+="CC"
elif hundreds == 3:
roman+="CCC"
elif hundreds == 4:
roman+="CD"
elif hundreds == 5:
roman+="D"
elif hundreds == 6:
roman+="DC"
elif hundreds == 7:
roman+="DCC"
elif hundreds == 8:
roman+="DCCC"
elif hundreds == 9:
roman+="CM"
tens = remainder//10# d=32//10=3, x= 32%10=2
ones = remainder % 10
if tens == 1:
roman+="X"
elif tens == 2:
roman+="XX"
elif tens == 3:
roman+="XXX"
elif tens == 4:
roman+="XL"
elif tens == 5:
roman+="L"
elif tens == 6:
roman+="LX"
elif tens == 7:
roman+="LXX"
elif tens == 8:
roman+="LXXX"
elif tens == 9:
roman+="XC"
if ones == 1:
roman+="I"
elif ones == 2:
roman+="II"
elif ones == 3:
roman+="III"
elif ones == 4:
roman+="IV"
elif ones == 5:
roman+="V"
elif ones == 6:
roman+="VI"
elif ones == 7:
roman+="VII"
elif ones == 8:
roman+="VIII"
elif ones == 9:
roman+="IX"
return roman
def toDecimal (s):
old= 100000
t = 0
v=0
for i in range(0, len(s)):
if s[i] == "I":
v = 1
elif s[i] == "V":
v = 5
elif s[i] == "X":
v = 10
elif s[i] == "L":
v = 50
elif s[i] == "C":
v = 100
elif s[i] == "D":
v = 500
elif s[i] == "M":
v = 1000
if v > old:
t = v-t
else:
t = t + v
old = v
return t
我对如何组合函数和制作计算器有点困惑。谁能帮忙?谢谢
这里有一个可能的方法来缩短你的函数并使它们更具可读性:
H = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']
T = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC']
O = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX']
V = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 100}
def to_roman(x):
return H[x // 100] + T[(x % 100) // 10] + O[(x % 100) % 10]
def to_decimal(x):
result = 0
i = 0
while i < len(x):
s1 = V[x[i]]
if i + 1 < len(x):
s2 = V[x[i+1]]
if s1 >= s2:
result += s1
i += 1
else:
result += s2 - s1
i += 2
else:
result += s1
i += 1
return result
以下是实现计算器的方法:
number1 = to_decimal(input()) # input a Roman number
operator = input() # input +, -, * or /
number2 = to_decimal(input()) # input a Roman number
if operator == '+':
result = number1 + number2
elif operator == '-':
result = number1 - number2
elif operator == '*':
result = number1 * number2
else:
result = number1 // number2
print(f'Result: {to_roman(result)}')
你仍然应该做一些工作来处理特殊情况(结果不是整数的除法、负数、大于 1000 的数字等)。