rpn

Reverse polish notation C# don't work correctly

狂风中的少年 提交于 2021-02-05 08:10:41
问题 I write an rpn, with a struktogram. Newest Problem: It is'nt work correctly now. If input string is "5 + ((1 + 2) * 4) - 3" My output is: 5 1 2 + 4 * 3 - + I have to got this result: 5 1 2 + 4 * + 3 - Edited the source * That was the original problem, but helped me, and now the original mistakes fixed: * , At the debug when the loop or int i = 12, the c value is 0\0 or something else and this value is added to output (name: formula) string as a '(' bracket. And I don't know why. And the last

Apply distributive law on AST (or RPN) => disjunctive normal form

可紊 提交于 2021-01-27 20:32:52
问题 I have expressions like the following: {1000} AND ({1001} OR {1002} OR {1003}) Allowed operators are OR and AND, expressions can be nested using parenthesis. I already managed to tokenize this string and to convert it to an abstract syntax tree (AST) using the Shunting Yard algorithm, implemented in PHP 5.3. The above expression results in the following: 1000 1001 1002 | 1003 | & & / \ 1000 | / \ | 1003 / \ 1001 1002 When traversing this tree I want to output the final combinations of numbers

Regexp issue involving reverse polish calculator

▼魔方 西西 提交于 2020-01-04 05:25:10
问题 I'm trying to use a regular expression to solve a reverse polish calculator problem, but I'm having issues with converting the mathematical expressions into conventional form. I wrote: puts '35 29 1 - 5 + *'.gsub(/(\d*) (\d*) (\W)/, '(\1\3\2)') which prints: 35 (29-1)(+5) * expected (35*((29-1)+5)) but I'm getting a different result. What am I doing wrong? 回答1: I'm assuming you meant you tried puts '35 29 1 - 5 + *'.gsub(/(\d*) (\d*) (\W)/, '(\1\3\2)') ^ ^ Anyway, you have to use the

Infix to Postfix and unary/binary operators

Deadly 提交于 2019-12-19 04:16:09
问题 I have a piece of code that converts an infix expression to an expression tree in memory. This works just fine. There's just one small trouble. I just connect work out how to involve the unary operators correctly (the right associative ones). With the following infix expression : +1 + +2 - -3 - -4 I would expect an RPN of: 1+2++3-4-- Yet, none of the online infix-post converters I can find handle this example in the way I would expect. Does anyone have a clear explanation of handling right

RPN Evaluator optimization without losing readability

一曲冷凌霜 提交于 2019-12-14 03:16:40
问题 I'm implementing a program that needs a RPN calculator function, I've got the one bellow, but being new to python I wonder if I can optimize it without losing the readability. Found some solutions using dictionaries and so on, but I got lost in the 'pythonesque' parts, the lists are still somewhat of a mistery to me... my function is : def parseRPN(expression): """Parses and calculates the result fo an RPN expression takes a list in the form of ['2','2','*'] returns 4 """ try: stack = [] for

String index out of range when accessing chars in a String

岁酱吖の 提交于 2019-12-13 11:13:11
问题 I'm trying to make an RPN calculator. I have done the conversion from infix to postfix, now I want to evaluate the converted expression. When I enter any expression I get error String index out of range: 1. Here's my code with what I'm supposed to do in the program: static int eval(String postfix) { int result = 0; String temp2 = ""; int num1, num2, OPS; char operator; String delete = ""; for (int i = 0; i < postfix.length(); i++) { char M = postfix.charAt(i); // if v_i is an operand: Push v

convert infix to Rpn (shunting yard)

て烟熏妆下的殇ゞ 提交于 2019-12-12 05:15:16
问题 here is my code to convert infix to ron using shunting yard . I know how the algorithm works well and I have no problem with that . but when I run this just nothing happen . when I debug it I get unknown error on stack initialization line #include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <stack> using namespace std ; void convert (char *) ; double eval(char *) ; int precedence(char); int main() { cout << "\n Enter an expression :\n" ; cout << " >> " ; char

How to make a RPN calculator (Java)

冷暖自知 提交于 2019-12-11 13:17:50
问题 I have an assignment and I need a bit of help, there seems to be an error when doing more than one calculation using RPN format. I use the example input as given in the link below. On the first input (16 3 7 + *) it gives me the correct answer (160). But, the next two inputs (4 32.125 13 – * 20 +) and (5 –3 * 4 2 / 6 3 1 – / + +) return "error." Thanks for your help in advance, if you need some more information don't be afraid to ask. The assignment details: Details My code so far: import

RPN evaluation C++

亡梦爱人 提交于 2019-12-11 08:04:28
问题 Hello) This is my code for converting from a infix expression to a postfix one , however I just can't understand how can I evaluate the postfix expression that I get and I will be very grateful for any tips. I am not asking for a code although that would be helpful. #include <iostream> #include <stack> #include <string> using namespace std; bool operation(char b) { return b=='+' || b=='-' || b=='*' || b=='/' ; } bool priority(char a, char b) { if(a=='(') { return true; } if(a=='+' || a=='-')

K and R Reverse Polish Notation

会有一股神秘感。 提交于 2019-12-10 12:25:13
问题 Unable to figure out how function are getting called. Input 1 2 3 + + [Enter] //Note there is a space between input Output 6 //which is correct 1 -> when program compile while statement calls function getop(s). 2 -> In getop() function it will call getch() function which in turn call getchar() so at this step it will read 1 as input and return it. 3 -> Now it checks if c is digit or not which is true so it will again call getch() which read space,return its values,now it checks if it is digit