can anyone tell why my program for solving postfix expression is not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jinnejeevansai
    New Member
    • Nov 2014
    • 48

    can anyone tell why my program for solving postfix expression is not working

    #include<stdio. h>
    struct stack{
    int top;
    int n[100];
    }s;
    void push(int a)
    {
    s.n[s.top+1]=a;
    s.top++;
    }
    void pop(char a)
    {
    int c,b;
    b=s.n[s.top];
    c=s.n[s.top-1];
    s.top--;
    switch(a)
    {
    case '+':
    s.n[s.top]=b+c;
    break;
    case '-':
    s.n[s.top]=b-c;
    break;
    case '*':
    s.n[s.top]=b*c;
    break;
    case '/':
    s.n[s.top]=b/c;
    break;
    }
    }
    int main()
    {
    s.top=-1;
    int m,i,j,k;
    char a[100],c[100];
    scanf("%d",&m);
    for(i=0;i<m;i++ )
    {
    for(j=0;j<100;j ++)
    {
    for(k=0;;k++)
    {scanf("%c",&a[k]);
    if(a[k]==' ')
    break;
    }
    if(a[0]=='+'||'-'||'*'||'/')
    pop(a[0]);
    else if(a[0]=='?')
    break;
    else
    push(atoi(a));
    }
    printf("%d",s.n[s.top]);
    }
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Have you stepped through your code using your debugger? If not, this is an excellent time to learn how. Having me debug your code isn't going to help you learn how to do it.

    Comment

    • jinnejeevansai
      New Member
      • Nov 2014
      • 48

      #3
      But the program is not even running .main function is also not getting executed

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        It's a problem if your code won't compile.

        In this case you use atoi() but don't #include <stdlib.h>.

        If your code compiles, main() will always execute

        Comment

        • jinnejeevansai
          New Member
          • Nov 2014
          • 48

          #5
          for(i=0;i<m;i++ ) is not getting executed but why?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            It executes on my machine.

            I inserted a breakpoint at the for loop and then selected "start debugging" on Visual Studio. The debugger stopped at that breakpoint.

            Does your debugger not do the same thing?

            Comment

            Working...