⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quad.cpp

📁 多项式与常数和多项式之间的加减乘除等运算
💻 CPP
字号:
// quad.cpp: implementation of the quad class.
//
//////////////////////////////////////////////////////////////////////

#include "quad.h"
#include <cmath>
//#include <string>

using namespace std;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////
// default constructor
//
// forces size = 3
//////////////////////////////////////////////////////////////////////
quad::quad():
poly(3)
{
	
}

//////////////////////////////////////////////////////////////////////
// constructor
//
// forces size = 3 and assigns passes values to coefficients
// NOTE: coefficients are passed in 'normal' format, highest first
//////////////////////////////////////////////////////////////////////
quad::quad(double values[]):
poly(3, values)
{

}

quad::~quad()
{

}


//////////////////////////////////////////////////////////////////
//	getRoots
//
//	finds roots for quadratic, assignes them to complex objects
//	and encapsulates into a 'root' object holding a message, the
//	number of roots and the root(s), print function provided for 
//	roots.  Algorithm determines form and number of roots then solves
//
//	parameters:		none
//	return type:	'root' object
//////////////////////////////////////////////////////////////////
root quad::getRoots()
{
	double r;				//temp object
	double s;				//temp object
	complex root1;			//complex number for root
	complex root2;			//complex number for root
	root rObj;				//root object for return
	
	poly::normalise();		//ensure highest coefficient is one
	r = ((pow(data[1],2))/4) - data[0];// calculate p^2/4-q
	s = -data[1]/2;					// calculate -p/2

	if (r>0)				// two real roots
	{
		rObj.setMsg("two_roots");	// set message
		root1.setReal(s + sqrt(r));//set first root value
		root2.setReal(s - sqrt(r));//set second root value
		rObj.setNumRoots(2);		//set number of roots
		rObj.setRoot(0,root1);//encapsulate root into root object
		rObj.setRoot(1,root2);//encapsulate root into root object
	}

	else if (r==0)			//one real root
	{
		rObj.setMsg("single_root");//message
		root1.setReal(s);		//set root value
		rObj.setNumRoots(1);	//set number of roots
		rObj.setRoot(0, root1);	//encapsulate root into root object
	}
	else if (r<0)			//two complex roots
	{
		rObj.setMsg("complex_roots");//message
		rObj.setNumRoots(2);	//number of roots
		r = - r;				//adapt for square root of negative
		root1.setReal(s);		//set real part of root
		root1.setImag(sqrt(r));	//set imaginary part of root
		root2.setReal(s);		//set real part of root
		root2.setImag(-sqrt(r));//set imaginary part of root
		rObj.setRoot(0,root1);	//encapsulate root into root object
		rObj.setRoot(1,root2);	//encapsulate root into root object
	}
	return rObj;			//return root object
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -