can anyone find why is this program to solve postfix expression giving runtime error

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

    can anyone find why is this program to solve postfix expression giving runtime error

    #include<stdio. h>
    #include<stdlib .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,k;
    char a[100],c[100];
    scanf("%d",&m);
    for(i=0;i<m;i++ )
    { int j=0;

    while(1)
    {

    scanf("%c",&a[j]);
    if(a[j]=='?')
    break;
    else if(a[j]==' ')
    {
    push(atoi(a));
    j=0;
    }
    else if(a[j]=='+'||'-'||'*'||'/')
    pop(a[j]);
    else
    j++;
    }
    printf("%d",s.n[s.top]);

    }

    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This sort of thing is common in software development. It is a signal for you to step through your code using your debugger. If you don't know how to use it, then this is a great opportunity to learn how.

    All I could do is do your debugging for you but this would be a disservice since I would rob you of a learning opportunity.

    Comment

    Working...