简体   繁体   English

你如何制作计算器?

[英]How do you make a calculator?

I'm making a roman calculator: An input asks for a ROMAN calculation (Like I + IV) and the output is the answer in roman numerals.我正在制作一个罗马计算器:输入要求进行 ROMAN 计算(如 I + IV),而 output 是罗马数字的答案。 If the output if more than 1000, the output is "error."如果 output 超过 1000,则 output 为“错误”。 Here's my code:这是我的代码:

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

I'm kind of stuck on how to combine the functions and make the calculator.我有点卡在如何组合函数和制作计算器上。 Can anyone help?谁能帮忙? Thanks.谢谢。

Here is a possible way to shorten your functions and make them more readable:这是一种缩短函数并使其更具可读性的可能方法:

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

Here is how you can implement your calculator:以下是如何实现您的计算器:

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)}')

You should still do some work to handle special cases (divisions whose result is not an integer, negative numbers, numbers higher than 1000 etc).您仍然应该做一些工作来处理特殊情况(结果不是 integer、负数、大于 1000 的数字等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM