Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > C/C++ программирование > Общие вопросы C/C++
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 11.10.2023, 20:13   #1
natatttt
 
Регистрация: 08.04.2023
Сообщений: 9
По умолчанию Класс Complex с перегрузкой операций

Помогите исправить ошибку в коде, неправильно выводит результат операций operator- и operator/

Вот сам код:

Код:
/*Определите в классе Complex следующие операции:
Complex& operator=(const Complex&); - копирование
Complex& operator+(const Complex&); - сложение
Complex& operator-(const Complex&); - вычитание
Complex& operator*(const Complex&); - умножение
Complex& operator/(const Complex&); - деление
Complex& operator+=(const Complex&); - сложение
Complex& operator-=(const Complex&); - вычитание
Complex& operator*=(const Complex&); - умножение
Complex& operator/=(const Complex&); - деление
friend ostream& operator<<(ostream& os, const Complex&); - вывод
friend istream& operator>>(istream& is, Complex&); - ввод */
 
#include <iostream>
using namespace std;
 
class Complex
{
private:
    double re;
    double im;
public:
    Complex() : re(0), im(0) {}
 
    Complex(double r, double i) : re(r), im(i) {}
 
    Complex(const Complex &c) : re(c.re), im(c.im) {}
 
    Complex& operator=(const Complex&c)
    {
        re = c.re;
        im = c.im;
        return *this;
    }
 
    Complex& operator+(const Complex&c)
    {
        re = re + c.re;
        im = im + c.im;
        return *this;
    }
 
    Complex& operator-(const Complex&c)
    {
        re = re - c.re;
        im = im - c.im;
        return *this;
    }
 
    Complex& operator*(const Complex&c)
    {
        double r = re * c.re - im * c.im;
        double i = re * c.im + im * c.re;
        re = r;
        im = i;
        return *this;
    }
 
    Complex& operator/(const Complex&c)
    {
        double d = c.re * c.re + c.im * c.im;
        double r = (re * c.re + im * c.im) / d;
        double i = (im * c.re - re * c.im) / d;
        re = r;
        im = i;
        return *this;
    }
 
    Complex& operator+=(const Complex&c)
    {
        re += c.re;
        im += c.im;
        return *this;
    }
 
    Complex& operator-=(const Complex&c)
    {
        re -= c.re;
        im -= c.im;
        return *this;
    }
 
    Complex& operator*=(const Complex&c)
    {
        double r = re * c.re - im * c.im;
        double i = re * c.im + im * c.re;
        re = r;
        im = i;
        return *this;
    }
 
    Complex& operator/=(const Complex&c)
    {
        double d = c.re * c.re + c.im * c.im;
        double r = (re * c.re + im * c.im) / d;
        double i = (im * c.re - re * c.im) / d;
        re = r;
        im = i;
        return *this;
    }
 
    friend ostream& operator<<(ostream& os, const Complex&a)
    {
        if (a.im < 0) os << a.re << a.im << "i" << endl;
        else os << a.re << "+" << a.im << "i" << endl;
        return os;
    }
 
    friend istream& operator>>(istream& is, Complex&a)
    {
        is >> a.re >> a.im;
        return is;
    }
 
    ~Complex() {}
};
 
int main()
{
    setlocale(LC_ALL, "rus");
    Complex compl1, compl2;
    cout << "Введите 1-е компл.число: ";
    cin >> compl1;
    cout << compl1 << endl;
    cout << "Введите 2-е компл.число: ";
    cin >> compl2;
    cout << compl2 << endl;
    Complex a = compl1 + compl2;
    cout << "1. Сложение: " << a;
    Complex b = compl1 - compl2;
    cout << "2. Вычитание: " << b;
    Complex c = compl1 * compl2;
    cout << "3. Умножение: " << c;
    Complex d = compl1 / compl2;
    cout << "4. Деление: " << d;
    cout << endl;
    Complex a1 = compl1;
    a1 += compl2;
    cout << "1. Сложение (сокращ.форма): " << a1;
    Complex b1(compl1);
    b1 -= compl2;
    cout << "2. Вычитание (сокращ.форма): " << b1;
    Complex c1(compl1);
    c1 *= compl2;
    cout << "3. Умножение (сокращ.форма): " << c1;
    Complex d1(compl1);
    d1 /= compl2;
    cout << "4. Деление (сокращ.форма): " << d1;
    return 0;
}
natatttt вне форума Ответить с цитированием
Старый 11.10.2023, 21:40   #2
p51x
Старожил
 
Регистрация: 15.02.2010
Сообщений: 15,709
По умолчанию

Операторы +,-,... в отличии от +=,-=, ... не должны менять свои параметры.
p51x вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
C#. Класс Array с Complex. jOBER Помощь студентам 1 21.04.2019 19:26
Класс Complex.Извлечение и помещение в поток. Otar4ik Помощь студентам 2 19.11.2014 20:55
Класс АТД. Перегрузка операций zuzuzuz Помощь студентам 1 30.10.2013 05:52
Создать класс Complex Anastasia2012 Помощь студентам 2 10.12.2012 18:20
Разработать класс Complex frixer Помощь студентам 0 03.05.2011 19:12