it is the code infix to postfix conversion

example: (a+(b*c))

answer should be : abc*+

but my code is giving answer abc*

i am not getting my mistake...pls help

My Code:

#include<stdio.h>
#include<string.h>
int main()
{
    char s[400],stack[400];
    int t;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        gets(s);
        int len = strlen(s);
        int i,top=0,item=1;
        for(i=0;i<len;i++)
        {
            if(s[i]=='(')
            {
                stack[top]=s[i];
                 // printf("in top %d character is %c",top,stack[top]);
                top++;
            }
            else if(s[i]>=97 && s[i]<=122)
            {
                printf("%c",s[i]);
            }
            else if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' || s[i]=='^')
            {
                if((stack[top-1]=='+' || stack[top-1]=='-') && (s[i]=='*' || s[i]=='/'))
                {
                    printf("%c",stack[top-1]);
                    stack[top-1]=s[i];
                }
                else if((stack[top-1]=='+' || stack[top-1]=='-') && (s[i]=='^'))
                {
                    printf("%c",stack[top-1]);
                    stack[top-1]=s[i];
                }
                else if((stack[top-1]=='*' || stack[top-1]=='/') && (s[i]=='^'))
                {
                    printf("%c",stack[top-1]);
                    stack[top-1]=s[i];
                }
                else
                {
                    stack[top]=s[i];
                  //  printf("in top %d character is %c",top,stack[top]);
                    top++;
                }
            }
            else if(s[i]==')')
            {
                while(item!='(')
                {
                    item=stack[top];
                    top--;
                    if(item!='(')
                    {
                        printf("%c",item);
                    }
                }
            }
            item=1;
        }
        printf("\n");
    }
}

asked 08 Jun, 14:48

Rejwan's gravatar image

Rejwan
1056

edited 08 Jun, 19:09

prefix to postfix ? or it will be infix to postfix ?

(08 Jun, 14:53) menon

sorry it is infix to postfix

(08 Jun, 19:09) Rejwan
Be the first one to answer this question!
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×55
×4

question asked: 08 Jun, 14:48

question was seen: 186 times

last updated: 08 Jun, 19:09