## C and C++

Moderators: None (Apply to moderate this forum)
Number of posts: 94711

This Forum Only

Help me to show step by step solution infix to postfix Posted by jeffreyancheta on 3 Nov 2012 at 8:36 PM
the code is running but my professor told me to modify the code. he need to see the step by step conversion of infix to postfix. the problem is i don't know how to show the step by step conversion..
example:
(a+b )/(a-b )
a+b / a-b
a+b a-b/
ab+ ab-/

```#include <iostream>
#define STACK_SIZE 100
using namespace std;

char stackChar[STACK_SIZE];
int top=-1;

void push(char c);
char pop();

int main()
{

char expr[STACK_SIZE], postfixexpr[STACK_SIZE];
cout << "Enter the exp<b></b>ression: ";
cin.getline(expr,STACK_SIZE);

strcat(expr,")");
push('(');

int j=0;

for(int i=0;i<strlen(expr);i++)
{
if(isalnum(expr[i]))
postfixexpr[j++]=expr[i];
else if(expr[i]==')')
{
while(stackChar[top]!='(')
postfixexpr[j++]=pop();
pop();
}

else if(expr[i]=='+' || expr[i]=='-')
{
while(stackChar[top]=='*' || stackChar[top]=='/' || stackChar[top]=='^' || stackChar[top]=='+' || stackChar[top]=='-')
postfixexpr[j++]=pop();
push(expr[i]);
}

else if(expr[i]=='*' || expr[i]=='/')
{
while(stackChar[top]=='^' || stackChar[top]==expr[i])
postfixexpr[j++]=pop();
push(expr[i]);
}
else
push(expr[i]);
}

postfixexpr[j]='\0';

cout << postfixexpr;
system("pause>0");
return 0;

}

void push(char c)
{
stackChar[++top] = c;
}

char pop()
{
return stackChar[top--];
}
```