How to evaluate a postfix|prefix expression using stack?

This is a discussion on How to evaluate a postfix|prefix expression using stack? within the C Programming forums, part of the General Programming Boards category; can somebody help me bout this? Code: void postfix() { stack *A; char a[100]; int a; clrscr(); create_stack(A); p("Enter a ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    34

    How to evaluate a postfix|prefix expression using stack?

    can somebody help me bout this?

    Code:
    void postfix()
    { stack *A;
    char a[100];
    int a;
    clrscr();
    create_stack(A);
    p("Enter a postfix expression: ");
    s("%s",&a)
    
    for(x=0;x<strlen(a);x++)
    {if(x[a]>='0'&&x[a]<='9')
        push(A,a[x]);
     else
     pop(A);
    
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Looks like a function to me that calls other functions.

    So what help do you want?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    I think you should lose the pointless
    #define p printf
    #define s scanf
    macros.

    You also have two variables with the same name.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    34
    ah ok... tnx.. but we have a problem in evaluating the expression

    Code:
    void postfix()
    { stack *A;
    stack *B; stack *C; stack *D;
    char a[100];
    char b;
    char *c;
    
    int x,f,d,e,g,h,i;
    clrscr();
    create_stack(A);
    create_stack(B);
    create_stack(C);
    create_stack(D);
    printf("Enter a postfix expression: ");
    scanf("&#37;s",a);
    
    for(x=0;x<strlen(a);x++)
       {if(a[x]>='0' && a[x]<='9')
    	{ push(A,a[x]);
    	  *c=pop(A);
    	  b=atoi(c);
    	  push(B,b);
    	}
    
    	else if(a[x]=='-')
    	{
    	  e=pop(B);
    	  d=pop(B);
    	  f=e-d;
    	  push(C,f);
    	  h=pop(C);
    
    
    	}
    	
    }
    printf("Result: %d",i);
    
    getch();
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you please describe what the problem is that you are struggling with?

    It's all well and good reading your code and coming up with POTENTIALLY broken bits, but seriously, we need to understand what it is that you can't solve. What is your input, and what does it output, and how is that different from what you expected?

    --
    Mats

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    > create_stack(A);
    Is this a function, or a macro?
    Because if it's a function, then it's highly unlikely that you're actually initialising the stack, just a copy of it which is lost when the function returns.

    Also, pick more meaningful names for your variables. i is OK for a loop variable, but everything else should have at least some meaning.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-12-2006, 12:41 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  5. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21