?
Solved

Unary minus in a postfix expression

Posted on 2003-03-28
7
Medium Priority
?
3,291 Views
Last Modified: 2008-02-01
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
Comment
Question by:larissav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:akshayxx
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

by:larissav
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

by:larissav
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:larissav
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

by:akshayxx
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

by:
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

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
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 opening and reading files in the C programming language.

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

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

Join & Ask a Question