题目要求如图:
我的代码如下:
“标头.h”
#pragma once
#include<iostream>
using namespace std;
class complex
{
public:
complex(double r, double i);
int operator==(complex c);
//friend complex operator+(complex& c1, complex& c2);
//friend complex operator-(complex& c1, complex& c2);
//friend complex operator*(complex& c1, complex& c2);
friend ostream& operator<<(ostream&, complex&);
friend istream& operator>>(istream&, complex&);
private:
double real;
double imag;
};
complex::complex(double r = 0.0, double i = 0.0)
{
real = r;
imag = i;
}
int complex::operator==(complex c)
{
if (real == c.real && imag == c.imag)
return 1;
else
return 0;
}
//complex operator+(complex& c1, complex& c2)
//{
// return
// complex(c1.real + c2.real, c1.imag + c2.imag);
//}
//complex operator-(complex& c1, complex& c2)
//{
// return
// complex(c1.real - c2.real, c1.imag - c2.imag);
//}
ostream& operator<<(ostream& output, complex& c)
{
output << "(" << c.real << "+" << c.imag << "i)";
return output;
}
istream& operator>>(istream& input, complex& c)
{
cout << "input real part and imaginary part of Complex number:";
input >> c.real >> c.imag;
return input;
}
“源.cpp”
#include"标头.h"
int main()
{
complex c1, c2;
cin >> c1 >> c2;
cout << "(1+1i)(2+2i)(2+4i)测试复数的+、-、*以及==运算:" << endl;
cout << c1 << "与" << c2 << "是否相等:";
if (c1 == c2)
cout << 1 << endl;
else
cout << 0 << endl;
}
我的运行结果如图:
题目要求中
“(2+4i)”没有输入显示,我就不太清楚该怎么把输入的(2+2i)与这个(2+4i)相加减乘了。
(也可能是我读题理解有误,感谢指正)
目前卡到这个(2+4i)上了。
非常感谢!