在C++中使用类进行复数的加法和减法运算

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

本文关键字:运算 C++      更新时间:2023-10-16

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

#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_stream),我甚至没有得到这些错误。

我遇到的另一个问题是函数void::复杂打印。cout内部应该有一个条件。不,如果有的话。但我不知道该怎么办。
程序必须按如下方式运行:
输入操作数一的实数部分:5
为操作数一输入虚部:2(不应写入虚部的i)
输入操作数二的实数部分:8
为操作数二输入虚部:1(同样,不应输入i)
/然后它将打印输入(ed)数字/
(5,2i)//这次带有i
(8,1i)
/然后是答案/
总和为13+3i
差值为-3,1i//或-3,i

请帮帮我!我是C++的新手,现在在stackoverflow,非常感谢您的帮助。非常感谢!

cout<lt;"总和是"<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) {};

最后,你可能希望考虑运算符重载——我相信你可以在谷歌上找到一个合理的教程。

在main中,在调用result.add之后,当cout流不返回任何内容时,您会将相同的函数放入该流中。我想你是想写cout<lt;"总和是"<lt;result.print();

您已经在使用std::命名空间。只需使用其中的复数库,如下所示:使用类添加复数

#include <iostream>
using namespace std;
class Complex {
    public:
    double r; 
    double i; 
    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 <<")";
}
int 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();
}