C++复数编程

2024-11-09 02:11:44
推荐回答(3个)
回答(1):

#include
using namespace std;
class complex //复数类声明
{
public: //外部接口
complex(double r=0.0,double i=0.0){real=r;imag=i;} //构造函数
complex operator + (complex c2); //+重载为成员函数
complex operator = (complex c2); //=重载为成员函数
void display(); //输出复数
private: //私有数据成员
double real; //复数实部
double imag; //复数虚部
};
complex complex::operator +(complex c2) //重载函数实现
{
complex c;
c.real=c2.real+real;
c.imag=c2.imag+imag;
return complex(c.real,c.imag);
}
complex complex::operator =(complex c2) //重载函数实现
{
this->real=c2.real;
this->imag=c2.imag;
return *this;
}
void complex::display()
{ cout<<"("<
void main() //主函数
{
complex c1(5,4),c2(2,10),c3; //声明复数类的对象
cout<<"c1="; c1.display();
cout<<"c2="; c2.display();
c3=c1+c2; //使用重载运算符完成复数加法
cout<<"c3=c1+c2=";
c3.display();
c3=c2; //使用重载运算符完成复数赋值
cout<<"c3=c2=";
c3.display();
}

回答(2):

#include
using namespace std;

class CComplex {
private:
double m_real;
double m_imag;
public:

CComplex() {
m_real = 0;
m_imag = 0;
}

CComplex(double real) {
m_real = real;
m_imag = 0;
}

CComplex(double real, double imag) {
m_real = real;
m_imag = imag;
}

void display();
const CComplex operator+(CComplex &);
const CComplex operator-(CComplex &);
const CComplex operator*(CComplex &);
const CComplex operator/(CComplex &);
};

void CComplex::display() {
cout << "(" << m_real << ",";
cout << m_imag << "i)" << endl;
}

const CComplex CComplex::operator+(CComplex &rightNumber) {
CComplex resultNumber;
resultNumber.m_real = m_real + rightNumber.m_real;
resultNumber.m_imag = m_imag + rightNumber.m_imag;
return resultNumber;
}

const CComplex CComplex::operator-(CComplex &rightNumber) {
CComplex resultNumber;
resultNumber.m_real = m_real - rightNumber.m_real;
resultNumber.m_imag = m_imag - rightNumber.m_imag;
return resultNumber;
}

const CComplex CComplex::operator*(CComplex &rightNumber) {
CComplex resultNumber;
resultNumber.m_real = (m_real * rightNumber.m_real)-(m_imag * rightNumber.m_imag);
resultNumber.m_imag = (m_real * rightNumber.m_imag)+(m_imag * rightNumber.m_real);
return resultNumber;
}

const CComplex CComplex::operator/(CComplex &rightNumber) {
CComplex resultNumber;
resultNumber.m_real = ((m_real * rightNumber.m_real)+(m_imag * rightNumber.m_imag)) / ((rightNumber.m_real * rightNumber.m_real)+(rightNumber.m_imag * rightNumber.m_imag));
resultNumber.m_imag = ((m_real * rightNumber.m_imag)+(m_imag * rightNumber.m_real)) / ((rightNumber.m_real * rightNumber.m_real)+(rightNumber.m_imag * rightNumber.m_imag));
return resultNumber;
}

回答(3):

#include

using namespace std;

class complex
{
public:
complex(double r = 0.0, double i = 0.0)
{
real = r;
imag = i;
}
complex operator +(complex c1);
complex operator -(complex c2);
complex operator *(complex c3);
complex operator /(complex c4);
void display();

private:
double real;
double imag;
};

void complex::display()
{
if (imag > 0)
cout << real << " + " << imag << "i" << endl;
else if (imag == 0)
cout << real;
else
cout << real << " + " << "(" << imag << ")" << "i" <}

complex complex::operator +(complex c1)
{
complex v(real + c1.real, imag + c1.imag);

return v;
}

complex complex::operator -(complex c2)
{
return complex(real - c2.real, imag - c2.imag);
}

complex complex::operator *(complex c3)
{
return complex(real * c3.real - imag * c3.imag, real * c3.imag + imag * c3.real);
}

complex complex::operator /(complex c4)
{
double temp = c4.real * c4.real + c4.imag * c4.imag;
double tempr = (real * c4.real + imag * c4.imag) / temp;
double tempi = (imag * c4.real - real * c4.imag) / temp;

return complex(tempr, tempi);
}

int main(int argc, char* argv[])
{
complex c1(5, 4), c2(6, 7);

c1.display();
c2.display();

complex c3 = c1 + c2, c4 = c1 - c2;
complex c5 = c1 * c2, c6 = c1 / c2;

c3.display();
c4.display();
c5.display();
c6.display();

return 0;
}