postfix-notation

Converting from Infix to Postfix and evaluating Postfix notation

戏子无情 提交于 2021-02-07 11:13:50
问题 I'm writing a program that reads an Infix notation, converts it to Postfix and then evaluate that Postfix. Here's my program: #include<stdio.h> #include <ctype.h> #define SIZE 50 /* Size of Stack */ char s[SIZE]; int top = -1; /* Global declarations */ push(char elem) { /* Function for PUSH operation */ s[++top] = elem; } char pop() { /* Function for POP operation */ return (s[top--]); } int pr(char elem) { /* Function for precedence */ switch (elem) { case '#': return 0; case '(': return 1;

Converting from Infix to Postfix and evaluating Postfix notation

只谈情不闲聊 提交于 2021-02-07 11:13:45
问题 I'm writing a program that reads an Infix notation, converts it to Postfix and then evaluate that Postfix. Here's my program: #include<stdio.h> #include <ctype.h> #define SIZE 50 /* Size of Stack */ char s[SIZE]; int top = -1; /* Global declarations */ push(char elem) { /* Function for PUSH operation */ s[++top] = elem; } char pop() { /* Function for POP operation */ return (s[top--]); } int pr(char elem) { /* Function for precedence */ switch (elem) { case '#': return 0; case '(': return 1;

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

StackOverFlowError in Java postfix calculator

喜你入骨 提交于 2020-01-07 07:51:09
问题 The following class is used by another program. When it is accessed, it throws a StackOverFlowError. This is part of a Postfix Calculator I have to do as a project at my university. Any help would be greatly appreciated, thank you in advance. I'm quite new at Java and I have no idea what to do. CODE: import java.util.Queue; import java.util.Stack; public class MyPostfixMachine implements PostfixMachineInterface { MyMathOperations mmo = new MyMathOperations(); MyPostfixMachine mpm = new

StackOverFlowError in Java postfix calculator

浪子不回头ぞ 提交于 2020-01-07 07:51:08
问题 The following class is used by another program. When it is accessed, it throws a StackOverFlowError. This is part of a Postfix Calculator I have to do as a project at my university. Any help would be greatly appreciated, thank you in advance. I'm quite new at Java and I have no idea what to do. CODE: import java.util.Queue; import java.util.Stack; public class MyPostfixMachine implements PostfixMachineInterface { MyMathOperations mmo = new MyMathOperations(); MyPostfixMachine mpm = new

Evaluate postfix using a stack in C++

耗尽温柔 提交于 2020-01-03 05:47:22
问题 #include <iostream> #include <sstream> #include <stack> #include <limits> #include <string> using namespace std; int main() { string input; cout << "Enter a postfix expression: " << endl; getline(cin, input); int operand1, operand2, result,number; stack<char>operation; stringstream temp; int i=0; while (i < input.length()) { if (isdigit(input[i])) { operation.push(input[i]); } else { operand2 = operation.top(); temp << operation.top(); operation.pop(); operand1 = operation.top(); temp <<

Postfix notation validation?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-02 03:03:35
问题 What would be a good way to evaluate a string(array, something) that contains a postfix expression(ex: 3 5 +) to check for validity? 回答1: I'm assuming here that what you mean by valid is that executing the code will never underflow the stack and will leave a single value on the stack. If you have a more stringent notion of validity, you'll need a more sophisticated checker. If you want to check for this kind of validity, it is not necessary to evaluate the string, and you can use a counter ,

`defined?` and `unless` not working as expected

纵饮孤独 提交于 2019-12-30 17:26:20
问题 I was expecting the following snippet: var = "Not Empty" unless defined? var var # => nil to return "Not Empty" , but I got nil . Any insight into why this is happening? 回答1: This is one of the only moments in Ruby I would call actual WTFs. You have to use unless defined? var var = :value end With the postfix syntax, the interpreter will internally nil -ify the value so it can reason about the variable, thus making it defined before the check is done: # Doesn't print anything unless defined?

`defined?` and `unless` not working as expected

╄→尐↘猪︶ㄣ 提交于 2019-12-30 17:26:10
问题 I was expecting the following snippet: var = "Not Empty" unless defined? var var # => nil to return "Not Empty" , but I got nil . Any insight into why this is happening? 回答1: This is one of the only moments in Ruby I would call actual WTFs. You have to use unless defined? var var = :value end With the postfix syntax, the interpreter will internally nil -ify the value so it can reason about the variable, thus making it defined before the check is done: # Doesn't print anything unless defined?

Postfix to Infix with minimum number of parentheses

北慕城南 提交于 2019-12-30 03:35:23
问题 I am looking for algorithm postfix to infix notation which will produce the minimum number of the parentheses. I have found that but it will produce many, many parentheses: http://tajendrasengar.blogspot.com/2011/09/postfix-to-infix-algorithm.html For example The input: <ONP>abcd*/+~ The result: <INF>~(a+b/(c*d)) 回答1: What you need to do if you really want as few parentheses as possible, is similar to what the algorithm you linked to says. However... You should store an operator for each