美文网首页
计算器专题

计算器专题

作者: madao756 | 来源:发表于2020-05-30 19:10 被阅读0次

0X00 基础知识

后缀表达式的计算

150. 逆波兰表达式求值

后缀表达式就是这么简单粗暴,碰到一个符号,就从栈中拿出两个数字计算,再重新放入栈中。

from operator import add, sub, mul, truediv

class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        ops = {'+': add, '-': sub, '*': mul, '/': truediv}
        nums = []
        for x in tokens:
            if x not in ops: 
                nums.append(int(x))
                continue
            r, l = nums.pop(), nums.pop()
            nums.append(int(ops[x](l, r)))
        
        return nums[0]

中缀表达式转换成后缀表达式

包含:小数、括号、加减乘除、正负号(以及 ------------10 这种情况)

首先我们得了解一下运算符的优先级(括号另外处理)

加减 < 乘除 < 符号

如果我们要记住这个算法,只需要了理解:

有这么一个栈只能严格递增保存符号,一旦有新的符号小于等之前的符号,那么之前的符号就会被 pop 出来,表示这些符号会先执行

看着很难,其实没这么难,耐心一点

772. 基本计算器 III

from operator import add, sub, mul, truediv
pri_map = {
    '+' : 1,
    '-':  1,
    '*': 2,
    '/': 2,
    's+': 3,
    's-': 3
}

def postfix(s):
    ops = []
    ans = []
    i = 0
    while i < len(s):
        c = s[i]
        if c == '(':
            ops.append(c)
        elif c == ')':
            while ops and ops[-1] != '(':
                ans.append(ops.pop())
            ops.pop()
        elif c in pri_map:
            if c in "+-" and (not i or s[i-1] == '(' or s[i-1] in pri_map):
                 c = 's' + c
            else:
                while ops and ops[-1] != '(' and pri_map[ops[-1]] >= pri_map[c]:
                    ans.append(ops.pop())
            ops.append(c)
        elif c.isdigit():
            d = ""
            j = i
            while j < len(s) and (s[j].isdigit() or s[j] == "."):
                d += s[j]
                j += 1
            i = j - 1
            if '.' not in d:
                ans.append(int(d))
            else:
                ans.append(float(d))
        i += 1
    return ans + ops[::-1]

class Solution:
    def calculate(self, s: str) -> int:
        s = "".join(s.split())
        post = postfix(s)
        # print(post)
        ops = {'+': add, '-': sub, '*': mul, '/': truediv}
        ans = []
        for x in post:
            if x not in pri_map:
                ans.append(x)
            else:
                if x in ['s+', 's-']:
                    t = ans.pop()
                    ans.append(t if t =='s+' else -t)
                else:
                    r, l = ans.pop(), ans.pop()
                    ans.append(ops[x](l, r))
        
        return ans[0]

0X01 相关题目

相关文章

  • 计算器专题

    0X00 基础知识 后缀表达式的计算 150. 逆波兰表达式求值 后缀表达式就是这么简单粗暴,碰到一个符号,就从栈...

  • iOS简单项目 — 计算器

    计算器所需求功能 计算器所用模式:MVC计算器所用语言:Swift计算器所参考课程:斯坦福 第一步:构造计算器界面...

  • 【敲黑板】考试计算器技巧

    方案一 普通计算器+科学计算器(让带计算器) 方案二 搜狗输入法V+计算大法(不让带计算器) 快捷操作 加:+ ...

  • 2018-11-11

    html部分 计算器 计算器 × □ - CE ...

  • 房贷计算器

    最近房产版升级,需要重新制作房贷计算器的功能,主要参考了 房天下房贷计算器和安居客房贷计算器,两大平台的计算器。 ...

  • 2018-11-01for/while/计算器

    计算器 计算器 window.onload = function(){va...

  • 使用WGIS的字段计算器

    WGIS的字段计算器(Field Calculator)跟其他桌面GIS软件的字段计算器类似,计算器的规则遵循Ja...

  • C#第一次作业02

    #作业要求 乘法计算器 #程序 namespace 乘法计算器 { public partial class ...

  • 项目-HTML,CSS,JavaScript计算器

    整个计算器分为四部分:1.构建计算器的框架;2.为计算器添加样式;3.实现计算、回删、清零功能;4.为计算器添加日...

  • ???

    2019.6.28 昨天考试摁计算器,9999999的999999次方计算器算不出来。 看来计算器也不是万能的。 ...

网友评论

      本文标题:计算器专题

      本文链接:https://www.haomeiwen.com/subject/zmphzhtx.html