Solved

# Unary minus in a postfix expression

Posted on 2003-03-28
Medium Priority
3,769 Views
I have developed a program that converts infix expressions to postfix and evaluates them, however, I would like to implement a unary minus ~ to represent negative numbers in my expressions. Does anyone know how I could implement this? I have tried thinking of everything, but I do not know how to make it evaluate it as a negative number, as opposed to an operator. I am thinking I will have it take precedence over div,mult,and mod, but be lower than parentheses. I am just lost as to how to convert this, though. Any help would be greatly appreciated! Thanks!!
0
Question by:larissav
• 4
• 3

LVL 8

Expert Comment

ID: 8228950
these links might be of ur interest.

http://dbforums.com/archives/t317336.html
http://teachertech.rice.edu/Participants/marilynt/Lessons/CPlus/APAB/StacksQueues/InfixToPostfixLab.html

on this link ..
http://www.onthenet.com.au/~grahamis/int2008/week02/lect02.html

look for following topic

Infix to Postfix (RPN) Conversion
Of course postfix notation is of little use unless there is an easy method to convert standard (infix) expressions to postfix. Again a simple algorithm exists that uses a stack:

Algorithm
initialise stack and postfix output to empty;
while(not end of infix expression) {
get next infix item
if(item is value) append item to pfix o/p
else if(item == ‘(‘) push item onto stack
else if(item == ‘)’) {
pop stack to x
while(x != ‘(‘)
app.x to pfix o/p & pop stack to x
} else {
while(precedence(stack top) >= precedence(item))
pop stack to x & app.x to pfix o/p
push item onto stack
}
}
while(stack not empty)
pop stack to x and append x to pfix o/p
Operator Precedence (for this algorithm):

4 : ‘(‘ - only popped if a matching ‘)’ is found

3 : All unary operators

2 : / *

1 : + -

0

Author Comment

ID: 8229009
Thanks for the quick reply-- I appreciate it!

However, I have already written the code to convert from infix to postfix, and to evaluate the postfix expression. It handles all binary operators. As you know, I these operators require two operands, which is no problem. I just don't know how to implement the negative sign. It's read in as a string, but when the expression is parsed, it is treated as an operator. ~2-2 = (~2)-2 = 2~2-. It treats it as though I am trying to do 2-~, which obviously causes an error. I feel incredibly stupid since I just don't know how to set this up!

Thanks again for the time and help!
0

Author Comment

ID: 8229010
Thanks for the quick reply-- I appreciate it!

However, I have already written the code to convert from infix to postfix, and to evaluate the postfix expression. It handles all binary operators. As you know, I these operators require two operands, which is no problem. I just don't know how to implement the negative sign. It's read in as a string, but when the expression is parsed, it is treated as an operator. ~2-2 = (~2)-2 = 2~2-. It treats it as though I am trying to do 2-~, which obviously causes an error. I feel incredibly stupid since I just don't know how to set this up!

Thanks again for the time and help!
0

Author Comment

ID: 8229028
Thanks for the quick reply-- I appreciate it!

However, I have already written the code to convert from infix to postfix, and to evaluate the postfix expression. It handles all binary operators. As you know, I these operators require two operands, which is no problem. I just don't know how to implement the negative sign. It's read in as a string, but when the expression is parsed, it is treated as an operator. ~2-2 = (~2)-2 = 2~2-. It treats it as though I am trying to do 2-~, which obviously causes an error. I feel incredibly stupid since I just don't know how to set this up!

Thanks again for the time and help!
0

LVL 8

Expert Comment

ID: 8229039
evaluation is pretty easy..
to evaluate
2~2-
here is algorithm
while( more tokens)
if token = operand
push in the stack
if token = unary operator
pop one element of the stack ( if cannot pop malformed postfix expression)
apply unary operator ..
push back in the stack
if token = binary operator.
pop two top operands and apply operator and push result in the stack

0

LVL 8

Accepted Solution

akshayxx earned 300 total points
ID: 8229045
here is excerpt from one of the links that i posted..
http://www.onthenet.com.au/~grahamis/int2008/week02/lect02.html

Consider the postfix expression :

6 5 2 3 + 8 * + 3 + *

Algorithm
initialise stack to empty;
while (not end of postfix expression) {
get next postfix item;
if(item is value)
push it onto the stack;
else if(item is binary operator) {
pop the stack to x;
pop the stack to y;
perform y operator x;
push the results onto the stack;
} else if (item is unary operator) {
pop the stack to x;
perform operator(x);
push the results onto the stack
}
}
The single value on the stack is the desired result.
Binary operators: +, -, *, /, etc.,

Unary operators: unary minus, square root, sin, cos, exp, etc.,
0

Author Comment

ID: 8229094
Got it this time! Thanks so much for your help!!!!!!!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
###### Suggested Courses
Course of the Month8 days, 11 hours left to enroll

#### 621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.