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

发布于 2025-01-06 21:18:48 字数 1700 浏览 0 评论 0原文

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

#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 本身内部应该有一个条件。没有 if-else。但我不知道该怎么办。
该程序必须像这样运行:
输入操作数一的实部:5
输入操作数一的虚部:2(虚数的i不要写)
输入操作数二的实部:8
输入操作数二的虚部:1(同样,不应输入 i)
/然后它将打印输入的数字/
(5, 2i) // 这次带有 i
(8, 1i)
/然后是答案/
总和是 13+3i。
差值是-3、1i。 //或-3,i

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清旖 2025-01-13 21:18:48

该行

cout << “总和是”<<结果.add <<结束;

不正确,因为 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) {};

最后,您可能希望考虑运算符重载 - 我相信您可以通过谷歌搜索找到合理的教程。

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.

晌融 2025-01-13 21:18:48

在 main 中,调用 result.add 后,当它不返回任何内容时,您将相同的函数放入 cout 流中。我认为你的意思是写 cout << “总和是” <<结果.print();

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

北风几吹夏 2025-01-13 21:18:48

您已经在使用 std:: 命名空间。只需使用其中的复数库,就像这个答案建议的那样: 添加复数使用类的数字

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

拒绝两难 2025-01-13 21:18:48
#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();
}
#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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文