@im_noob

Как вызвать класс комплексное число?

Как сделать так, чтобы выводился результат комплексного числа? Допустим я ввожу:


Enter count of elements: 3
Enter real: 1
Enter imaginary: 2
Enter real: 3
Enter imaginary: 4
Enter real: 5
Enter imaginary: 6
[здесь должен выводится результат расчет комплексного числа]
(1 + 2i) * x^1 + (3 + 4i) * x^2 + (5 + 6i) * x^3 // это комплексный полином
и получаю 9+12i. Как сделать именно этот вывод?

Класс Комплексное число
#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  
{
	
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();
}
  • Вопрос задан
  • 119 просмотров
Решения вопроса 1
@res2001
Developer, ex-admin
Для получения 9+12i надо вызвать computePolynomial(), его результат вывести на экран.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы