python简单计算器

Posted hhjwqh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python简单计算器相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018-01-15 20:40
# @Author : hhj
# @Site :
一、计算器一
import re


def operator_update(formula): # 对formula公式进行 去除空字符,更新运算符处理
formula = formula.replace(" ", "") # 去除空字符
formula = formula.replace("+-", "-")
formula = formula.replace("--", "+")
return formula
def calc_muldiv(calc_list):
‘‘‘
计算公式里面的乘除
:param formula: 列表
:return:
‘‘‘
for index, element in enumerate(calc_list):
if "*" in element or "/" in element:
operators = re.findall("[*/]", element)
sub_calc_list = re.split("[*/]", element)
num = None
for i, e in enumerate(sub_calc_list):
if num:
if operators[i - 1] == "*":
num *= float(e)
elif operators[i - 1] == "/":
num /= float(e)
else:
num = float(e)
calc_list[index] = num
return calc_list


def calc_plumin(operators, num_list):
‘‘‘
计算列表数字的加减
:param operators: 运算符列表
:param num_list: 进行运算的数字列表
:return: 返回计算结果
‘‘‘
num = None
for i, e in enumerate(num_list):
if num:
if operators[i - 1] == "+":
num += float(e)
elif operators[i - 1] == "-":
num -= float(e)
else:
num = float(e)
return num
def merge(plus_minus_operator, multiply_divide_list): #乘除中含负数处理
# ‘‘‘
# 把列表中这样的形式‘2*‘ ‘-3*‘ ‘5/3*‘ ‘4/2‘合并到一块
# :param formula_list:
# :return:
# ‘‘‘
for index, element in enumerate(multiply_divide_list):
if element.endswith("*") or element.endswith("/"):
multiply_divide_list[index] = element + plus_minus_operator[index] + multiply_divide_list[index + 1]
del multiply_divide_list[index + 1]
del plus_minus_operator[index]
return merge(plus_minus_operator, multiply_divide_list)
return plus_minus_operator, multiply_divide_list

def fushu_cl(formula): #字符串负数处理
plus_minus_operator = re.findall("[+-]", formula) # 拿出+和-
multiply_divide_list = re.split("[+-]", formula) # 以+和-为分隔符,拿出值
if multiply_divide_list[0] == "": # multiply_divide_list列表第一个字符为空的话,表示第一个数字为负号
multiply_divide_list[1] = "-" + multiply_divide_list[1]
del plus_minus_operator[0]
del multiply_divide_list[0]
res1 = merge(plus_minus_operator, multiply_divide_list)
else:
res1 = merge(plus_minus_operator, multiply_divide_list)
return res1

def mutilpy_and_dividend(formula): #运算函数
print("运算",formula)
formula = operator_update(formula)
res1 = fushu_cl(formula)
operator_list=res1[0]
calc_list=res1[1]
calc_list=calc_muldiv(calc_list)
res=calc_plumin(operator_list,calc_list)
return res

def calc(formula):
while True: #有值时循环
formula_sub= re.search("\([^()]+\)",formula) #找出最里层无括号的计算式
if formula_sub: #如果找到值
# print(m.group())
sub_formula=formula_sub.group().strip("()") #去掉括号
# print (sub_formula)
res = mutilpy_and_dividend(sub_formula)
formula= formula.replace(formula_sub.group(), str(res))
print("-----")
# print(sub_res)
else:
res= mutilpy_and_dividend(formula)
print (res)
exit()


if __name__=="__main__": #主程序入口
# formula = "1 - 2 * ( (60-30 +(-9-2- 5-2*-3-5/3-40*4/2-3/5+6*3) * (-9-2-5-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) -(-4*3)/ (16-3*2) )"
formula=input("请输入计算式:")
res=calc(formula)
print(res)


二、计算器2

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re

def operator_update(formula):
# 对formula公式进行 去除空字符,更新运算符处理
formula = formula.replace(" ", "") # 去除空字符
formula = formula.replace("+-", "-")
formula = formula.replace("--", "+")
return formula


