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


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

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


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

示例1: main

int main()
{
	Polynomial poly;

	/*Polynomial poly2;
	Polynomial poly3(0);

	cout << "P1: ";
	poly.display();

	cout << "\nP2: ";
	poly2.display();

	cout << "\nP1 + P2: ";
	poly3 = poly + poly2;
	poly3.display();

	cout << "\nP1 - P2: ";
	poly3 = poly - poly2;
	poly3.display();

	cout << "\nP1 * P2: ";
	poly3 = poly * poly2;
	poly3.display();

	cout << "\nP3 += P1: ";
	poly3 += poly;
	poly3.display();

	cout << "\nP3 -= P2: ";
	poly3 -= poly;
	poly3.display();

	cout << endl << endl << endl;

	*/Polynomial poly4(0);/*
	Polynomial poly5;
	Polynomial poly6 = poly4 / poly5;

	poly6.display();
	poly6.getRemainder();*/

	poly.factor(poly);

	cout << endl;

	return 0;
}
开发者ID:Joe-McCann,项目名称:My-Projects,代码行数:48,代码来源:Driver.cpp

示例2: if

// O(n^2 + m^2) from factor
// O(n log n) sort
// O(n * m) adding
Polynomial Polynomial::operator +(Polynomial& rhs)
{
	/*
	PRE-CONDITIONS:
	The two polynomials have been entered in the correct format.  Factoring called in-function.

	POST-CONDITIONS:
	The polynomial will combine all like terms from both of the polynomials and return the result as a new polynomial.
	*/

	Polynomial newPoly;
	bool foundLikeTerm;
	int newCoefficient;

    // factor polynomials before adding them together
    factor();
    rhs.factor();

	// begin comparing terms from each Polynomial and combining them
	for (list<Term>::iterator iter = termList.begin(); iter != termList.end(); iter++)
	{
		foundLikeTerm = false;
		// compare each term from poly1 with every term from poly2
		for (list<Term>::iterator iter2 = rhs.termList.begin(); iter2 != rhs.termList.end();)
		{
			if (iter->getHasVariable() == true && iter->getExponent() == iter2->getExponent())
			{
				// If there is a like term, add the coefficients together and add to new poly list
				foundLikeTerm = true;
				newCoefficient = iter->getCoefficient() + iter2->getCoefficient();
                // Advance iterator and delete current term
				iter2 = rhs.termList.erase(iter2);

				if (newCoefficient != 0)
					// Add combined term to new polynomial
					newPoly.addTermToList(Term(newCoefficient, iter->getExponent()));
			}
			else if (iter->getHasVariable() == false && iter2->getHasVariable() == false)
			{
				foundLikeTerm = true;
				newCoefficient = iter->getCoefficient() + iter2->getCoefficient();
                // Advance iterator and delete current term
				iter2 = rhs.termList.erase(iter2);
                // Add combined term to new polynomial if it is anything but zero.
				if (newCoefficient != 0)
					newPoly.addTermToList(Term(newCoefficient));
			}
			else{
				iter2++;
			}
		}
		// If there are no terms to be combined with, add term as it is.
		// drop stand alone zeros from the polynomial
		if (foundLikeTerm == false && iter->getCoefficient() != 0)
			newPoly.addTermToList(*iter);
	}

	// add remaining terms from poly2 that had no matching terms in poly1
	for (list<Term>::iterator iter2 = rhs.termList.begin(); iter2 != rhs.termList.end(); iter2++)
		newPoly.addTermToList(*iter2);

    // Sort (ascending order) and reverse to get it descending
    newPoly.termList.sort(greater<Term>());

	return newPoly;
}
开发者ID:btoles,项目名称:poly,代码行数:69,代码来源:Polynomial.cpp


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