Python 3实现简单计算器功能

用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;

这代码是刚接触时候写的。。自己都不忍心看下去了,有空再改改吧

#!//bin/python
# -*- coding: UTF-8 -*-
import sys 
'''
date : 2017-09-08
@author: vassago
'''

#get and update the data
def process_args(args):
    #合并,组成正确的小数
    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]
    #合并,组成正确的多位数
    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
    return args

def get_nibolan_list(args):
    #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=[]
    while(len(stack2)!=0):
        nibolan.append(stack2.pop())

    #print(nibolan)
    return nibolan

def process_nibolan(nibolan):
    #output the answer
    stack,fla=[],1
    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
    return stack[0]

if __name__ == '__main__':
    string=""
    for x in sys.argv:
        if(x=="main.py" or x==' '):
            continue
        string+=x

    args=list(string)
    print (process_nibolan(get_nibolan_list(process_args(args)))

测试时��
采用命令行输入

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

输出:

4.0

猜你喜欢

转载自www.linuxidc.com/Linux/2018-01/150541.htm