-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task9.cpp
58 lines (50 loc) · 1.53 KB
/
Task9.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "Task9.h"
Task9::Task9() : _a(0), _b(0), _c(0), _d(0), _sol1(0), _sol2(0), _isvalid(false) {}
Task9::Task9(float a, float b, float c, float d)
: _a(a), _b(b), _c(c), _d(d), _sol1(0), _sol2(0), _isvalid(false) {}
Task9::Task9(const Task9 &t)
: _a(t._a), _b(t._b), _c(t._c), _d(t._d), _sol1(t._sol1), _sol2(t._sol2), _isvalid(t._isvalid){};
Task9::~Task9() {}
void Task9::solve()
{
if (_a == _c)
{
Task1 t2(-_a - _c, 0, _b + _d);
_sol2 = t2.solve();
}
else if ((-1 * _a) == _c)
{
Task1 t2(_a - _c, 0, -_b + _d);
_sol2 = t2.solve();
}
else
{
Task1 t1(-_a - _c, 0, _b + _d);
_sol1 = t1.solve();
_isvalid = true;
Task1 t2(_a - _c, 0, -_b + _d);
_sol2 = t2.solve();
}
}
bool Task9::isValidSolution(double sol)
{
if(fabs(_a*sol + _b) == (_c*sol + _d))
return true;
else
return false;
}
ostream &operator<<(ostream &os, Task9 &t)
{
t.solve();
if (t._isvalid && t.isValidSolution(t._sol1))
os << fixed << setprecision(4) << "x = " << t._sol1 << ", " << t._sol2 << endl;
else if(t.isValidSolution(t._sol2))
os << setprecision(4) << "x = " << t._sol2 << endl;
else
os << "No valid solution for the given equation! " << endl;
return os;
}
void Task9 ::print()
{
cout << *this;
}