bool Calculator_Postfix::infix_to_postfix(const std::string & infix, Stack_Expr_Command_Factory & factory, Array<Expr_Command *>& postfix)
{
	std::istringstream input(infix);
	std::string token;
    
	int num=0;
	Expr_Command * cmd = 0;
    
	Stack <Expr_Command * > temp;
	size_t i=0;
    
    //variables for checking if infix expression is correct
	//numbers must be always +1 than operators(except parenthesis)
	//parentheses balnced is checked in other method
    int countnum=0;
    int countops=0;
   
    
    
	while(!input.eof())
	{
		input >> token;
        
                
        
            
			if(token== "(")
			{
				cmd = factory.create_parenthesis_command();
				temp.push(cmd);
			}
			else if(token == "+")
			{
                if(countnum==countops+1)
                {
                    countops++;
                    cmd=factory.create_add_command();
                    Check_Priority(temp,postfix,cmd,i);
                }
                else break;
			}
			else if(token == "-")
			{
                if(countnum==countops+1)
                {
                    countops++;
                    cmd= factory.create_subtract_command();
                    Check_Priority(temp,postfix,cmd,i);
                }
                else break;
				
			}
			else if(token == "*")
			{
                if(countnum==countops+1)
                {
                    countops++;

                    cmd= factory.create_multiply_command();
                    Check_Priority(temp,postfix,cmd,i);
                }
                else break;
			}
			else if(token == "/")
			{
                if(countnum==countops+1)
                {
                    countops++;
                    cmd= factory.create_devide_command();
                    Check_Priority(temp,postfix,cmd,i);
                }
                else break;
                
			}
			else if(token == "%")
			{
                if(countnum==countops+1)
                {
                    countops++;
                    cmd= factory.create_percentage_command();
                    Check_Priority(temp,postfix,cmd,i);
                }
                else break;
                
			}
			else if(token == ")")
			{
                
				while(temp.size()!=0)
				{
					Expr_Command * tempcmd = temp.top();
					if(tempcmd->priority()!=0)
					{
                        postfix.resize(postfix.cur_size_+1);
                        
                        postfix[i]=tempcmd;
						//postfix.set(i,tempcmd);
						i++;
						temp.pop();
					}
					else
					{
						delete tempcmd;
						break;
					}
                    
				}
				temp.pop();
				
			}
			else if(!(atoi(token.c_str())==0 & token[0]!='0'))
			{
			     std::istringstream token_num(token);
                 token_num >> num;
                 countnum++;
			     cmd= factory.create_number_command(num);
                postfix.resize(postfix.cur_size_+1);
				postfix[i]=cmd;
			     //postfix.set(i,cmd);
                
			     i++;
                
			}
            else{
                //std::cout <<"\nWrong expression " ;
                return false;
bool Postfix_Evaluator_Strategy::infix_to_postfix(const std::string & infix, Stack_Expr_Command_Factory & factory, Array<Command *>& postfix)
{

	std::istringstream input(infix);
	std::string token = "0";
	int num=0;
	Command * cmd = 0;

	Stack <Command * > temp;
	size_t i=0;

	bool checkOperator= true;
	bool checkOperand= true;
	bool checkExtraClosing = true;

	while(!input.eof())
	{
		input >> token;
		if(token != "QUIT")
		{
		
			if(token== "(")
			{
				cmd = factory.create_openbracket_command();
				temp.push(cmd);				
			}
			else if(token.compare("+")==0)
			{
				if(!checkOperator)
				{
					throw CalException("Expression is Invalid");
					
				}

				cmd=factory.create_add_command();
				Convert(temp,postfix,cmd,i);

				checkOperand = true ;
				checkOperator = false ;		
			}
			else if(token == "-")
			{

				if(!checkOperator)
				{
					throw CalException("Expression is Invalid");
					
				}

				cmd= factory.create_sub_command();
				Convert(temp,postfix,cmd,i);
				
				checkOperand = true ;
				checkOperator = false ;
				
			}
			else if(token == "*")
			{
				if(!checkOperator)
				{
					throw CalException("Expression is Invalid");
					
				}
				
				cmd= factory.create_mul_command();
				Convert(temp,postfix,cmd,i);
				
				checkOperand = true ;
				checkOperator = false ;

			}
			else if(token == "/")
			{
				if(!checkOperator)
				{
					throw CalException("Expression is Invalid");
					
				}

				cmd= factory.create_div_command();
				Convert(temp,postfix,cmd,i);
				
				checkOperand = true ;
				checkOperator = false ;
				
			}
			else if(token == "%")
			{
				if(!checkOperator)
				{
					throw CalException("Expression is Invalid");
					
				}

				cmd= factory.create_mod_command();
				Convert(temp,postfix,cmd,i);
				
				checkOperand = true ;
				checkOperator = false ;
				

			}
			else if(token == ")")
			{

				if(!checkExtraClosing)
					throw CalException("Expression is invalid");

				while(!temp.is_empty())
				{
					Command * tempcmd = temp.top();
					if(tempcmd->precedence()!=0)	
					{

						postfix.resize(postfix.cur_size_+1);
						postfix[i]= tempcmd;
						i++;
						temp.pop();
					}
					else
					{	
						delete tempcmd;
						break;
						
					}
				
				}
				temp.pop();	
				
			}
			else
			{
				std::istringstream token_num(token);

				if(!(token_num >> num))
				{
					throw CalException("Expression is Invalid");
				
				}
				
				cmd= factory.create_num_command(num);
				postfix.resize(postfix.cur_size_+1);
				postfix[i]=cmd;
				i++;

				checkOperand = false;
				checkOperator = true;

				
			}
			
		 
		}

				

	}
	while(!temp.is_empty())
	{
		postfix.resize(postfix.cur_size_+1);
		if((temp.top())->precedence() == 0)
		{
			throw CalException("Expression is Invalid");
		
		}

		postfix[i]=temp.top();
		
		temp.pop();
		i++;
		
		
	}
	return true;

}