python3实现计算器基本功能

时间:2021-12-13 17:31:00

用Python3写一个计算器:实现基本功能如+,-,*,/,^,
要注意的是:输入含有括号( ),小数点 .

思路就是逆波兰表达式的算法:

从中缀式的左端开始逐个读取字符x,逐序进行如下步骤:

      1.若x是操作数,将x直接压入栈s2;

      2.若x是运算符,则分情况讨论:

          若x是’(‘,则直接压入栈s1;

          若x是’)’,则将距离栈s1栈顶的最近的’(‘之间的运算符,逐个出栈,依次压入栈s2,此时抛弃’(‘;

          若x是除’(‘和’)’外的运算符,则再分如下情况讨论:

              若当前栈s1的栈顶元素为’(‘,则将x直接压入栈s1;

              若当前栈s1的栈顶元素不为’(‘,则将x与栈s1的栈顶元素比较,若x的优先级大于栈s1栈顶运算符优先级,则将x直接压入栈s1。否者,将栈s1的栈顶运算符弹出,压入栈s2中,直到栈s1的栈顶运算符优先级别低于(不包括等于)x的优先级,或栈s2的栈顶运算符为’(‘,此时再则将x压入栈s1;

# -*- coding: UTF-8 -*-
import sys, getopt

#get and update the data
string=""
for x in sys.argv:
if(x=="main.py" or x==' '):
continue
string+=x

args=list(string)

while(1):
try:
x=args.index('.')
except:
break
args[x-1]+=args[x]
args[x-1]+=args[x+1]
del args[x]
del args[x]
#7.12bug更新,多位数没处理
llen =len(args)
i=1
while(i<llen):
if(args[i-1]=='(' or args[i-1]==')' or args[i-1]=='+' or args[i-1]=='-' or args[i-1]=='*' or args[i-1]=='/' or args[i-1]=='^'):
i+=1
continue
if(args[i]!='(' and args[i]!=')' and args[i]!='+' and args[i]!='-' and args[i]!='*' and args[i]!='/' and args[i]!='^'):
args[i-1]+=args[i]
del args[i]
llen-=1
i-=1
i+=1


#get nibolan list
pri={'(':0,')':0, '+':1,'-':1, '*':2,'/':2,'^':3}
stack1=[]
stack2=[]

for x in args:
if(x=='main.py' or x==' '):
continue
if(x=='('):
stack1.append(x)
elif(x==')'):
top=stack1.pop()
while(top!='('):
stack2.append(top)
top=stack1.pop()
elif(x=='+' or x=='-' or x=='*' or x=='/' or x=='^'):
if(len(stack1)==0):
stack1.append(x)
else:
top1=stack1.pop()
if(pri[x]>pri[top1]):
stack1.append(top1)
stack1.append(x)
else:
while(1):
if(pri[x]>pri[top1]):
stack1.append(top1)
break
stack2.append(top1)
if(len(stack1)==0):
break
top1=stack1.pop()
stack1.append(x)
else:
stack2.append(x)

while(len(stack1)!=0):
stack2.append(stack1.pop())

nibolan=[]
stack=[]
fla=1
while(len(stack2)!=0):
nibolan.append(stack2.pop())

#print(nibolan)

#output the answer
while(1):
top=nibolan.pop()
if(top=='+' or top=='-' or top=='*' or top=='/' or top=='^'):
try:
y=float(stack.pop())
x=float(stack.pop())
except IndexError:
print('FORMAT ERROR')
fla=0
break
except ValueError:
print('INPUT ERROR')
fla=0
break

try:
if(top=='+'):
stack.append(x+y)
if(top=='-'):
stack.append(x-y)
if(top=='*'):
stack.append(x*y)
if(top=='/'):
stack.append(x/y)
if(top=='^'):
stack.append(x**y)
except ValueError :
print('Value Error')
fla=0
break

while(len(stack)!=0):
nibolan.append(stack.pop())
else:
stack.append(top)
if(len(nibolan)==0):
break
if(fla):
print(stack[0])

测试时:
采用命令行输入

python main.py "1+2^2-3*(2-1)/3"

输出:

4.0