root.cpp
来自「钱能的《C++程序设计习题及解答》配套代码」· C++ 代码 · 共 72 行
CPP
72 行
// Root.cpp: implementation of the Root class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Root.h"
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
#include "Real.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Root::Root( double m1, double m2, double m3 )
:
a(m1), b(m2), c(m3)
{
}
Root::~Root()
{
}
void Root::Print()
{
cout << "方程";
cout << a;
cout << setiosflags( ios::showpos );
cout << "*X*X";
cout << b << "*X";
cout << c << " = 0\n";
cout << resetiosflags( ios::showpos );
}
void Root::Solve()
{
Print();
cout << "的解是:\n";
double delta = b * b - 4 * a * c;
if ( fabs(delta) < 1e-5 )
{
Real x( -b/2/a );
cout << "\nX1=X2=";
x.Print();
}
else if ( delta > 0 )
{
Real x1( -b/2/a + sqrt(delta)/2/a );
Real x2( -b/2/a - sqrt(delta)/2/a );
cout << "\nX1 = ";
x1.Print();
cout << "\nX2 = ";
x2.Print();
}
else
{
Complex x1( -b/2/a, sqrt(-delta)/2/a );
Complex x2( -b/2/a, -sqrt(-delta)/2/a );
cout << "\nX1 = ";
x1.Print();
cout << "\nX2 = ";
x2.Print();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?