简体   繁体   English

使用C ++中的类对复数进行加法和减法

[英]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. 但是,当我编译程序时,会显示很多错误(std :: basic_ostream),我什至没有得到。

Another issue I'm having is in the function void::Complex print. 我遇到的另一个问题是函数void :: Complex print。 There should be a condition inside cout itself. cout本身内部应该有条件。 No if-else. 不,如果不是。 But I have no idea what to do. 但是我不知道该怎么办。
The program must run like this: 该程序必须像这样运行:
Input real part for operand one: 5 输入操作数一的实数部分:5
Input imaginary part for operand one: 2 (the i for imaginary shouldn't be written) 输入操作数一的虚部:2(虚部的i不应该写)
Input real part for operand two: 8 输入操作数二的实数部分:8
Input imaginary part for operand two: 1 (again, i shouldn't be entered) 输入操作数二的虚部:1(再次,我不应输入)
/ then it will print the input(ed) numbers / / 然后将打印输入的数字 /
(5, 2i) //this time with an i (5,2i)//这次是一个i
(8, 1i) (8,1i)
/ then the answers / / 然后答案 /
The sum is 13+3i. 总和是13 + 3i。
The difference is -3, 1i. 区别是-3,1i。 //or -3, i //或-3,我

Please help me! 请帮我! I'm new in C++ and here in stackoverflow and your help would be very appreciated. 我是C ++和Stackoverflow的新手,我们将非常感谢您的帮助。 Thank you very much! 非常感谢你!

The line 线

cout << "The sum is " << result.add << endl; cout <<“总和为” << 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. 是不正确的,因为add是一种方法,所以result.add将是该方法的指针,而cout不知道如何处理它-这会使编译器将其吐出。

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. 另外,您可以将类变量ri私有。 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. 最后,您不妨考虑运算符重载-我相信您可以在Google上找到合适的教程。

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. 您已经在使用std ::名称空间。 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: 我也在使用Complex Numbers,这是我的ComplexNumbers.h文件:

#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: .cpp文件是这样的:

#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. 在main中,调用result.add之后,如果不返回任何内容,则将相同的函数放入cout流中。 I think you meant to write cout << "the sum is " << result.print(); 我想你是想写cout <<“总和” << result.print();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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