在C++中使用类进行复数的加法和减法运算
Addition and subtraction of complex numbers using class in 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_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;
}
这将使您能够将加法链接在一起,还可以将一个复数添加到现有的复数中。
此外,您可以将类变量r
和i
设为私有变量。这将需要一个替代构造函数:
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();
}
- 使用C++中的模板和运算符重载执行矩阵运算
- GCC本机矩阵运算库
- 位阵列上的快速AND运算
- 字符串中int的加法运算
- 算术运算的结果类似于:C浮点变量中的1/3
- 如何使用OpenMP并行化此矩阵时间矢量运算
- 如何在两个 boost::multi_arrays (C++) 之间执行数学运算?
- 如何在 vtk Poly 数据上进行布尔运算?
- MSVC 无法根据模板参数进行数学运算,这是一个错误吗?
- 避免指针运算,修复叮当整齐错误
- 使用双精度的浮点运算
- 如何在 c++ 中使用带有数学运算的引用/指针?
- 更快的C++算术运算
- 是否存在用于 C 或 C++ 中常见数学运算(例如最小值、最大值和平均值)的可导入库?
- 为什么循环体中的一个基本算术运算执行得比两个算术运算慢
- 两个字符串之间的数学运算
- 为什么对无符号字符进行算术运算会将它们提升为有符号整数
- 何时使用按位运算而不是算术替代方法?
- Arduino 上数学运算的计时速度 - 异常
- 如何使用按位运算将随机uint64_t转换为范围 (0, 1) 的随机双精度