[*]Witam.
Mam zadanie napisać program który oblicza wartość wielomianu w punkcie zespolonym bez wykonania operacji na liczbach zespolonych.
Czy ktoś może z tym pomoc?
[C++][Algorytmy] Wartość wielomianu w punkcie zespolonym.
-
- Użytkownik
- Posty: 1
- Rejestracja: 6 sty 2017, o 14:19
- Płeć: Mężczyzna
- Lokalizacja: Warszawa
-
- Użytkownik
- Posty: 1567
- Rejestracja: 15 gru 2008, o 16:38
- Płeć: Mężczyzna
- Lokalizacja: Ostrzeszów/Wrocław
- Podziękował: 8 razy
- Pomógł: 398 razy
[C++][Algorytmy] Wartość wielomianu w punkcie zespolonym.
Moja implementacja w C++11 (w g++ /MinGW trzeba uzywac flagi -std=c++11):
Kod: Zaznacz cały
#include <iostream>
#include <utility> //for move
#include <vector> // for initializer_list
class Complex {
private:
double _x{};
double _y{};
public:
Complex(double x, double y): _x(x), _y(y) {}
Complex(): Complex(0.0, 0.0) {}
Complex(double x): Complex(x, 0.0) {}
Complex(const Complex &other): _x(other._x), _y(other._y) {}
Complex(Complex &&other): _x(std::move(other._x)), _y(std::move(other._y)) {}
~Complex() = default;
double real() const {
return _x;
}
double imag() const {
return _y;
}
Complex operator+(const Complex &other) const {
return Complex(_x+other._x, _y+other._y);
}
Complex operator+=(const Complex &other) {
_x += other._x;
_y += other._y;
return *this;
}
Complex operator-(const Complex &other) const {
return Complex(_x-other._x, _y-other._y);
}
Complex operator-=(const Complex &other) {
_x -= other._x;
_y -= other._y;
return *this;
}
Complex operator-() const {
return Complex(-_x,-_y);
}
Complex operator*(const Complex &other) const {
double real{ _x*other._x - _y*other._y};
double imag{ _x*other._y + _y*other._x};
return Complex(real, imag);
}
Complex operator*=(const Complex &other) {
double tmpx{ _x*other._x - _y*other._y};
double tmpy{ _x*other._y + _y*other._x};
_x = tmpx;
_y = tmpy;
return *this;
}
Complex power(unsigned int n) const {
if (n == 0) return Complex(1.0, 0.0);
else if (n == 1) return Complex(*this);
Complex tmp{this->power(n/2)};
tmp *= tmp;
return tmp * this->power(n%2);
}
Complex& operator=(const Complex &other) {
_x = other._x;
_y = other._y;
return *this;
}
Complex& operator=(Complex &&other) {
_x = std::move(other._x);
_y = std::move(other._y);
return *this;
}
friend std::ostream& operator<<(std::ostream &os, const Complex &cplx);
};
std::ostream& operator<<(std::ostream &os, const Complex &cplx) {
os << '(' << cplx._x << "+" << cplx._y << "j" << ')';
return os;
}
class Polynomial {
private:
unsigned int _order{};
std::vector<Complex> _coeffs{};
public:
Polynomial(unsigned int n, std::initializer_list<Complex> lst)
:_order(n)
,_coeffs()
{
unsigned int i{0};
for (auto x: lst) {
_coeffs.push_back(x);
++i;
}
}
Complex operator()(const Complex &val) {
Complex result{};
for (unsigned int i=0; i < _coeffs.size(); i++) {
result += _coeffs[i]*val.power(i);
}
return result;
}
};
int main() {
Complex cplx{0, 1};
Polynomial P{2, {0, 0, 1}};
std::cout << P(cplx) << std::endl;
return 0;
}