Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.12/5 (3 votes)
See more:
C++
#include<iostream>
#include<stack>
#include<string>
#include<vector>
using namespace std;
int Priority(char symbol)
{                                        // to found priority of the symboles
	if (symbol == '(')
		return 3;
	else
		if (symbol == '/' || symbol == '*')
			return 2;
		else
			if (symbol == '+' || symbol == '-')
				return 1;
}
bool IsOperation(char symbol)                   // return true if symbole ,else return false {like numbers}
{
	return (symbol == '(' || symbol == '/' || symbol == '*' || symbol == '+' || symbol == '-');
}

bool IsNumber(char symble)               // return true if numbers , else return false {like operation + - ...}
{
	return (symble == '0' || symble == '1' || symble == '2' || symble == '3' || symble == '4' || symble == '5' || symble == '6'

		|| symble == '7' || symble == '8' || symble == '9');
}

void main()                            // start Function main
{
	stack<char>postfix;
	stack<char>OPERATION;
	string x;
	cout << "Enter your process without spaces to convert infix to postfix : ";
	cin >> x;   // enter x
	
	
	for (int i = 0; i < x.size(); i++)  // To Check on every index in x string
		
	{
		if (x[i] == ')')                                               // This's case if the symbole { ) } it means the end of the brackets { (..) }
		{                                                             // and this will insert  all element in operation stack	                                                         
			while (OPERATION.top() != '('&& !OPERATION.empty())      //to postfix stack and remove all element into { ( } in operation stack
			{                                                       // then remove the symbole{ ( }
				postfix.push(OPERATION.top());
				OPERATION.pop();
			}
			OPERATION.pop();
		}
		
		else	
			if (IsNumber(x[i]))                   // if symoble is Number enter to stack "  postfix "
		{
			if (IsOperation(x[i+1]))       // To check if i need to put space or not
			{
				postfix.push(x[i]);
				postfix.push(' ');
			}
			else
			{
				postfix.push(x[i]);
			}
		}
		else                                 // else if symoble is operation
			if (IsOperation(x[i]))
			{
				if (OPERATION.empty())
				{
					OPERATION.push(x[i]);
				}
				else
					if (Priority(x[i]) > Priority(OPERATION.top()) || OPERATION.top() == '(')  // insert in stack operation if element of index is operation or 
						                                                                    //  OPERATION.top()=='('
					{
						OPERATION.push(x[i]);
					}
				else
				{
					while ((!OPERATION.empty())&& Priority(OPERATION.top()) >= Priority(x[i]) )     // Repetition the body of while into 
						                                                                   // stack operation become empty and Priority OPERATION.top()   
					{
						if (OPERATION.top() == '(')
						{
							break;
						}                                                            // and Priority(OPERATION.top()) >= Priority(x[i])
						postfix.push(OPERATION.top());                                     // then insert to operation stack x[i]
						OPERATION.pop();
					}
					OPERATION.push(x[i]);
				}
			}
			else                        // This's case if the symbole is not operation and number // in fact the enter is error
			{
				cout << "Go out" << endl;
				break;
			}
	}                 // finish for loop

	while (!OPERATION.empty())                      // insert all element in operation stack to postfix stack and remove all element in operation stack
	{                                               // because we have finished converting
		postfix.push(OPERATION.top());
		OPERATION.pop();
	}


	vector<char> q;
	 
	while (!postfix.empty())
	{
		q.push_back(postfix.top());
		postfix.pop();
	}
	cout << endl;
	cout << "Before the conversion process : " << x << endl;
	cout << "After the conversion process : ";

	for (int i = q.size() - 1; i >= 0; --i)
	{
		cout << q[i] ;
	}


	cout << endl;
	system("pause");
}
Posted
Updated 25-Dec-15 8:41am
v2
Comments
[no name] 25-Dec-15 10:34am    
a.) Use enum in "Priority"
b.) Use Sets in "IsOperation"
c.) Use isDigit in "IsNumber" or at least (symble >= '0') && (symble <= '9')
d.) Read some theory about parsers
e.) Ask a question, what your Problem is and what you expect here.
Gnasani 25-Dec-15 14:03pm    
Thanks
Andreas Gieriet 25-Dec-15 14:46pm    
In addition to 0x01AA's comments: Your compiler should issue a warning (at least, if not an error) since not all control flow path of function Priority return an explicit value. E.g. what is the return value if neither of ( / * + - is passed as argument to that function?
Cheers
Andi
Gnasani 26-Dec-15 15:39pm    
Yes that's true, but my compiler it did't give any error for function "Priority". Thanks
Arthur V. Ratz 1-Jan-16 10:29am    
Nice code, it implements the concept of the infix computation similar to polish notation

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900