题目

需求::

    设计一个计算器类,可以进行加、减、乘、除计算。

代码

# @Time:    2021/10/15 13:02
# @Auth:    君若(QQ1813774883)

class myCalc:
    def __init__(self, one, two):
        self.one = one
        self.two = two

    def addition(self, Retain):
        return round(self.one + self.two, Retain)

    def subtraction(self, Retain):
        return round(self.one - self.two, Retain)

    def multiplication(self, Retain):
        return round(self.one * self.two, Retain)

    def division(self, Retain):
        return round(self.one / self.two, Retain)


while True:
    get_num1 = input("请输入第一个数字:")
    opera = input("请输入运算符:")
    get_num2 = input("请输入第二个数字:")
    get_retain = input("请输入保留小数位数:")
    num1 = float(get_num1)
    num2 = float(get_num2)
    retain = int(get_retain)
    result = 0.00
    if opera == "+":
        result = myCalc(num1, num2).addition(retain)
        print("输出结果是:", result)
    elif opera == "-":
        result = myCalc(num1, num2).subtraction(retain)
        print("输出结果是:", result)
    elif opera == "*":
        result = myCalc(num1, num2).multiplication(retain)
        print("输出结果是:", result)
    elif opera == "/":
        result = myCalc(num1, num2).division(retain)
        print("输出结果是:", result)
    else:
        print("运算符错误!请重新输入")

最后修改:2021 年 10 月 16 日
如果觉得我的文章对你有用,请随意赞赏