简体   繁体   中英

Addition and subtraction of complex numbers using class in C++

I have here a code that is supposed to ask the user two sets of real and imaginary numbers.

#include <iostream>

using namespace std;

class Complex {
    public:
        double r;
        double i;
    public:
        Complex();
        void add(Complex, Complex);
        void subtract(Complex, Complex);
        void print();
};



Complex::Complex() {
    r = i = 0;
}

void Complex::add (Complex op1, Complex op2) {
    r = op1.r+op2.r;
    i = op1.i+op2.i;
}

void Complex::subtract (Complex op1, Complex op2) {
     r = op1.r-op2.r;
     i = op1.i-op2.i;
}

void Complex::print () {
    cout << r << i;
}

int main () {
    Complex operand1, operand2, result;
    cout << "Input real part for operand one: " << endl;
    cin >> operand1.r;
    cout << "Input imaginary part for operand one: " << endl;
    cin >> operand1.i;
    cout << "Input real part for operand two: " << endl;
    cin >> operand2.r;
    cout << "Input imaginary part for operand two: " << endl;
    cin >> operand2.i;
    result.add(operand1, operand2);
    cout << "The sum is " << result.add << endl;
    result.subtract(operand1, operand2);
    cout << "The difference is " << result.subtract << endl;
}

However, when I compiled the program, lots of errors are displayed (std::basic_ostream) which I don't even get.

