Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

Help me to show step by step solution infix to postfix

$
0
0
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--];
}


Viewing all articles
Browse latest Browse all 2703

Trending Articles