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


C++ Polynomial::getNumTerms方法代码示例

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


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

示例1: assert

Polynomial::Polynomial(const Polynomial& that)
  : numVars_(that.numVars_) {
  PowersToCoeffMap::const_iterator iter;
  for (iter = that.terms_.begin(); iter != that.terms_.end(); ++iter) {
    assert(hasSameNumVars(iter->first));
    assert(isValidCoeff(iter->second));
    terms_.insert(PowersToCoeffMap::value_type(iter->first, iter->second));
  }
  assert(getNumTerms() == that.getNumTerms());
}
开发者ID:Peter-Collins,项目名称:NormalForm,代码行数:10,代码来源:Polynomial.cpp

示例2: factor

// O(n^2) factoring 
void Polynomial::factor()
{
	/*
	PRE-CONDITIONS: 
	The polynomial is of the correct format.

	POST-CONDITIONS: 
	The polynomial will combine all like terms in preparation for either output or addition.
	*/
	Polynomial newPoly;
	Term currentTerm;
	int newCoefficient;

    // Loop while there is a term left to check
	while (termList.size() > 0)
	{
		bool foundLikeTerm = false;

        // If there are no terms in the new polynomial add the first term
		if (newPoly.getNumTerms() == 0)
		{
            // If the exponent is anything but 0, add the term as is.
            if (termList.front().getExponent() != 0)
            {
                newPoly.addTermToList(termList.front());
                termList.pop_front();
            }
            else{
                // If the exponent is zero, drop the variable and add the coefficient
                newPoly.addTermToList(Term(termList.front().getCoefficient()));
                termList.pop_front();
            }
		}
		else
        {
            // Set new current term to be checked against the new polynomial
            currentTerm = termList.front();

            // If the exponent of the term is zero, drop the variable
            if (currentTerm.getExponent() == 0)
                currentTerm = Term(termList.front().getCoefficient());

            // Iterate through the new polynomial to check for matches of exponents
            // If the exponents match add the terms together, else add to the end of the new poly
			for (list<Term>::iterator iter = newPoly.termList.begin(); iter != newPoly.termList.end();)
			{
                // If the exponents match or they are both numbers with no variables, add them together
				if ((iter->getExponent() == currentTerm.getExponent()) || (iter->getHasVariable() == false && currentTerm.getHasVariable() == false))
				{
					newCoefficient = iter->getCoefficient() + currentTerm.getCoefficient();
					iter->setCoefficient(newCoefficient);
                    foundLikeTerm = true;
                    break;
                }
				else{
                    // Advance iterator if no matches are found on the current term
					iter++;
				}
			}

            // If it gets through the new poly without a match, add it to the new poly
            if(foundLikeTerm == false)
                newPoly.addTermToList(currentTerm);

            termList.pop_front();

		}
	}

    // Clear everything from old polynomial, update with the new polynomial
    termList = newPoly.termList;
}
开发者ID:btoles,项目名称:poly,代码行数:73,代码来源:Polynomial.cpp


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