标签: rpn

生成所有可能的"唯一"RPN(反向波兰表示法)表达式

我想在Python中生成所有可能的RPN(反向波兰表示法)表达式,它们使用输入列表中的字母(例如['a', 'b', 'c'])并包含运算符['+', '-', '*', '/'].

我的想法是我们可以在当前表达式中添加元素,直到出现以下情况之一:要么我们使用了所有字母,要么表达式已完成(即我们无法添加更多运算符).

所以我写了以下函数:

1)

'''
The function returns True if we can add operator to current expression:
we scan the list and add +1 to counter when we meet a letter
and we add -1 when we meet an operator (it reduces
last two letters into 1 (say ab+ <--> a + b)
''' 
def can_add_operator(string_):
    n = 0
    for letter in string_:
        if letter not in ['+', '-', …
Run Code Online (Sandbox Code Playgroud)

python algorithm recursion rpn postfix-notation

11
推荐指数
2
解决办法
786
查看次数

转换反向波兰表示法

当使用C++或C#时,有没有办法将反向波兰表示法解释为"正常"数学符号?我在一家工程公司工作,所以他们偶尔会使用RPN,我们需要一种方法来转换它.有什么建议?

c# c++ rpn

8
推荐指数
1
解决办法
1万
查看次数

处理一元减去分流码算法

在将中缀表达式转换为后缀表达式时,是否有更好的方法来处理一元" - "?

显而易见的是每个一元" - "前缀为0.是否有人知道更好的实现?谢谢!

algorithm rpn shunting-yard

8
推荐指数
1
解决办法
4198
查看次数

数学解析的基于堆栈的表达式评估的效率

出于学术目的,我必须编写一个绘制用户输入表达式的应用程序,如:f(x)= 1 - exp(3 ^(5*ln(cosx))+ x)

我选择编写解析器的方法是使用Shunting-Yard算法转换RPN中的表达式,将原始函数如"cos"视为一元运算符.这意味着上面写的函数将被转换为一系列令牌,如:

1, x, cos, ln, 5, *,3, ^, exp, -
Run Code Online (Sandbox Code Playgroud)

问题是绘制我必须要评估它的函数很多次,因此对每个输入值应用堆栈评估算法将是非常低效的.我怎么解决这个问题?我是否必须忘记RPN的想法?

math performance parsing rpn shunting-yard

7
推荐指数
1
解决办法
2283
查看次数

这种Python后缀符号(反向波兰表示法)解释器可以更有效和准确吗?

这是一个Python后缀符号解释器,它使用堆栈来计算表达式.是否可以使此功能更有效和准确?

#!/usr/bin/env python


import operator
import doctest


class Stack:
    """A stack is a collection, meaning that it is a data structure that 
    contains multiple elements.

    """

    def __init__(self):
        """Initialize a new empty stack."""
        self.items = []       

    def push(self, item):
        """Add a new item to the stack."""
        self.items.append(item)

    def pop(self):
        """Remove and return an item from the stack. The item 
        that is returned is always the last one that was added.

        """
        return self.items.pop()

    def is_empty(self):
        """Check whether the stack is …
Run Code Online (Sandbox Code Playgroud)

python rpn postfix-notation

7
推荐指数
1
解决办法
6407
查看次数

R中反波兰符号的评价

在R中评估RPN表示法的最有效算法是什么?

这是一个问题:假设我们有

c("4", "13", "5", "/", "+") -> (4 + (13 / 5)) -> 6
Run Code Online (Sandbox Code Playgroud)

如何编写评估任何输入RPN的通用函数?

R有堆栈数据结构吗?

谢谢你的提示

parsing rpn r

6
推荐指数
1
解决办法
804
查看次数

可以通过正则表达式导出RPN(反向波兰表示法)或后缀表示法

我想知道我是否可以定义一个正则表达式来检查给定的输入是否与RPN表达式匹配,即给定的输入是否有效?

不幸的是,我对Regex不是很熟悉,所以我想知道是否可以定义一个正则表达式来验证postfix的输入.

非常感谢塔兹

regex rpn

5
推荐指数
1
解决办法
1214
查看次数

从抽象语法树评估表达式

我有一个抽象的语法树,我用反向抛光表示法表达.树的节点都是字符串.

#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

struct snode 
{
  char *datum;
  struct snode* bottom;
};

struct tnode
{
  char *datum;
  struct tnode* left;
  struct tnode*right;
};

struct snode* 
push(struct snode* stack, char *x) {
  struct snode *S = (struct snode*)malloc(sizeof(struct snode));
  S->datum = (char *)malloc(strlen(x) + 1);
  strcpy(S->datum, x);
  S->bottom = stack;
  return S;
}

void
pop(struct snode **stack) {
  struct snode *S;
  if (*stack == NULL)
    return;

  S = (*stack)->bottom;
  free(*stack);
  *stack = S;
}

char …
Run Code Online (Sandbox Code Playgroud)

c rpn abstract-syntax-tree

5
推荐指数
1
解决办法
857
查看次数

5
推荐指数
2
解决办法
7331
查看次数

逆波兰表示法中的变长运算符(后缀)

背景:在传统的 Reverse Polish Notation 中,所有的操作符都必须有固定的长度,这使得 RPN 可以很容易地被代码评估和操作,因为每个标记、表达式和子表达式都是“自包含”的,以至于人们可以盲目地将yin替换x?y?*y?1?+to get x?y?1?+?*,这是另一个有效的表达式,它完全符合您的要求。这是一个带有命名变量支持的简单 RPN 计算器的交互式演示。请注意,演示试图展示算法的要点;它们与生产代码无关或不代表生产代码。

var rpn = prompt("Please enter RPN string, where each token is " +
  "separated by a space", "x 1 x + * 2 /").trim().split(/\s+/);

var stack = [], variables = [], values = [];
for (let i = 0, len = rpn.length|0; i < len; i=i+1|0) {
    if (/^\d*(\.\d*)?$/.test(rpn[i]) && rpn[i] !== "") {
        stack.push( rpn[i] );
    } else if (/^[a-z]$/i.test(rpn[i])) …
Run Code Online (Sandbox Code Playgroud)

algorithm rpn variadic-functions variable-length postfix-notation

5
推荐指数
1
解决办法
303
查看次数