Another issue I'm having is in the function void::Complex print. There should be a condition inside cout itself. No if-else. But I have no idea what to do.
The program must run like this:
Input real part for operand one: 5
Input imaginary part for operand one: 2 (the i for imaginary shouldn't be written)
Input real part for operand two: 8
Input imaginary part for operand two: 1 (again, i shouldn't be entered)
/ then it will print the input(ed) numbers /
(5, 2i) //this time with an i
(8, 1i)
/ then the answers /
The sum is 13+3i.
The difference is -3, 1i. //or -3, i

Please help me! I'm new in C++ and here in stackoverflow and your help would be very appreciated. Thank you very much!

The line

cout << "The sum is " << result.add << endl;

is incorrect, as add is a method so result.add will be a pointer to that method, and cout does not know how to handle it - which makes the compiler spit it out.

Change the line to

cout << "The sum is ";
result.print();
cout << endl;

You need to do the same for the line

cout << "The difference is " << result.subtract << endl;

As to coding style, the two methods are overwrting an existing complex number. Perhaps having a the function like this would be better

Complex &Complex::add (const Complex &op) { 
    r += op.r; 
    i += op.i;
    return *this;
}

This will enable you to chain additions together and also just add a complex number to the existing complex number.

In addition you could make the class variables r and i private. This will require an alternative constructor:

Complex:Complex(double real, double imaginary) : r(real), i(imaginary) {};

Finally you may wish to consider operator overloading - I am sure you can google that to find a reasonable tutorial.

Try again with slight corrections

    #include <iostream.h>
    class Complex {
        public:
        double r; //real part
        double i; //imaginary part
        public:
        void add(Complex, Complex);
        void subtract(Complex, Complex);
        void print();
    };

    void Complex::add (Complex op1, Complex op2) {
        r = op1.r + op2.r;
        i = op1.i + op2.i;
    }

    void Complex::subtract (Complex op1, Complex op2) {
         r = op1.r - op2.r;
         i = op1.i - op2.i;
    }

    void Complex::print () {
        cout << "("<<r<<", " << i <<")";
    }

    void main () {
        Complex operand1, operand2, result;
        cout << "\nInput real part for operand one: " << endl;
        cin >> operand1.r;
        cout << "Input imaginary part for operand one: " << endl;
        cin >> operand1.i;
        cout << "Input real part for operand two: " << endl;
        cin >> operand2.r;
        cout << "Input imaginary part for operand two: " << endl;
        cin >> operand2.i;
        cout << "\nThe sum is ";
        result.add(operand1, operand2);
        result.print();
        cout << "\nThe difference is ";
        result.subtract(operand1, operand2);
        result.print();
    }

You are already using the std:: namespace. Just use the complex number library in it like this answer suggests: Addition of complex numbers using classes

I'm also working with Complex Numbers, here's my ComplexNumbers.h file:

#include <iostream> // for std namespace
class ComplexNumber
{
    public:
        ComplexNumber();
        ComplexNumber(float RealPart, float ImaginaryPart);
        ComplexNumber(ComplexNumber &NewComplexNumber);
        void SetRealPart(float RealPart);
        void SetImaginaryPart(float ImaginaryPart);
        friend ComplexNumber operator+(const ComplexNumber Complex1, const ComplexNumber Complex2);
        friend ComplexNumber operator-(const ComplexNumber Complex1, const ComplexNumber Complex2);
        friend std::ostream & operator<<(std::ostream &output, const ComplexNumber &NumberToDsiplay);
        friend std::istream & operator >>(std::istream &input, ComplexNumber &NumberToInput);
        bool operator==(const ComplexNumber Complex);
        bool operator!=(const ComplexNumber Complex);

    private:
        float RealPart;
        float ImaginaryPart;
};

and the .cpp file is this:

#include "Complex Numbers.h"

ComplexNumber::ComplexNumber()
{
    RealPart = 0;
    ImaginaryPart = 0;
}

ComplexNumber::ComplexNumber(float RealPart, float ImaginaryPart)
{
    SetRealPart(RealPart);
    SetImaginaryPart(ImaginaryPart);
}

ComplexNumber::ComplexNumber(ComplexNumber &NewComplexNumber)
{
    RealPart = NewComplexNumber.RealPart;
    ImaginaryPart = NewComplexNumber.ImaginaryPart;
}

void ComplexNumber::SetRealPart(float RealPart)
{
    this->RealPart=RealPart;
}

void ComplexNumber::SetImaginaryPart(float ImaginaryPart)
{
    this->ImaginaryPart=ImaginaryPart;
}

ComplexNumber operator+(const ComplexNumber Complex1, const ComplexNumber Complex2)
{
    ComplexNumber TemporaryComplexNumber;
    TemporaryComplexNumber.RealPart = Complex1.RealPart + Complex2.RealPart;
    TemporaryComplexNumber.ImaginaryPart = Complex1.ImaginaryPart + Complex2.ImaginaryPart;

    return TemporaryComplexNumber;
}

ComplexNumber operator-(const ComplexNumber Complex1, const ComplexNumber Complex2)
{
    ComplexNumber TemporaryComplexNumber;
    TemporaryComplexNumber.RealPart = Complex1.RealPart - Complex2.RealPart;
    TemporaryComplexNumber.ImaginaryPart = Complex1.ImaginaryPart - Complex2.ImaginaryPart;

    return TemporaryComplexNumber;
}


std::ostream & operator<<(std::ostream &output, const ComplexNumber &NumberToDsiplay)
{
    if(NumberToDsiplay.ImaginaryPart > 0)
        output << NumberToDsiplay.RealPart << "+" << NumberToDsiplay.ImaginaryPart << "i";
    else if(NumberToDsiplay.ImaginaryPart < 0)
        output << NumberToDsiplay.RealPart << "" << NumberToDsiplay.ImaginaryPart << "i";
    else
        output << NumberToDsiplay.RealPart << "+" << NumberToDsiplay.ImaginaryPart << "i";
    return output;
}

std::istream & operator >>(std::istream &input, ComplexNumber &NumberToInput)
{
    std::cout << "Enter the real part: ";
    input >> NumberToInput.RealPart;
    std::cout << "Enter the imaginary part: ";
    input >> NumberToInput.ImaginaryPart;
}

bool ComplexNumber::operator==(const ComplexNumber Complex)
{
    return RealPart==Complex.RealPart && ImaginaryPart==Complex.ImaginaryPart;
}

bool ComplexNumber::operator!=(const ComplexNumber Complex)
{
    if(RealPart != Complex.RealPart && ImaginaryPart != Complex.ImaginaryPart)
            return true;
    else if(RealPart != Complex.RealPart && (!(ImaginaryPart != Complex.ImaginaryPart)))
            return true;
    else if(ImaginaryPart != Complex.ImaginaryPart && (!(RealPart != Complex.RealPart)))
        return true;

    return false;
}

In main, after you call result.add, you put the same function in the cout stream when it doesn't return anything. I think you meant to write cout << "the sum is " << result.print();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM