Thread: Stack-Postfix Expression

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    13

    Stack-Postfix Expression

    I have another problem with stack. I want to evaluate a postfix expression and display the result. Below is my code but contain some errors. Anyone can help me?

    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include<ctype.h>
    #include<string.h>
    #include "StackInterface.h"


    char *PostfixString;

    void InterpretPostfix (void)
    {
    float LeftOperand, RightOperand, Result;
    int i;
    char c;
    char *s="x";
    Stack EvalStack;

    InitializeStack(&EvalStack);

    printf("Enter Postfix expression-> ");
    scanf("%s", PostfixString);

    for(i=0; i<strlen(PostfixString); ++i)
    {
    s[0]=c=PostfixString[i];
    if(isdigit(c))
    Push((float)atof(s), &EvalStack);
    else if(c=='+' || c=='-' || c=='*' || c=='/' || c=='^')
    {
    Pop(&EvalStack, &RightOperand);
    Pop(&EvalStack, &LeftOperand);

    switch (c)
    {
    case '+':
    Push(LeftOperand + RightOperand, &EvalStack);
    break;
    case '-':
    Push(LeftOperand - RightOperand, &EvalStack);
    break;
    case '*':
    Push(LeftOperand * RightOperand, &EvalStack);
    break;
    case '/':
    Push(LeftOperand / RightOperand, &EvalStack);
    break;
    case '^':
    Push(exp(log(LeftOperand)*RightOperand), &EvalStack);
    break;
    default:
    break;
    }
    }
    }

    Pop(&EvalStack, &Result);
    printf("Value of postfix expression = %f\n", Result);
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    What is the problem here? I read the code quickly, but noticed there are a lot of datatypes and functions which are probably defined by you and not provided in the posting. So I can't run the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  4. Replies: 4
    Last Post: 03-12-2006, 02:17 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM