2 回答

TA贡献1772条经验 获得超5个赞
import re
class OPERATION(object):
def __init__(self, a, b,ys):
self.a = a
self.b = b
self.ys=ys
def addition(self):
result = self.a + self.b
print(' = %s' % result)
return result
def subtraction(self):
result = self.a - self.b
print(' = %s' % result)
return result
def multiplication (self):
result = self.a * self.b
print(' = %s' % result)
return result
def division(self):
if self.b==0:
return print("输入有误")
else:
result = self.a / self.b
print(' = %s' % result)
return result
def operation(self):
if self.ys is '+':
OPERATION.addition(self)
elif self.ys is '-':
OPERATION.subtraction(self)
elif self.ys is '*':
OPERATION. multiplication (self)
elif self.ys is '/':
OPERATION.division(self)
else:
print("暂时没有实现这种运算")
def main():
print("请输要计算的算式,如 a+b 按回车键查看结果,输入exit退出")
while True:
str = input(">>")
if str =='exit':
break;
else:
ret = re.match(r'^(\d+)([\+\-\*/]+)(\d+)', str)
if ret:
numa = int(ret.group(1))
operationalCharacter = ret.group(2)
numb = int(ret.group(3))
print(numa, operationalCharacter, numb,end='')
yunsuan = OPERATION(numa, numb,operationalCharacter)
yunsuan.operation()
else:
print("请检查输入是否正确")
if __name__=='__main__':
main()
运行结果
添加回答
举报