计算机编程题
- 提问者网友:鼻尖触碰
- 2021-07-30 00:56
- 五星知识达人网友:鸠书
- 2021-07-30 02:24
#include <iostream>
using namespace std;
class complex{
public:
double sb;
double xb;
complex(){sb=0,xb=0;};
complex(double k,double x);
void display();
complex operator +(complex &p);
complex operator -(complex &y);
complex operator *(complex &p);
complex operator /(complex &p);
};
complex::complex(double k,double x):sb(k),xb(x){}
complex complex::operator +(complex &p)
{
complex c;
c.sb=sb+p.sb;
c.xb=xb+p.xb;
return c;
}
complex complex::operator -(complex &y)
{
complex c;
c.sb=sb-y.sb;
c.xb=xb-y.xb;
return c;
}
complex complex::operator*(complex &p)
{
complex c;
c.sb=sb*p.sb;
c.xb=xb*p.xb;
return c;
}
complex complex::operator /(complex &p)
{
complex c;
c.sb=sb / p.sb;
c.xb=xb / p.xb;
return c;
}
void complex::display()
{cout <<"(" <<sb<<","<<xb<<"i)";}
int main()
{
complex t1(7,8),t2(2,9),t3;
t3=t1+t2;
cout <<"t1+t2";
t3.display();
t3=t1-t2;
cout<<"t1-t2";
t3.display();
t3=t1*t2;
cout<<"t1*t2";
t3.display();
t3=t1/t2;
cout<<"t1/t2";
t3.display();
return 0;
}