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


C++ shared_ptr::toDouble方法代码示例

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


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

示例1: if

// Adds number to this and returns the sum
//
// Parameters:
// shared_ptr<AbstractNumber> number	number being added
//
// Returns:
// shared_ptr<AbstractNumber> 			resulting sum of addition
 tr1::shared_ptr<AbstractNumber> Exponent::add(tr1::shared_ptr<AbstractNumber> number){
 	// Checks for cancellation
 	number = number->simplify();
 	if(abs(toDouble() - number->toDouble()) < 0.000001 &&
 	   this->getSign() != number->getSign()){
 		tr1::shared_ptr<AbstractNumber> r(new SmartInteger(0));

 	    return r;
 	}
 	// Checks for duplication/simplification
 	else if(abs(toDouble() - number->toDouble()) < 0.000001 &&
 			this->getSign() == number->getSign()){
 		vector< tr1::shared_ptr<AbstractNumber> > MultVector;
		tr1::shared_ptr<AbstractNumber> i(new SmartInteger(2));
		MultVector.push_back(i);
		tr1::shared_ptr<AbstractNumber> me(new Exponent(base, power, sign));
		MultVector.push_back(me);
		tr1::shared_ptr<AbstractNumber> r(new MultExpression(MultVector, '+'));

	    return r;
  	}

    //Can't tell if they add or not
 	else if(number->getName() == "SumExpression" ||
            number->getName() == "MultExpression"){
 	    return number->add(shared_from_this());
 	}
    // Duplication necessary for simplification
 	// Assuming number is in simplest form

 	// No simplification possible
	else{
		vector< tr1::shared_ptr<AbstractNumber> > SumVector;
		tr1::shared_ptr<AbstractNumber> me(new Exponent(base, power, sign));
		SumVector.push_back(me);
		SumVector.push_back(number);
		tr1::shared_ptr<AbstractNumber> r(new SumExpression(SumVector));

	    return r;
	}

}
开发者ID:kylelin47,项目名称:COP-Project,代码行数:49,代码来源:Exponent.cpp

示例2: multiply

// Divides this by number and returns the product
//
// Parameters:
// shared_ptr<AbstractNumber> number	divisor
//
// Returns:
// shared_ptr<AbstractNumber> 			resulting quotient of division
tr1::shared_ptr<AbstractNumber>  Exponent::divide(tr1::shared_ptr<AbstractNumber>number){
	// Reverses sign if exponent and multiplies
	if(number->getName()=="Exponent"){
        tr1::shared_ptr<Exponent> givenNumber = tr1::static_pointer_cast<Exponent>(number);
		if(givenNumber->getSign()=='-'){
			tr1::shared_ptr<AbstractNumber> r(new Exponent(givenNumber->getValue("base"), givenNumber->getValue("power")));
			return multiply(r);
		}
		else{
            tr1::shared_ptr<AbstractNumber> nOne(new SmartInteger(-1));
            tr1::shared_ptr<AbstractNumber> newPower = givenNumber->getValue("power")->multiply(nOne);
			tr1::shared_ptr<AbstractNumber> r(new Exponent(givenNumber->getValue("base"), newPower, givenNumber->getSign()));
			return multiply(r);
		}
	}

	// Subtracts 1 from power if base = number
	else if(number->toDouble() == base->toDouble()){
		tr1::shared_ptr<AbstractNumber> c(new SmartInteger(-1));
		tr1::shared_ptr<AbstractNumber> r(new Exponent(base, power->add(c), this->calcSign(number)));
		return r;
	}
	else if(number->getName() == "MultExpression")
    {
        tr1::shared_ptr<MultExpression> MultE = tr1::static_pointer_cast<MultExpression>(number);
        vector<tr1::shared_ptr<AbstractNumber> > MultENum = MultE->getNumerator();
        vector<tr1::shared_ptr<AbstractNumber> > MultEDem = MultE->getDenominator();
        if (MultEDem.size() == 0)
        {
            tr1::shared_ptr<AbstractNumber> one(new SmartInteger(1));
            MultEDem.push_back(one);
        }
        tr1::shared_ptr<AbstractNumber> reversedMultE(new MultExpression(MultEDem, MultENum, '+'));
        return reversedMultE->multiply(shared_from_this());
    }

    vector< tr1::shared_ptr<AbstractNumber> > NumVector;
    tr1::shared_ptr<AbstractNumber> me(new Exponent(base, power, sign));
    NumVector.push_back(me);
    vector< tr1::shared_ptr<AbstractNumber> > DenVector;
    DenVector.push_back(number);
    tr1::shared_ptr<AbstractNumber> r(new MultExpression(NumVector, DenVector, this->calcSign(number)));
    return r;
}
开发者ID:kylelin47,项目名称:COP-Project,代码行数:51,代码来源:Exponent.cpp

示例3: main


//.........这里部分代码省略.........
                    					input2.replace(found, std::string("ans").length(), "(" + history + ")");

                    					tr1::shared_ptr<AbstractNumber> num(new SumExpression(input2,true));
                    					historyAns = num->simplify();
                    					cout << "Result: \n" << historyAns->toString() << endl;
                    					ansHistory.push_back(historyAns);
                    					history = input2;
                    					expHistory.push_back(history);
                    					}
                    					else
                    					{
                    						tr1::shared_ptr<AbstractNumber> num(new SumExpression(input2,true));
                    						historyAns = num->simplify();
                    						cout << "Result: \n" << historyAns->toString() << endl;
                    						ansHistory.push_back(historyAns);
                    						history = input2;
                    						expHistory.push_back(history);
                    					}


                    				}
                    				catch(exception &msg)
                    				{

                    					cout << "***ERROR: "<< msg.what() << "***"  << endl;
                    				}



                    		}
                    	}
                    else
                    {
                    	cout << "Result: Invalid Expression\n" << endl;
                    }
                }


                break;
            }
            case 2:
                printHelp();

                break;
            case 3:
            	if(ansHistory.size() == 0)
            	{
            		cout << "Please enter some expressions before viewing this option." << endl;
            	}
            	else
            	{
            	cout << "The previous answer was: " << historyAns->toString() << endl;
            	cout << "The previous answer as a decimal: " << historyAns->toDouble() << endl;
            	cout << "This answer may be used in further calculations by using the keyword 'ans'" << endl;
            	cout << "\nThe previous expressions and answers calculated are as follows: " << endl;

            	for(int i=0; i<ansHistory.size(); i++)

            	{

            		cout << "(#" << (ansHistory.size()-i) << ")" << "Expression: " << expHistory[i] << endl;
            		cout << "Answer: " << ansHistory[i]->toString() << "\n" << endl;

            	}




            		cout << "To set 'ans' to a previous expressions's answer, please enter the corresponding digit: " << endl;
            		cin>>setAns;

            		if(!( (1 <= setAns) && (setAns <= ansHistory.size()) ))
            		{

            			cin.clear();
            			while (cin.get() != '\n');
            			cout << "\nNot a valid menu option. Please enter 1-10." << endl;
            			continue;
            		}

            			else if(((1 <= setAns) && (setAns <= ansHistory.size()))== 1)
            			{
            			history = ansHistory[ansHistory.size() - setAns]->toString();


            		}
            		else
            		{
            			cout << "\nNot a valid menu option. Please enter 1-10." << endl;
            		}

            	}
                break;
            case 4:
            	cout << "The calculator will now exit. Thank you!" << endl;
                menuLoop = false;
                break;
        }
    }
}
开发者ID:kylelin47,项目名称:COP-Project,代码行数:101,代码来源:main.cpp


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