Can anyone show me how to make this perform the calculation using the postfix data?? I assume I would have to read the post fix data one character at a time and code what to do with each character???

For example, if I entered this:

5 ^ 3 * 4 ^ ( 2 + ( 9 * 8 / ( 2 * 8 * ( 8 / 4 ) ) ) ^ 2 * 8 - 5 ) / 5 ^ 2 - 4

my program would correctly show the infix and postfix data, but how do I code it to read that data and perform the calculation correctly?

Thanks in advance, and here's the code:

#include <iostream>
using namespace std;

const int STACK_SIZE = 100;

int op_priority(char op);
void handle_operator(char next);

class STACK2
{
	public:
	   STACK2();    
	   void push(char);
	   char pop();
	   char top();
	   int top_p();
	   bool is_empty();

	private:
		char items[STACK_SIZE];
		int priority[STACK_SIZE];
		int size;
};

STACK2::STACK2() 
{
	size = 1;
	items[0] = '.';
	priority[0]=-1;
}



void STACK2::push(char item)
{
	items[size] = item;
	priority[size] = op_priority(item);
	if (item == '(')
	  priority[size] = 1;
	size++;
}

char STACK2::pop()
{
	size--;
	return items[size];
}

char STACK2::top()
{
	return items[size-1];
}

int STACK2::top_p()
{
	return priority[size-1];
}

bool STACK2::is_empty()
{
	return (size==1);
}

STACK2 stack;

int main() 
{
	char S[80];
	cout << "Infix Expression: ";
	cin.getline(S,80,'\n');

	cout << "Postfix Expression: ";
	int i = 0;
	while (i < strlen(S))
	{
	   char next_token = S[i];
	   switch (next_token) 
	   {
		  case ' ' : break;
		  case '^':
		  case '*':
		  case '/':
		  case '+':
		  case '-':
		  case '(':
		  case ')': handle_operator(next_token); break;
		  default: cout << " " << next_token;
	   }
	   i++;
	}
	handle_operator('.');
	cout << endl;
}

int op_priority(char op)
{
   switch (op)
   {
      case '^' : return 4; break;
      case '*':
      case '/': return 3; break;
      case '+':
      case '-': return 2; break;
      case '(': return 5; break;
      case ')': return 0; break;
      default: return -1; break;
   }
}

void handle_operator(char next)
{
	if (next !='.') 
	{
	   int priority = op_priority(next);
	   if (priority >= stack.top_p()) 
		  stack.push(next);
	   else 
	   {
		  while (!stack.is_empty() && stack.top_p() >= priority)
		  {
			 char next_one = stack.pop();
			 if (next_one != '(' && next_one != ')')
				cout << next_one;
		  }
		  stack.push(next);
	   }
	}
	else 
		while (!stack.is_empty()) 
		{
			char next_one = stack.pop();
			if (next_one != '(' && next_one != ')')
			   cout << next_one;
		}
}

In doing some research online I found this code that allows the user to enter the postfix data and performs the calculations. I've been trying to figure out how to use this to help me write the code to perform the calculation for my original problem, but seem to be thoroughly lost. Could someone point me in the right direction? I just don't see exactly how the program should read the postfix to perform the calculation..

#include <stdio.h>
#include <ctype.h>

#define MAX 50
#define EMPTY -1

struct stack
{
        int data[MAX];
        int top;
};

void emptystack(struct stack* s)
{
        s->top = EMPTY;
}

void push(struct stack* s,int item)
{
        if(s->top == (MAX-1))
        {
                printf("\nSTACK FULL");
        }
        else
        {
                ++s->top;
                s->data[s->top]=item;
        }
}

int pop(struct stack* s)
{
        int ret=EMPTY;
        if(s->top == EMPTY)
                printf("\nSTACK EMPTY");
        else
        {
                ret= s->data[s->top];
                --s->top;
        }
        return ret;
}

void display(struct stack s)
{
        while(s.top != EMPTY)
        {
                printf("\n%d",s.data[s.top]);
                s.top--;
        }
}

int evaluate(char *postfix)
{
        char *p;
        struct stack stk;
        int op1,op2,result;

        emptystack(&stk);
        p = &postfix[0];

        while(*p != '\0')
        {
           /* removes tabs and spaces */
                while(*p == ' ' || *p == '\t')
                {
                        p++;
                }
          /* if is digit */
                if(isdigit(*p))
                {
                        push(&stk,*p - 48);
                }
                else
                {
                   /* it is an operator */
                        op1 = pop(&stk);
                        op2 = pop(&stk);

                        switch(*p)
                        {
                                case '+':
                                        result = op2 + op1;
                                        break;

                                case '-':
                                        result = op2 - op1;
                                        break;

                                case '/':
                                        result = op2 / op1;
                                        break;

                                case '*':
                                        result = op2 * op1;
                                        break;

                                case '%':
                                        result = op2 % op1;
                                        break;

                                default:
                                        printf("\nInvalid Operator");
                                        return 0;
                        }
                        push(&stk,result);
                }
                p++;
        }
        result = pop(&stk);
        return result;
}

int main()
{
        char exp[MAX];
        printf("Enter Postfix Expression : ");
        gets(exp);
        printf("%s EQUALS %d\n",exp,evaluate(&exp[0]));
        return 0;
}

1. Next time use code tag correctly:
[code=c++] source(s)

[/code]
No line numbers for a plain text (to refer for explanation).

2. A very simple logic (pseudocode):

loop
   get the next token
   if the token is operand then
      push it to the stack
   else if the token is operator then
      pop this operator arguments from the stack
      perform operator
      push the result (as an operand)
   else // end of expression
      pop expression result from the stack
      now the stack is empty (check)
      break the loop
   endif
endloop

Well, I "flailed mightily" at incorporating the postfix calculation over the past few days, but ended up just submitting the original code (which was all that was required for the assignment, getting it to actually calculate the answer was for bonus points).

Would anyone mind showing me how this would work for my code? My instructor said he would give us his version of how this works (including the bonus) in class next week, but I still would like to see how it would run for my code.

If I understand the basic jist of how this should work, it should follow something like this, correct?

1
2
+
10
+

step one
pop-->1
pop-->2
pop-->+ = 3

10
+

step two
3<--push
10
+

step 3
pop-->3
pop-->10
pop-->+ = 13

But I don't understand how that would work for my code..

This article has been dead for over six months. Start a new discussion instead.