复数的算术运算

时间:2019-10-22 09:49:44

标签: c++ class oop c++11 object

Here is the error I am Facing In Results我正在尝试制作一个程序,该程序从用户处获取两个复数,然后在输出中显示我在下面提供的mY代码的这些数字的加法,减法和乘法,但未显示正确的输出I尝试了很多,但是没有用 一点帮助将不胜感激

  #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;
}

2 个答案:

答案 0 :(得分:0)

public:
{
real = real;
image = image;
}

我假设这是您在main中调用的set函数

public:
void set(int real, int image){

real = real;
image = image;

}

此处,这不是将成员变量realimage设置为fucntion set的参数值。这只是将传递的变量分配回自己。编译器将首先寻找一个称为“ real”的局部变量或构造函数参数,只有在找不到该变量时,它才会开始寻找一个名为“ real”的类成员。结果是上面的语句“ real = real”会将存储在参数“ real”中的值分配给参数“ real”,从而使其成为无用的语句。

public:
void set(int real, int image){
this->real = real;
this->image = image;
}

这将获得所需的输出。

答案 1 :(得分:0)

此代码有帮助。:)

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; }

如有任何疑问,请通知我。:)