当前位置: 首页>>代码示例>>C++>>正文


C++ Token::GetPrecedence方法代码示例

本文整理汇总了C++中Token::GetPrecedence方法的典型用法代码示例。如果您正苦于以下问题:C++ Token::GetPrecedence方法的具体用法?C++ Token::GetPrecedence怎么用?C++ Token::GetPrecedence使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Token的用法示例。


在下文中一共展示了Token::GetPrecedence方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: if


//.........这里部分代码省略.........
                    }
                    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
开发者ID:yotoo2920,项目名称:Expression_Evaluator,代码行数:67,代码来源:CoreEvaluator.cpp

示例2: main

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 == '*'){
//.........这里部分代码省略.........
开发者ID:jruddell,项目名称:school-lab-C--,代码行数:101,代码来源:main.cpp


注:本文中的Token::GetPrecedence方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。