标签: postfix-notation

C#生成IL for ++运算符 - 何时以及为什么前缀/后缀表示法更快

由于这个问题是关于增量运算符和带前缀/后缀表示法的速度差异,我将非常仔细地描述这个问题,以免Eric Lippert发现它并激怒我!

(有关我为什么要问的更多信息和详细信息,请访问http://www.codeproject.com/KB/cs/FastLessCSharpIteration.aspx?msg=3899456#xx3899456xx/)

我有四个代码片段如下: -

(1)单独,前缀:

    for (var j = 0; j != jmax;) { total += intArray[j]; ++j; }
Run Code Online (Sandbox Code Playgroud)

(2)单独,后缀:

    for (var j = 0; j != jmax;) { total += intArray[j]; j++; }
Run Code Online (Sandbox Code Playgroud)

(3)Indexer,Postfix:

    for (var j = 0; j != jmax;) { total += intArray[j++]; }
Run Code Online (Sandbox Code Playgroud)

(4)索引器,前缀:

    for (var j = -1; j != last;) { total += intArray[++j]; } // last = jmax - 1
Run Code Online (Sandbox Code Playgroud)

我试图做的是证明/反驳在这个上下文中前缀和后缀表示法之间是否存在性能差异(即局部变量因此不易变,不能从另一个线程等变化)如果存在,为什么会出现这种情况.

速度测试表明:

  • (1)和(2)以相同的速度运行.

  • (3)和(4)以相同的速度运行.

  • (3)/(4)比(1)/(2)慢〜27%.

因此,我得出的结论是,在postfix表示法本身上选择前缀表示法没有性能优势.但是,当实际使用操作结果 …

c# optimization performance il postfix-notation

20
推荐指数
3
解决办法
2078
查看次数

生成所有可能的"唯一"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
查看次数

`定义?`和`除非'没有按预期工作

我期待以下片段:

var = "Not Empty" unless defined? var
var # => nil
Run Code Online (Sandbox Code Playgroud)

回来"Not Empty",但我得到了nil.任何洞察为什么会发生这种情况?

ruby local-variables postfix-notation

10
推荐指数
2
解决办法
486
查看次数

使用Stacks从中缀表达式转换为postfix(C++)

我的讲师给了我一个创建程序来转换和使用Stacks将表达式转换为postfix的任务.我已经制作了堆栈类和一些函数来读取中缀表达式.

但是这个函数被称为convertToPostfix(char * const inFix, char * const postFix)负责将数组inFix中的inFix表达式转换为使用堆栈的postFix数组中的post fix表达式,并没有做它想做的事情.你能帮助我,告诉我我做错了什么吗?

以下是从inFix转换为postFix的函数的代码,convertToPostfix(char * const inFix, char * const postFix)是我需要帮助修复的代码:

 void ArithmeticExpression::inputAndConvertToPostfix()
    {
       char inputChar; //declaring inputChar
       int i = 0; //inizalize i to 0

       cout << "Enter the Arithmetic Expression(No Spaces): ";

       while( ( inputChar = static_cast<char>( cin.get() ) ) != '\n' )
       {
          if (i >= MAXSIZE) break; //exits program if i is greater than or equal to 100

          if(isdigit(inputChar) || isOperator(inputChar))
          {
             inFix[i] = inputChar; …
Run Code Online (Sandbox Code Playgroud)

c++ stack infix-notation postfix-notation

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

我可以写{x,a,b} //做[...,#]而不是做[...,{x,a,b}]吗?

我爱上了Ruby.在这种语言中,所有核心功能实际上都是方法 这就是为什么我更喜欢后缀表示法 - 当我想要处理的数据从匿名处理函数的主体中放置时,例如:array.map{...}.我相信,这个代码的易读性有很大优势.

但Mathetica,功能正常(是的,如果你想要它可以是程序性的)指示一个样式,其中函数名称放在数据的左边.正如我们在其手册中所看到的,//仅在它是一些简单的函数时使用,没有参数,如list // MatrixForm.当Function需要很多参数时,编写手册的人会使用语法F[data].
它会好起来的,但我的问题是这样的F[f,data],例如Do[function, {x, a, b}].大多数Mathematica函数(如果不是全部)都具有完全按此顺序的参数 - [function, data]而不是[data, function].由于我更喜欢​​使用纯函数来保持命名空间清理而不是在我的笔记本中创建很多命名函数,因此参数function可能太大 - 如此之大,该参数data将放在第5-20行代码之后函数调用.

这就是为什么有时候,当邪恶的 Ruby性质让我受到控制时,我用后缀方式重写这些函数:

做[f(x),{x,a,b}] \n {x,a,b} //做[f(x),#]&

因为它对我很重要,所以纯函数(可能是大代码)就是处理数据.是的我做到了,我很高兴.但有两件事:

  1. 这导致Mathematica突出显示解析器问题:x后缀表示法用蓝色突出显示,而不是绿松石;
  2. 每次当我查看Mathematica手册时,我都会看到这样的例子:Do[x[[i]] = (v[[i]] - U[[i, i + 1 ;; n]].x[[i + 1 ;; n]])/ U[[i, i]], {i, n, 1, -1}];这意味着......他们认为它很容易阅读/支持/等等.

所以这两件事让我在这里问这个问题:我是个坏男孩,使用我的Ruby风格,我应该像这些人一样编写代码,还是没关系,我不必担心,应该按我喜欢的方式写?

coding-style wolfram-mathematica postfix-notation

8
推荐指数
5
解决办法
576
查看次数

后缀表示法验证?

什么是评估包含后缀表达式(例如:3 5 +)的字符串(数组,某些东西)来检查有效性的好方法?

validation postfix-notation

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

这种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
查看次数

如何将后缀表达式放在二叉树中?

所以我有一个二叉树和一个后缀表达式"6 2*3 /"什么是算法把它放在一棵树?喜欢,

          [/]
          / \
        [*]  [3]
        / \
      [6] [2]
Run Code Online (Sandbox Code Playgroud)

algorithm tree postfix-notation

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

后缀计算器Java

好的,我必须从文件中读取后缀表达式.后缀表达式必须具有空格以分隔每个运算符或操作数.到目前为止,只有在输入文件中的运算符或操作数之间没有空格时,我才能使用它.(即如果文件有12+,我得到的结果是3.)为了做到这一点,我认为我需要对输入进行标记化,但我不确定如何.这就是我到目前为止所拥有的.感谢您的回复.

import java.util.*;
import java.io.*;
public class PostfixCalc{
public static void main (String [] args) throws Exception {
File file = new File("in.txt");
Scanner sc = new Scanner(file);
String input = sc.next();
Stack<Integer> calc = new Stack<Integer>();
while(sc.hasNext()){
for(int i = 0; i < input.length(); i++){
    char c = input.charAt(i);
    int x = 0;
    int y = 0;
    int r = 0;
    if(Character.isDigit(c)){
       int t = Character.getNumericValue(c);
        calc.push(t);
    }
    else if(c == '+'){
        x = calc.pop();
        y = calc.pop();
        r …
Run Code Online (Sandbox Code Playgroud)

java stack calculator postfix-notation

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

为什么postfix(rpn)符号比前缀更常用?

通过使用我的意思是它在许多计算器如HP35-中使用

我的猜测(和混淆)是 -

  1. postfix实际上是更高效的内存 - (所以在这里发表评论).(混淆 - 两者的评估算法与堆栈类似)
  2. 当时计算器中的键盘输入类型(混淆 - 这应该不重要,因为它只取决于第一个或最后一个给定的运算符的顺序)

可以问这个问题的另一种方式是后缀表示法优于前缀的优点是什么?
任何人都可以开导我吗?

algorithm calculator postfix-notation polish-notation

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