KiKi实现一个简单计算器,实现两个数的“加减乘除”运算,用户从键盘输入算式“操作数1运算符操作数2”,计算并输出表达式的值,如果输入的运算符号不包括在(+、-、*、/)范围内,输出“Invalid operation!”。当运算符为除法运算,即“/”时。如果操作数2等于0.0,则输出“Wrong!Division by zero!”
数据范围:字符串长度满足 ,保证运算符是一个char类型字符。
输入一行字符串,操作数1+运算符+操作数2 (其中合法的运算符包括:+、-、*、/)。
输出为一行。
如果操作数和运算符号均合法,则输出一个表达式,操作数1运算符操作数2=运算结果,各数小数点后均保留4位,数和符号之间没有空格。
如果输入的运算符号不包括在(+、-、*、/)范围内,输出“Invalid operation!”。当运算符为除法运算,即“/”时。
如果操作数2等于0.0,则输出“Wrong!Division by zero!”。
1.0+3.0
1.0000+3.0000=4.0000
1.0;4.0
Invalid operation!
44.0/0.0
Wrong!Division by zero!
x = input() a = b = c = m = 0 for i in x: if i in ('+', '-', '*', '/'): m += x.index(i) a = float(x[:m]) b = float(x[m+1:]) c = x[m] if c not in ('+', '-', '*', '/'): print('Invalid operation!') elif c == '+': print(f'{a:.4f}+{b:.4f}={a + b:.4f}') elif c == '-': print(f'{a:.4f}-{b:.4f}={a - b:.4f}') elif c == '*': print(f'{a:.4f}*{b:.4f}={a * b:.4f}') elif c == '/': if b != 0: print(f'{a:.4f}/{b:.4f}={a / b:.4f}') else: print('Wrong!Division by zero!')
num = input() flag = 0 for i in num: if i in ['+', '-', '*', '/']: flag = 1 a, b = map(float, num.split(i)) if i == '+': c = a + b elif i == '-': c = a - b elif i == '*': c = a * b elif i == '/': if b == 0: print('Wrong!Division by zero!') break c = a / b print('{:.4f}{}{:.4f}={:.4f}'.format(a, i, b, c)) break if flag == 0: print('Invalid operation!')
expr = input() # 找到提取运算符位置 for i in range(len(expr)): if expr[i] not in '0123456789.': op_idx = i break op = expr[op_idx] a = float(expr[:op_idx]) b = float(expr[op_idx+1:]) # 检查运算符 if op not in '+-*/': print('Invalid operation!') elif op == '/' and b == 0: print('Wrong!Division by zero!') elif op == '+': print('{:.4f}+{:.4f}={:.4f}'.format(a,b,a+b)) elif op == '-': print('{:.4f}-{:.4f}={:.4f}'.format(a,b,a-b)) elif op == '*': print('{:.4f}*{:.4f}={:.4f}'.format(a,b,a*b)) elif op == '/': print('{:.4f}/{:.4f}={:.4f}'.format(a,b,a/b))方法2:
expr = input() try: # 寻找运算符 for op in expr: if not(op.isnumeric()&nbs***bsp;op == '.'): a, b = expr.split(op) res = eval(expr) print('{:.4f}{}{:.4f}={:.4f}'.format(eval(a), op, eval(b), res)) break except ZeroDivisionError: print("Wrong!Division by zero!") except SyntaxError: print("Invalid operation!")
import re regex = re.compile(r"([0-9]+\.{0,1}[0-9]*)([\+\-\*/])([0-9]+\.{0,1}[0-9]*)") t = input() res = regex.match(t) try: a = float(res.group(1)) b = float(res.group(3)) sgn = res.group(2) print("{:.4f}{}{:.4f}={:.4f}".format(a, sgn, b, eval(t))) except ZeroDivisionError: print("Wrong!Division by zero!") except: print("Invalid operation!")
# 核心代码 # eval把字符数转为整形、浮点型或复数。 n = input() if '+'in n: a,b = n.split('+') a = eval(a) b= eval(b) print(f"{a:.4f}+{b:.4f}={a+b:.4f}") elif '-'in n: a,b = n.split('-') a = eval(a) b= eval(b) print(f"{a:.4f}-{b:.4f}={a - b:.4f}") elif '*'in n: a,b = n.split('*') a = eval(a) b= eval(b) print(f"{a:.4f}*{b:.4f}={a * b:.4f}") elif '/'in n: a,b = n.split('/') c = "/" if eval(b) == 0: print("Wrong!Division by zero!") else: a = eval(a) b = eval(b) print(f"{a:.4f}/{b:.4f}={a / b:.4f}") else: print("Invalid operation!")
def calculator(x): for symb in x: if symb in ['+', '-', '*', '/']: a, b = map(float, x.split(symb)) if symb == '/' and b == 0: print('Wrong!Division by zero!') else: print('{:.4f}{}{:.4f}={:.4f}'.format(a, symb,b, eval(x))) break else: print('Invalid operation!') while(1): try: calculator(input()) except: breakfor与else结合,如果没有跑break才会进入else
while True: try: expr = input() for i in expr: if i.isnumeric() or i == '.': pass else: x, y = expr.split(i) res = eval(expr) print(f'{eval(x):.4f}{i}{eval(y):.4f}={res:.4f}') except ZeroDivisionError as e: print("Wrong!Division by zero!") except SyntaxError as e: print("Invalid operation!") finally: break