使用 C++ 中的类进行复数加法和减法;
我这里有一个代码,应该询问用户两组实数和虚数。
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该行
cout << “总和是”<<结果.add <<结束;
不正确,因为
add
是一个方法,因此result.add
将是指向该方法的指针,而cout
不知道如何处理它- 这使得编译器将其吐出。将行更改为
您需要对该行执行相同的操作
至于编码风格,这两种方法都会覆盖现有的复数。也许拥有这样的函数会更好。
这将使您能够将加法链接在一起,并且只需将一个复数添加到现有的复数中。
此外,您还可以将类变量
r
和i
设为私有。这将需要一个替代的构造函数:最后,您可能希望考虑运算符重载 - 我相信您可以通过谷歌搜索找到合理的教程。
The line
cout << "The sum is " << result.add << endl;
is incorrect, as
add
is a method soresult.add
will be a pointer to that method, andcout
does not know how to handle it - which makes the compiler spit it out.Change the line to
You need to do the same for the line
As to coding style, the two methods are overwrting an existing complex number. Perhaps having a the function like this would be better
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
andi
private. This will require an alternative constructor:Finally you may wish to consider operator overloading - I am sure you can google that to find a reasonable tutorial.
在 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();
您已经在使用 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