复数的算术运算
Arithmetic operations on Complex Numbers
本文关键字:算术运算 更新时间:2023-10-16
这是我在结果中面临的错误我正在尝试制作一个程序 从用户那里获取两个复数,然后在输出中显示这些数字的加法、减法和乘法 我在下面提供了 mY 代码,但它没有显示正确的输出 我已经尝试了很多 但它不起作用 一点帮助将不胜感激
#include<iostream>
using namespace std;
class complexnumber
{
int real, image;
public:
{
real = real;
image = image;
}
complexnumber operator +(complexnumber c2)
{
complexnumber temp;
temp.real = real + c2.real;
temp.image = image + c2.image;
return temp;
}
complexnumber operator-(complexnumber c2)
{
complexnumber temp;
temp.real = real - c2.real;
temp.image = image - c2.image;
return temp;
}
complexnumber operator*(complexnumber c2)
{
complexnumber temp;
temp.real = real * c2.real + (image *c2.image)*(-1);
temp.image = image * c2.real+c2.image*real;
return temp;
}
void display(complexnumber c)
{
if (c.image < 0)
{
cout << real << image << "i";
}
else
{
cout << real << "+" << image << "i";
}
}
};
int main()
{
int real1, real2, image1, image2;
cout << "enter real value 1:";
cin >> real1;
cout << endl;
cout << "enter real value 2:";
cin >> real2;
cout << endl;
cout << "enter imaginary value 1:";
cin >> image1;
cout << endl;
cout << "enter imaginary value 2:";
cin >> image2;
cout << endl;
complexnumber c1, c2, c3, c4, c5;
c1.set(real1, image1);
c2.set(real2, image2);
c3 = c1 + c2;
c4 = c1 - c2;
c5=c1*c2;
cout << "addition of two complex number ";
c3.display(c3);
cout << endl;
cout << "subtraction of two complex number ";
c4.display(c4);
cout << endl;
cout << "multiplication of two complex number ";
c5.display(c5);
cout << endl;
return 0;
}
public:
{
real = real;
image = image;
}
我假设这是您在 main 中调用的set
函数
public:
void set(int real, int image){
real = real;
image = image;
}
这里不是将成员变量设置为real
,image
功能set
参数值。这只是将传递的变量赋值回自身。编译器将首先查找名为"real"的局部变量或构造函数参数,只有当它找不到时,它才会开始寻找名为"real"的类成员。结果是上面的语句 "real=real" 会将存储在参数 "real" 中的值分配给参数 "real",使其成为无用的语句。
public:
void set(int real, int image){
this->real = real;
this->image = image;
}
这将获得所需的输出。
这段代码很有帮助:)
int real, image; public: void set(int real, int image) { this->real = real; this->image = image; } complexnumber operator +(complexnumber c2) { complexnumber temp; temp.real = real + c2.real; temp.image = image + c2.image; return temp; } complexnumber operator-(complexnumber c2) { complexnumber temp; temp.real = real - c2.real; temp.image = image - c2.image; return temp; } complexnumber operator*(complexnumber c2) { complexnumber temp; temp.real = real * c2.real + (image *c2.image)*(-1); temp.image = image * c2.real+c2.image*real; return temp; } void display() { if (this->image < 0) { cout << this->real << this->image << "i"; } else { cout << this->real << "+" << this->image << "i"; } } }; int main() { int real1, real2, image1, image2; cout << "enter real value 1:"; cin >> real1; cout << endl; cout << "enter real value 2:"; cin >> real2; cout << endl; cout << "enter imaginary value 1:"; cin >> image1; cout << endl; cout << "enter imaginary value 2:"; cin >> image2; cout << endl; complexnumber c1, c2, c3, c4, c5; c1.set(real1, image1); c2.set(real2, image2); c3 = c1 + c2;//(1+3i)+(2+4i) c4 = c1 - c2;//(1+3i)-(2+4i) c5= c1 * c2; //(1+3i)(2+4i) = (1)(2)+(1)(4i)+(3i)(2)+(3i)(4i) // = (2) +(4i)+(6i)(12i^2) // = 2+10i+12i^2 //(i^2 == -1) = 2 + 10i-12 = -10+10i cout << "addition of two complex number "; c3.display();//Dont need to pass object. Its already has c3 object cout << endl; cout << "subtraction of two complex number "; c4.display(); cout << endl; cout << "multiplication of two complex number "; c5.display(); cout << endl; return 0; }
如有任何澄清,请告诉我:)
相关文章:
- 算术运算的结果类似于:C浮点变量中的1/3
- 更快的C++算术运算
- 为什么循环体中的一个基本算术运算执行得比两个算术运算慢
- 为什么对无符号字符进行算术运算会将它们提升为有符号整数
- 如何在 C++ 中计算字符串的算术运算
- 复数的算术运算
- 具有非常非常大数字的算术运算
- 如何使用C++中的处理器指令来实现快速算术运算
- boost::spirit::x3 phrase_parse 在进入 Vector 之前进行算术运算
- C++中混合数据类型的算术运算
- C++:编译器是否优化整数 + 浮点算术运算?
- 如何在特征中对二维数组的列执行简单的算术运算
- 使用数字列表和算术运算获取目标数字
- C++:“enable_if”用于限制支持特定算术运算的类型
- 如何正确避免 SIGFPE 和算术运算溢出
- 编译器用于编译 128 位整数的基本算术运算的技巧
- 将临时值存储为某种数据类型时,算术运算的标准规则是什么
- 相同的算术运算在 C++ 和 Python 中给出不同的结果
- 带有空的算术运算
- 算术运算和编译器优化