?? quad.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 + -