def calc_muldiv(formula_list):
‘‘‘
计算公式里面的乘除
:param formula: 列表
:return:
‘‘‘
for index, element in enumerate(formula_list):
if "*" in element or "/" in element:
operators = re.findall("[*/]", element)
calc_list = re.split("[*/]", element)
num = None
for i, e in enumerate(calc_list):
if num:
if operators[i - 1] == "*":
num *= float(e)
elif operators[i - 1] == "/":
num /= float(e)
else:
num = float(e)
formula_list[index] = num
return formula_list


def calc_plumin(operators, num_list):
‘‘‘
计算列表数字的加减
:param operators: 运算符列表
:param num_list: 进行运算的数字列表
:return: 返回计算结果
‘‘‘
num = None
for i, e in enumerate(num_list):
if num:
if operators[i - 1] == "+":
num += float(e)
elif operators[i - 1] == "-":
num -= float(e)
else:
num = float(e)
return num

def merge(plus_minus_operator, multiply_divide_list):
‘‘‘
把列表中这样的形式‘2*‘ ‘-3*‘ ‘5/3*‘ ‘4/2‘合并到一块
:param formula_list:
:return:
‘‘‘
for index, element in enumerate(multiply_divide_list):
if element.endswith("*") or element.endswith("/"):
multiply_divide_list[index] = element + plus_minus_operator[index] + multiply_divide_list[index + 1]
del multiply_divide_list[index + 1]
del plus_minus_operator[index]
return merge(plus_minus_operator, multiply_divide_list)
return plus_minus_operator, multiply_divide_list

def bracket_calc(formula):
‘‘‘
对括号最内层的formula公式进行计算
:param formula:
:return:
‘‘‘
formula = re.sub("[()]", "", formula) # 去除两边的()
formula = operator_update(formula)
plus_minus_operator = re.findall("[+-]", formula) # 列表 ‘+‘ ‘-‘ 运算符
multiply_divide_list = re.split("[+-]", formula) # 列表 有‘*‘ ‘/‘
if multiply_divide_list[0] == "": # multiply_divide_list列表第一个字符为空的话,表示一个数字为负号
multiply_divide_list[1] = "-" + multiply_divide_list[1]
del plus_minus_operator[0]
del multiply_divide_list[0]
res = merge(plus_minus_operator, multiply_divide_list)
plus_minus_operator = res[0] # 列表 ‘+‘ ‘-‘ 运算符 进行合并处理
multiply_divide_list = res[1]
plus_minus_list = calc_muldiv(multiply_divide_list) # 生成只进行加减运算的列表
res = calc_plumin(plus_minus_operator, plus_minus_list)
return res


def calculate(formula):
# ‘‘‘计算程序主入口, 主要逻辑是先计算拓号里的值,算出来后再算乘除,再算加减‘‘‘
while True:
formula_depth = re.search("\([^()]+\)", formula)
if formula_depth:
formula_depth = formula_depth.group()
res = bracket_calc(formula_depth)
formula = formula.replace(formula_depth, str(res))
print("\33[34;1m%s\33[0m" % (formula))
else:
res = bracket_calc(formula)
print("\33[31;1m结果:%s\33[0m" % (res))
exit()


if __name__ == ‘__main__‘:
formula = "1 - 2 * ( (60-30 +(-9-2- 5-2*-3-5/3-40*4/2-3/5+6*3) * (-9-2-5-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) -(-4*3)/ (16-3*2) )"
# formula = input("请输入计算式:")
calculate(formula)



































































































































































































































以上是关于python简单计算器的主要内容,如果未能解决你的问题,请参考以下文章

golang代码片段(摘抄)

GLSL片段着色器-绘制简单的粗曲线

Python学习总结

ruby 这个片段是一种简单的ruby方式,用于计算在Twitter上共享指定网址的次数。

30 段 Python 实用代码

PHP 精度计算引发的灾难性Bug