954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Converting equation from infix to postfix using stack

I am working on a home work assignment which is:
"Write a program that will allow the user to enter an infix expression terminated by '=' your program will convert to Postfix notation"

enter 3+7-6/2*8+7=
prints 3 7 + 6 2 / 8 * - 7 +

I have written the code below to complete this useing single digits, but I want to allow the user to put in any number and have it do the same
ie 37+47/12 prints 37 47 + 12/

This is not part of the assignment, but I have the next two week to fiddle with it.
Any suggestion? I am lost at the moment.

Thanx in advance!
Lanier

char InOp1='#'; //Holds values to be compared in switch case.
char InOp2='#'; //Holds value for comparison to properly push onto stack.
LinkedStack Cal; //stack used to hold info in postfix format

void In2Post()
	{
		cin>>InOp1;
	
		while(InOp1!='=')
		{
	
			//while loop to push digits on to stack
			while(InOp1!='+'&&InOp1!='-'&&InOp1!='*'&&InOp1!='/'&&InOp1!='('&&InOp1!=')'&&InOp1!='?')
			{
				Cal.Push(InOp1);
				cin>>InOp1;
			}
		
			switch(InOp1) //switch case used to properly format stack
			{
				case '+': cin>>InOp2;
					if(InOp2=='+'||InOp2=='-'||InOp2=='*'||InOp2=='/'||InOp2=='('||InOp2==')')
					{
						cout<<"Incorrect input "<<InOp2<<" cannot follow + sign."<<endl<<"Must be digit.";
						InOp1='?';
						break;
					}
					else
					{
						Cal.Push(InOp2);
						Cal.Push(InOp1);
						cin>>InOp1;
						break;
					}
				case '-': cin>>InOp2;
					if(InOp2=='+'||InOp2=='-'||InOp2=='*'||InOp2=='/'||InOp2=='('||InOp2==')')
					{
						cout<<"Incorrect input "<<InOp2<<" cannot follow - sign."<<endl<<"Must be digit.";
						InOp1='?';
						break;
					}
					else
					{
						Cal.Push(InOp2);
						Cal.Push(InOp1);
						cin>>InOp1;
						break;
					}
				case '/': cin>>InOp2;
					if(InOp2=='+'||InOp2=='-'||InOp2=='*'||InOp2=='/'||InOp2=='('||InOp2==')')
					{
						cout<<"Incorrect input "<<InOp2<<" cannot follow / sign."<<endl<<"Must be digit.";
						InOp1='?';
						break;
					}
					else
					{
						Check=Cal.Top();
						if(Check=='+'||Check=='-')
						{
							Cal.Pop();
							Cal.Push(InOp2);
							Cal.Push(InOp1);
							Cal.Push(Check);
							Check='#';
							cin>>InOp1;
							break;
						}
						else
						{
							Cal.Push(InOp2);
							Cal.Push(InOp1);
							Check='#';
							cin>>InOp1;
						}
					}
				case '*': cin>>InOp2;
					if(InOp2=='+'||InOp2=='-'||InOp2=='*'||InOp2=='/'||InOp2=='('||InOp2==')')
					{
						cout<<"Incorrect input "<<InOp2<<" cannot follow * sign."<<endl<<"Must be digit.";
						InOp1='?';
						break;
					}
					else
					{
						Check=Cal.Top();
						if(Check=='+'||Check=='-')
						{
							Cal.Pop();
							Cal.Push(InOp2);
							Cal.Push(InOp1);
							Cal.Push(Check);
							Check='#';
							cin>>InOp1;
							break;
						}
						else
						{
							Cal.Push(InOp2);
							Cal.Push(InOp1);
							Check='#';
							cin>>InOp1;
						}	
					}
				case '(':case ')': cin>>InOp1;
									break;
			
				default: cout<<endl<<"Input data contains error can not continue."<<endl<<endl;
						system("pause");
						return 0;
				
			}
                }
LanierWexford
Light Poster
27 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

The first thing I would suggest is you parse the expression from a string (and not user input), and return an expression list (not void, and not in a global).

You can then test with

LinkedStack in2post( std::string expr );

int main ( ) {
  std::string expr = "3+7-6/2*8+7=";
  LinkedStack result = in2post( expr );
}


You can then expand the tests with

LinkedStack in2post( std::string expr );

int main ( ) {
  std::string expr[] = {
      "1=",
      "1+2=",
      "1+2*3",
      "3+7-6/2*8+7=",
  };
  for ( int i = 0 ; i < sizeof expr / sizeof *expr ; i++ ) {
    LinkedStack result = in2post( expr[i] );
  }
}


When you're finally happy, then you can do

std::string myExpr;
getline( cin, myExpr );
LinkedStack result = in2post( myExpr );


As far as algorithms go, look up the "shunting yard algorithm".

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

I would also suggest better formatting. See this and pay particular attention to the Indentation section. As it is your code is very difficult to follow.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

First and formost THANX!! Walt for the link I knew my code was hard to read. Sorry for the bad formatting I will fix that soon.

Also thanx for the idea Sal. I will tweak and report back with the results.

Thanx again

Lanier

LanierWexford
Light Poster
27 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

fck

chalasesha
Newbie Poster
8 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You