- 定义一个Calculator类表示计算器,在该类中定义四个方法add(x, y)、subtract(x, y)、multiply(x, y)、divide(x, y)四个方法分别实现两个数的加法、减法、乘法和除法。
创建Claculator对象,提示用户输入两个操作数和运算符(+,-,*,/),调用对应的方法得到计算结果。请设计一个友好的信息交互方式。
#
# main.py
# Calculator
#
# Created by Mewlan Musajan on 4/29/21
# Bug fixed on 2/2/24.
#
from decimal import Decimal, DecimalException
class Calculator():
first_number = 0
last_number = 0
result = ''
def add(self):
print("%s + %s = %s" % (self.first_number, self.last_number, self.first_number + self.last_number))
def subtract(self):
print("%s - %s = %s" % (self.first_number, self.last_number, self.first_number - self.last_number))
def multiply(self):
print("%s * %s = %s" % (self.first_number, self.last_number, self.first_number * self.last_number))
def divide(self):
if self.first_number != 0:
print("