im wondering if any1 can send me the function that evaluates the value of a postfix expression in c++<<using stacks>>...
i really need it as soon as possible.. :sad:

its not a homework actuallly....its a project and its just a small part of it....
i just wanted help in this function only.. :o

>its not a homework actuallly....its a project
Okay, that's not different at all.

>i just wanted help in this function only..
No, you wanted a handout. We don't do that, so provide an honest attempt and we'll help.

loool...
im being honest...anyway,thankx for ur advice... ;)
hope u can help me one day... :o

>im being honest
I don't doubt it, but that doesn't change the fact that you're asking for us to do your work for you. Since we have neither the time nor the inclination to write something that we've all probably written before, that's just not going to happen.

>hope u can help me one day...
Hopefully you'll ask for something that I'm willing to help with.

/*  Program to evaluate postfix expression

      Author: Javed Khan

      Dated: 09-02-2011

*/
#include<math.h>
#include<stdio.h>
#include<conio.h>
#define SIZE 50

int top=-1;
float stack[SIZE/2]={0};

float pop(void);
void push(float num);
int is_alphabet(char symbol);

void main()
{
  int i;
  char exp[SIZE],ch;
  float op1,op2,value[26]={0};
  printf("\n\nEnter any correct postfix expression: ");
  gets(exp);
  for(i=0;exp[i]!='\0';i++)
  {
     ch=is_alphabet(exp[i]);
     if(ch)
        value[ch-97]=1;
  }
  for(i=0;i<26;i++)
  {
     if(value[i])
     {
        printf("\nEnter the value of %c: ",97+i);
        scanf("%f",&value[i]);
     }
  }
  for(i=0;exp[i]!='\0';i++)
  {
     ch=is_alphabet(exp[i]);
     if(ch)
        push(value[ch-97]);
     else
     {
        op2=pop();
        op1=pop();
        switch (exp[i])
        {
          case '+':
             push(op1+op2);
             break;
          case '-':
             push(op1-op2);
             break;
          case '*':
             push(op1*op2);
             break;
          case '/':
             push(op1/op2);
             break;
          case '^':
             push(pow(op1,op2));
             break;
        }
     }
  }
  printf("\n\nResult: %0.3f",pop());
}
void push(float num)
{
  stack[++top]=num;
}
float pop(void)
{
  float num;
  num=stack[top];
  top--;
  return num;
}
int is_alphabet(char ch)
{
  if(ch>=97&&ch<=122)
     return ch;
  else
     return 0;
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Comments
No code tags, no thank you (plus it's in C)
This article has been dead for over six months. Start a new discussion instead.