使用C ++中的类添加和减去复数

时间:2012-02-16 08:38:22

标签: c++ class object structure

我这里有一个代码,应该向用户询问两组实数和虚数。

#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;
}

然而,当我编译程序时,显示了很多错误(std :: basic_ostream),我甚至都没有。

我遇到的另一个问题是函数void :: Complex print。 cout本身应该有一个条件。不,如果 - 否则。但我不知道该怎么做。
程序必须像这样运行:
为操作数1输入实部:5
为操作数1输入虚部:2(想象的i不应写入) 为操作数二输入实部:8
输入操作数二的虚部:1(再次,我不应该输入)
/ 然后它将打印输入(ed)数字 /
(5,2i)//这次是用i (8,1i)
/ 然后是答案 /
总和是13 + 3i 差异是-3,1i。 //或-3,我是

请帮帮我!我是C ++的新手,在这里是stackoverflow,非常感谢你的帮助。非常感谢你!

5 个答案:

答案 0 :(得分:2)

该行

cout&lt;&lt; “总和是”&lt;&lt; result.add&lt;&lt; ENDL;

是不正确的,因为add是一种方法,因此result.add将是指向该方法的指针,并且cout不知道如何处理它 - 这使得编译器将其吐出

将行更改为

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

你需要为行

做同样的事情
cout << "The difference is " << result.subtract << endl;

对于编码风格,这两种方法都会覆盖现有的复数。也许拥有这样的功能会更好

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

这将使您能够将加法链接在一起,并且只需将复数加到现有的复数中。

此外,您可以将类变量ri设为私有。这将需要一个替代构造函数:

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

最后,您可能希望考虑运营商重载 - 我相信您可以谷歌那找到一个合理的教程。

答案 1 :(得分:0)

在main中,在调用result.add之后,如果没有返回任何内容,则在cout流中放入相同的函数。我想你打算写cout&lt;&lt; “总和是”&lt;&lt; result.print();

答案 2 :(得分:0)

稍作修改再试一次     

    #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();
    }

答案 3 :(得分:0)

您已经在使用std :: namespace。只需使用其中的复数库,如答案所示:Addition of complex numbers using classes

答案 4 :(得分:0)

我也在使用复数,这是我的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;
};

.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;
}