Created
April 22, 2017 16:56
-
-
Save joyc/004c0eaca3e56c615d9116cd5af0caaf to your computer and use it in GitHub Desktop.
calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
# @Time : 2017/04/23 1:55 | |
# @Author : Rusher | |
# @File : calculator2.py | |
# @Software: PyCharm | |
# 将输入的字符串格式统一转换成列表,将数字、符号、括号都成一个独立元素, | |
# 如: 12+3*(14+5) ----> ["12","+","3","*","(","14","+","5",")"] | |
def format_input(input_re_value): | |
input_re_value = input_re_value.replace(" ", "") | |
format_list = [] | |
for i in input_re_value: | |
format_list.append(i) | |
snum = 0 | |
while 1: | |
try: | |
if format_list[snum].isnumeric(): | |
if format_list[snum + 1].isnumeric(): | |
format_list[snum] = format_list[snum] + format_list[snum + 1] | |
format_list.pop(snum + 1) | |
else: | |
snum += 1 | |
else: | |
snum += 1 | |
except IndexError: | |
return format_list | |
break | |
# 计算没有括号的列表的值。 | |
def comput(re_value): | |
while "*" in re_value or "/" in re_value: | |
for i, j in enumerate(re_value): | |
if j == "*": | |
re_cheng = float(re_value[i - 1]) * float(re_value[i + 1]) | |
re_value.pop(i - 1) | |
re_value.pop(i - 1) | |
re_value.pop(i - 1) | |
re_value.insert(i - 1, re_cheng) | |
re_value.insert(0, 0) | |
re_value.insert(0, 0) | |
if j == "/": | |
re_chu = float(re_value[i - 1]) / float(re_value[i + 1]) | |
re_value.pop(i - 1) | |
re_value.pop(i - 1) | |
re_value.pop(i - 1) | |
re_value.insert(i - 1, re_chu) | |
re_value.insert(0, 0) | |
re_value.insert(0, 0) | |
while "+" in re_value or "-" in re_value: | |
for i, j in enumerate(re_value): | |
if j == "+": | |
re_jia = float(re_value[i - 1]) + float(re_value[i + 1]) | |
re_value.pop(i - 1) | |
re_value.pop(i - 1) | |
re_value.pop(i - 1) | |
re_value.insert(i - 1, re_jia) | |
re_value.insert(0, 0) | |
re_value.insert(0, 0) | |
if j == "-": | |
re_jian = float(re_value[i - 1]) - float(re_value[i + 1]) | |
re_value.pop(i - 1) | |
re_value.pop(i - 1) | |
re_value.pop(i - 1) | |
re_value.insert(i - 1, re_jian) | |
re_value.insert(0, 0) | |
re_value.insert(0, 0) | |
return re_value[-1] | |
# 通过循环,先查找列表的第一个")",然后在")"位置向列表前找到第一个"("所在位置, | |
# 并将其中的元素提取出来,生成新的列表,交给comput计算, | |
# 返回值再插入列表当中,继续循环,直到没有"("为止 | |
def bracket_filter(list1): | |
while "(" in list1: | |
i = list1.index(")") | |
for m in range(i, -1, -1): | |
if list1[m] == "(": | |
list_new = list1[(m + 1):i] | |
re_res = comput(list_new) | |
list1.insert(m, str(re_res)) | |
for item1 in range(i + 1 - m): | |
list1.pop(m + 1) | |
break | |
return comput(list1) | |
input_sn = input("pls input:") | |
f_re = format_input(input_sn) | |
result = bracket_filter(f_re) | |
print("The result is:", result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment