Тема: Класс Комплексне число
Як правильно організувати виведення та розрахунок комплексного числа? Комплексний поліном вивести можу, а ось комплексне число ні.
Результат повинен бути щось кшталт цього.
Код Complex.h
#pragma once
#include <iostream>
class Complex
{
private:
float real;
float imaginary;
public:
Complex() {
this->imaginary = 0;
this->real = 0;
}
Complex(float real, float imaginary) {
this->real = real;
this->imaginary = imaginary;
}
Complex(const Complex& complex) {
this->imaginary = complex.imaginary;
this->real = complex.real;
}
void setReal(float real) {
this->real = real;
}
void setImaginary(float imaginary) {
this->imaginary = imaginary;
}
float getReal() {
return real;
}
float getImaginary() {
return imaginary;
}
virtual void show() {
std::cout << this->getReal() << " + " << this->getImaginary() << "i";
}
virtual void input() {
std::cout << "Enter real: ";
std::cin >> this->real;
std::cout << "Enter imaginary: ";
std::cin >> this->imaginary;
}
void operator += (Complex complex)
{
this->setReal(this->getReal() + complex.getReal());
this->setImaginary(this->getImaginary() + complex.getImaginary());
}
Complex operator * (int number)
{
return Complex(this->getReal() * number, this->getImaginary() * number);
}
};
Код Main.cpp
#include <iostream>
#include "Complex.h"
class Polynomial : public Complex
{
private:
int power;
Complex* koeff;
public:
Polynomial()
{
this->power = 0;
this->koeff = NULL;
}
Polynomial(int power, int countOfKoeff, Complex* koeff)
{
this->power = power;
this->koeff = new Complex[countOfKoeff];
for (int i = 0; i < this->getPower(); i++)
{
this->koeff[i] = koeff[i];
}
}
~Polynomial()
{
delete[] koeff;
}
int getPower()
{
return this->power;
}
void setKoeff(Complex * complex, int count)
{
this->koeff = new Complex[count];
this->power = count;
for (int i = 0; i < count; i++)
this->koeff[i] = complex[i];
}
void input() {
int n = 0;
float r, im;
std::cout << "Enter count of elements: ";
std::cin >> n;
Complex* pc = new Complex[n];
for (int i = 0; i < n; ++i) {
std::cout << "Enter real: ";
std::cin >> r;
std::cout << "Enter imaginary: ";
std::cin >> im;
pc[i] = Complex(r, im);
}
this->power = n;
this->koeff = pc;
}
void show()
{
for (int i = 0; i < this->getPower(); i++)
{
std::cout << "(";
this->koeff[i].show();
std::cout << ")";
std::cout << " * " << "x^" << i + 1;
if (i == this->getPower() - 1)
std::cout << " ";
else
std::cout << " + ";
}
}
Complex computePolynomial()
{
Complex complex;
for (int i = 0; i < this->getPower(); i++)
{
complex += this->koeff[i];
}
return complex;
}
};
int main() {
Polynomial polynom;
polynom.input();
polynom.show(); //виведення комплексного поліному
// std::cout << "\n";
//Complex complex; //тут я намагаюся викликати класс комплексне число, але маю результат 0
//complex.show();
}