【C到C++】关于 Complex (复数类)
2012.8.15
complex库中的模板complex<>提供了一个复数(y=real+imagei)类型,它与其他数值类型兼容。
template <class SCALAR>
class complex{
// ……methods
private:
SCALAR x, y;
};
这使用户能够根据需要决定基本类型的精度。一般来说,这些基本类型是float、double或者long double。下面是用于测试这个类型的简单代码。
理解:SCALAR :由用户指定精度是float、double或者long double。
http://zhidao.baidu.com/question/149261754.html
C++ 编写复数类Complex
问:
编写复数类Complex。要求:
1) 类里面需要重载运算符函数,包括:复数加法+(二元),复数减法-(二元),复数求负-(一元)和复数赋值=(二元)。
2) 类里面有输出该复数的函数print。
3) 类的声明写在Complex.h中,类的实现写在Complex.cpp中。
4) 写出以上两个文件即可,无需客户代码。
5) 在思考以上问题时,可参考如下客户代码(假设类已编写好):
#include "complex.h"
#include <iostream.h>
int main()
{
COMPLEX c1(1, 2); // 定义一个值为1 + 2i的复数c1
COMPLEX c2(2); // 定义一个值为2的复数c2
COMPLEX c3(c1); // 用拷贝构造函数创建一个值同c1的新复数
c3.print(); // 打印c3的值
c1 = c1 + c2 + c3; // 将c1加上c2再加上c3赋值给c1
c2 = -c3; // c2等于c3求负
c3 = c2 - c1; // c3等于c2减去c1
c3.print(); // 再打印运算后c3的值
return 0;
}
回答:
#include "iostream.h"
class complex
{
public:
complex()
{
real=imag=0.0;
}
complex(double r)
{
real=r;
imag=0.0;
}
complex(double r,double i)
{
real=r;
imag=i;
}
complex(complex&cp)
{
real=cp.real;
imag=cp.imag;
}
friend complex operator+(const complex&c1,const complex&c2);
friend complex operator-(const complex&c1,const complex&c2);
inline complex operator+=(const complex&c);
inline complex operator-=(const complex&c);
void print();
private:
double real,imag;
};
complex operator+(const complex&c1,const complex&c2)
{
return complex(c1.real+c2.real,c1.imag+c2.imag);
}
complex operator-(const complex&c1,const complex&c2)
{
return complex(c1.real-c2.real,c1.imag-c2.imag);
}
inline complex complex::operator+=(const complex&c)
{
return(real+c.real,imag+c.imag);
}
inline complex complex::operator-=(const complex&c)
{
return(real-c.real,imag-c.imag);
}
void complex::print()
{
if (imag<0)
{
cout<<real<<imag<<"i"<<endl;
}
else
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
}
int main()
{
complex c1(1, 2); // 定义一个值为1 + 2i的复数c1
complex c2(2); // 定义一个值为2的复数c2
complex c3(c1); // 用拷贝构造函数创建一个值同c1的新复数
c3.print(); // 打印c3的值
c1=c1 + c2 + c3; // 将c1加上c2再加上c3赋值给c1
c2-=c3; // c2等于c3求负
c3 = c2 - c1; // c3等于c2减去c1
c3.print(); // 再打印运算后c3的值
return 0;
}
http://zhidao.baidu.com/question/256562692.html
这个程序是在上个程序的前提下,即在原来复数相加的前提下,又增加了复数能与整数运算的功能。
#include<iostream>
using namespace std;
class complex
{
public:
complex(){real=0;imag=0;}
complex(double r,double i){real=r;imag=i;}
complex operator+(complex &c2);
complex operator-(complex &c2);
complex operator*(complex &c2);
complex operator/(complex &c2);
complex operator+(int &i);
complex operator-(int &i);
complex operator*(int &i);
complex operator/(int &i);
void display();
private:
double real;
double imag;
};
complex complex::operator+(complex &c2)//不明白为什么这里的return不能像后面复数与整数相加时的return.
{
complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
complex complex::operator-(complex &c2)
{
complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
complex complex::operator *(complex &c2)
{
complex c;
c.real=real*c2.real-imag*c2.imag;
c.imag=imag*c2.real+real*c2.imag;
return c;
}
complex complex::operator / (complex &c2)
{
complex c;
c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
return c;
}
complex complex::operator +(int &i)
{
return complex(real+i,imag);
}
complex complex::operator -(int &i)
{
return complex(real-i,imag);
}
complex complex::operator *(int &i)
{
return complex(real*i,imag*i);
}
complex complex::operator /(int &i)
{
return complex(real/i,imag/i);
}
void complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
complex c1(1,2),c2(3,4),c3;
int i=5;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
c3=c1+c2;
cout<<"c1+c2=";
c3.display();
c3=c1-c2;
cout<<"c1-c2=";
c3.display();
c3=c1*c2;
cout<<"c1*c2=";
c3.display();
c3=c1/c2;
cout<<"c1/c2=";
c3.display();
cout<<"i="<<i<<endl;
c3=c1+i;
cout<<"c1+i=";
c3.display();
c3=c1-i;
cout<<"c1-i=";
c3.display();
c3=c1*i;
cout<<"c1*i=";
c3.display();
c3=c1/i;
cout<<"c1/i=";
c3.display();
return 0;
}