C++中使用类的复数加减法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9307942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Addition and subtraction of complex numbers using class in C++
提问by FauChristian
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. 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
我遇到的另一个问题是在函数 void::Complex 打印中。cout 本身应该有一个条件。没有if-else。但我不知道该怎么做。
程序必须这样运行:
输入操作数 1 的实部: 5
输入操作数 1 的虚部: 2 (虚数的 i 不应该写)
输入操作数 2 的实部: 8
输入操作数 2 的虚部: 1 (同样,我不应该被输入)
/然后它会打印输入的(ed)数字/
(5, 2i)//这次是一个 i
(8, 1i)
/然后答案/
总和是 13+3i .
区别是-3, 1i。//或-3,我
Please help me! I'm new in C++ and here in stackoverflow and your help would be very appreciated. Thank you very much!
请帮我!我是 C++ 新手,这里是 stackoverflow,非常感谢您的帮助。非常感谢!
回答by Ed Heal
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. This will require an alternative constructor:
此外,你可以使类变量r
和i
私有。这将需要一个替代的构造函数:
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.
最后,您可能希望考虑运算符重载 - 我相信您可以通过谷歌搜索找到合理的教程。
回答by Tariq
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();
}
回答by FauChristian
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
您已经在使用 std:: 命名空间。只需像这个答案建议的那样使用其中的复数库:Addition of complex numbers using classes
回答by realgenob
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();
在 main 中,在调用 result.add 之后,当它不返回任何内容时,您将相同的函数放入 cout 流中。我想你的意思是写 cout << "the sum is " << result.print();