vector<Token*>* CoreEvaluator::ShuntingOperations(vector<Token>* toUnSort)
{
    if (toUnSort == NULL) throw new exception();

    // vector
    vector<Token*>* SortedVector = new vector<Token*>();

    // queue
    queue<Token*>* toQueue = new queue<Token*>();

    // stack
    stack<Token*>* toStack = new stack<Token*>();

    // temp token
    Token* tempToken;

    // iterating token by token
    for (int i=0; i < toUnSort->size(); i++)
    {
        // 1 - reading at i
        tempToken = &toUnSort->at(i);

        // 2 - int, real or negative values
        if (isNumber(tempToken))
        {
            toQueue->push(tempToken);
        }

        // 4 - for log(v,b) and coma ERROR checking
        else if (tempToken->GetTokenType() ==  Comma) // exa: 3,4
        {


            if (toStack->empty())
            {
                cout << "Invalid expression entered." << endl;
								isValid = false;
                while (!toQueue->empty())
                {
                    toQueue->pop(); // cleaning the queue before printing the error
                }
                break;
            }

            Token* tempOpt = toStack->top();

            if (tempOpt->GetTokenType() == OpenPar) // exa: sin(3,4)
            {
                toStack->pop(); // getting the OpenPar out

                if (!toStack->empty())
                {
                    tempOpt = toStack->top(); // update top position
                }
                else
                {
                    cout << "Invalid expression entered." << endl;
										isValid = false;
                    while (!toQueue->empty())
                    {
                        toQueue->pop(); // cleaning the queue before printing the error
                    }
                    break;
                }

                if (tempOpt->GetTokenType() == sinFunc || tempOpt->GetTokenType() == cosFunc || tempOpt->GetTokenType() == tanFunc || tempOpt->GetTokenType() == PowerFunc || tempOpt->GetTokenType() == FactFunc || tempOpt->GetTokenType() == secFunc || tempOpt->GetTokenType() == cscFunc || tempOpt->GetTokenType() == cotFunc || tempOpt->GetTokenType() == lnFunc)
                {
                    cout << "Invalid expression entered." << endl;
										isValid = false;
                    while (!toQueue->empty())
                    {
                        toQueue->pop(); // cleaning the queue before printing the error
                    }
                    while (!toStack->empty())
                    {
                        toStack->pop(); // cleaning the queue before printing the error
                    }
                    break;
                }
                else // it is log
                {
                    Token* parth = new Token(OpenPar,"(");
                    toStack->push(parth); // adding the OpenPar back to stack
                    // adding and erassing the Comma
                    toStack->push(tempToken);
                    toStack->pop();
                }
            }
            else
            {
                // adding and erassing the Comma
                toStack->push(tempToken);
                toStack->pop();
            }
        }

        // 5 - an operator +,-,*,/,power ^, sin, cos, tan, factorial, log, root
        else if (isOperator(tempToken))
        {
            // if the stack is not empty
            if (!toStack->empty())
            {
                // 5.1 - while operator on the top of the stack
                Token* tempOpt = toStack->top();

                while (!toStack->empty() && isOperator(tempOpt) && ((tempToken->GetAssociativity() == Left && tempToken->GetPrecedence() <= tempOpt->GetPrecedence()) || (tempToken->GetAssociativity() == Right && tempToken->GetPrecedence() < tempOpt->GetPrecedence())))
                {
                    // 5.2
                    toQueue->push(toStack->top());
                    toStack->pop();
                    if (!toStack->empty())
                    {
                        tempOpt = toStack->top(); // update top position
                    }
                    else
                    {
                        break;
                    }
                }
            }
            // 5.4
            toStack->push(tempToken);
        }

        // 6 - openPar
        else if (tempToken->GetTokenType() == OpenPar)
        {
            toStack->push(tempToken);
        }

        // 7 - closePar
        else if (tempToken->GetTokenType() == ClosePar)
        {
            // 7.1
            Token* tempOpt = toStack->top();
            while (!toStack->empty() && tempOpt->GetTokenType() != OpenPar)
            {
                toQueue->push(toStack->top()); // pop onto the queue
                toStack->pop();
                tempOpt = toStack->top(); // update top position in the stack
            }
            // 7.4
            if(toStack->empty())
            {
                throw new logic_error("Mismatched parentheses.");
            }
            // 7.2
            toStack->pop(); // pop from the stack to nowhere
        }
        else
        {
            string str = tempToken->GetTokenStr();

            // clean memory

            //trow exception
            throw runtime_error( str + " is not a valid argument");
        }

    } // end of the for

    while(!toStack->empty())
    {
        if (toStack->top()->GetTokenType() == OpenPar || toStack->top()->GetTokenType() == ClosePar) // is parentheses
        {
            throw new logic_error("Mismatched parentheses.");
        }
        toQueue->push(toStack->top()); // pop onto the queue
        toStack->pop();
    }

    // passing token by token from the queue to the Sorted vector
    while (!toQueue->empty())
    {
        SortedVector->push_back(toQueue->front());
        toQueue->pop();
    }

    return SortedVector;
}
Ejemplo n.º 2
0
int main (int argc, char * argv [])
	{
	WCS_String temp;
	char op;
	char character;
	long i;
	long val;
	MathTree mathTree;
	Token token;

	cout<<"********************** Welcome to the MathTree Calculator!! ********************" << endl;
	cout<<"Enter 'C' followed by a whole number to enter a constant in the tree" <<endl;
	cout<<"Enter 'V' followed by two digits to enter a variable into the tree" <<endl;
	cout<<"Enter 'v' followed by two digits, a space, and then up to three digits"<<endl;
	cout<<"To set a value to your variable" <<endl;
	cout<<"Enter 'O' followed by and operator (+ - * /) followed by a digit "<<endl;
	cout<<"To insert an operator into the tree with a percedence set by the digit"<<endl;
	cout<<"Enter 'E' to evaluate the tree and to see what the answer is" <<endl;
	cout<<"Enter 'D' to delete the tree" <<endl;
	cout<<"Enter 'X' to exit the program"<<endl;
	cout<<endl;
	cout<<"\t\t\t    ENTER A VALID IMPUT " <<endl;
	for(;;)
		{
		character = cin.get();
		switch (character)
			{
		case 'c':
		case 'C':
			cin >> i;
			cin.ignore ();
			cout<<"you entered a constant "<< i << endl;
			cout<<"\n"<<endl;
			token.SetType (Token::ConstantToken);	// move SetType from private to public in tokenn class
			token.SetValue (i);
			mathTree.InsertOperand (token);
			break;
		case 'V':
			cin >> i;
			cin.ignore ();
			cout<<"You want to enter the variable " << i <<endl;
			token.SetType (Token::VariableToken);
			token.SetWhich (i);
			mathTree.InsertOperand (token);
			cout<<"You entered the variable "<<token.GetWhich() <<endl;
			cout<<"\n"<<endl;
			break;
		case 'e':
		case 'E':
			try{
				cout << "Answer is " << mathTree.Evaluate () << endl;
				cout<<"\n"<<endl;
				}
			catch(MathTree::Exceptions E){
				switch (E){
				case 0:
					cout<<"cannot divide by 0 exiting program.. "<< endl;
					exit (0);
					}
			}
		break;
		case 'v':
			cin >> i;
			cin >> val;
			cin.ignore ();
			cout<<"You want to set the variable " << i << " To the value " << val <<endl;
			token.SetType (Token::VariableToken);
			token.SetWhich (i);
			token.SetValue (val);
			cout<<"the variables value is "<<token.GetValue()<<endl;
			cout<<"\n"<<endl;
			break;
		case 'X':
		case 'x':
			exit (0);
			break;
		case 'O':
		case 'o':
			cin >> op;
			cin >> val;
			cin.ignore ();
			cout<<"You Entered a " << op << " Operator " << endl;
			cout<<"You want to set the " << op << " Operator's precedence to " << val << endl;
			if (op == '+'){
				token.SetType(Token::OperatorPlusToken);
				token.SetPrecedence((Operator::Precedence)(val));
				mathTree.InsertBinaryOperator (token);
				cout<<"operators precedence is : " << token.GetPrecedence() << endl;
				cout<<"\n"<<endl;
				}
			else
				if (op =='-'){
					token.SetType(Token::OperatorMinusToken);
					token.SetPrecedence((Operator::Precedence)(val));
					mathTree.InsertBinaryOperator (token);
					cout<<"operators precedence is : " << token.GetPrecedence() << endl;
					cout<<"\n"<<endl;
					}
				else 
					if (op == '*'){
						token.SetType(Token::OperatorAsteriskToken);
						token.SetPrecedence((Operator::Precedence)(val));
						mathTree.InsertBinaryOperator (token);
						cout<<"operators precedence is : " << token.GetPrecedence() << endl;
						cout<<"\n"<<endl;
						}
					else
						if (op == '/'){
							token.SetType(Token::OperatorSlashToken);
							token.SetPrecedence((Operator::Precedence)(val));
							mathTree.InsertBinaryOperator (token);
							cout<<"operators precedence is : " << token.GetPrecedence() << endl;
							cout<<"\n"<<endl;
							}
						break;

		case 'd':
		case 'D':
			cout<< "Deleting the tree " << endl;
			cout<<"Keep in mind that.... "<< endl;
			cout<<" Variable Token's Enumeration value is 2"<<endl;
			cout<<" Constant Token's Enumeration value is 3"<<endl;
			cout<<" BinaryPlus Token's Enumeration value is 7"<<endl;
			cout<<" BinaryMinus Token's Enumeration value is 8"<<endl;
			cout<<" BinaryAsterisk Token's Enumeration value is 9"<<endl;
			cout<<" BinarySlash Token's Enumeration value is 10"<<endl;
			mathTree.Delete();	
                    break;
        case 'P':
        case 'p':
                    cout<<"Printing your Tree " <<endl;
                    mathTree.walkingPrint();

		}	
	}
        return 0;
	}