Python 3实现简单计算器功能

栏目: Python · 发布时间: 8年前

内容简介:Python 3实现简单计算器功能

Python 3写一个计算器:实现基本功能如+,-,*,/,^,

要注意的是:输入含有括号( ),小数点 .

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

从中缀式的左端开始逐个读取字符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

本文永久更新链接地址 http://www.linuxidc.com/Linux/2018-01/150541.htm


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Probability and Computing

Probability and Computing

Michael Mitzenmacher、Eli Upfal / Cambridge University Press / 2005-01-31 / USD 66.00

Assuming only an elementary background in discrete mathematics, this textbook is an excellent introduction to the probabilistic techniques and paradigms used in the development of probabilistic algori......一起来看看 《Probability and Computing》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具