complex();
complex(float r,float i) { real= r; image= i; }
virtual ~complex();
friend complex operator + (complex a,complex b); friend complex operator - (complex a,complex b); friend complex operator * (complex a,complex b); friend complex operator / (complex a,complex b);
private:
float image; float real; };
/*****************************************************************************/
// rational.h: interface for the rational class. class rational {
public:
void print(); //输出函数
rational(int x=0,int y=0); virtual ~rational();
friend rational operator + (rational num1,rational num2); //重载运算符+
friend rational operator - (rational num1,rational num2); //重载运算符- friend rational operator * (rational num1,rational num2); //重载运算符*
friend rational operator / (rational num1,rational num2); //重载运算符/ friend bool operator ==(rational num1,rational num2); //重载运算符==
friend double real(rational x); //声明转换函数
private:
void optimization(); //优化有理数函数 int denominator; //分母 int numerator; //分子 };
/*****************************************************************************/
// matrix.h: interface for the matrix class.
class matrix {
public:
void Disp(); //显示矩阵所有元素
int matrix::operator ()(short row, short col); //重载运算符成员函数() void SetElem(short row,short col,int val); //将元素(row,col)设置为val matrix();
matrix(short r,short c) { rows= r; cols= c; elems= new int[rows*cols]; }
virtual ~matrix();
friend matrix operator +(matrix p,matrix q); //重载运算符+ friend matrix operator -(matrix p,matrix q); //重载运算符- friend matrix operator *(matrix p,matrix q); //重载运算符× private:
int * elems; //存放矩阵的所有元素 short cols; //矩阵的列 short rows; //矩阵的行 };
/*****************************************************************************/
// set.h: interface for the set class. #include
//enum bool {false,true};
enum errcode {noerr,overflow}; //定义集合类 class set {
public:
void print(); //显示输出集合元素 set(){card=0;} virtual ~set();
errcode additem(int); //增加集合元素
friend bool operator &(int,set); //声明重载运算符&,判断某一整数是否属于某一集合
friend bool operator ==(set,set); //声明重载运算符==,判断两个集合是否相等
friend bool operator !=(set,set); //声明重载运算符!=,判断两个集合是否不等
交
并
friend set operator *(set,set); friend set operator +(set,set);
//声明重载运算符*,求两个集合的//声明重载运算符+,求两个集合的
friend bool operator <(set,set); //声明重载运算符<,判断某一集合是否为另一集合的纯子集
friend bool operator <=(set,set); //声明重载运算符<=,判断某一集合是否为另一集合的子集
private:
int elems[16]; int card; };
/*****************************************************************************/
// complex.cpp: implementation of the complex class. #include
complex::~complex() { }
/*****************************************************************************/
/*
函数名称:print 功能描述:显示复数 修改记录: */
void complex::print() {
cout< /*****************************************************************************/ /* 函数名称:operator + 功能描述:显示复数 修改记录: */ complex operator + (complex a,complex b) { complex temp; temp.real= a.real +b.real ; temp.image= a.image +b.image ; return temp; } /*****************************************************************************/ /* 函数名称:operator - 功能描述:显示复数 修改记录: */ complex operator - (complex a,complex b) { complex temp; temp.real= a.real -b.real ; temp.image= a.image -b.image ; return temp; } /*****************************************************************************/ /* 函数名称:operator * 功能描述:显示复数 修改记录: */ complex operator * (complex a,complex b) { complex temp; temp.real= a.real *b.real -a.image *b.image ; temp.image =a.real *b.image +a.image *b.real ; return temp; } /*****************************************************************************/ /* 函数名称:operator / 功能描述:显示复数 修改记录: */ complex operator / (complex a,complex b) { complex temp; float tt; tt=1/(b.real *b.real +b.image *b.image ); temp.real =(a.real *b.real +a.image *b.image )*tt; temp.image =(b.real *a.image -a.real *b.image )*tt; return temp; } /*****************************************************************************/ // rational.cpp: implementation of the rational class. #include rational::rational(int x,int y) { numerator= x; denominator= y; optimization(); //有理数优化 } rational::~rational() { } /*****************************************************************************/ /* 函数名称:optimization 功能描述:定义有理数优化函数 修改记录: */ void rational::optimization() { int gcd; if(numerator==0) //如果分子为零,则分母为1后返回 { denominator= 1; return; } //取分子分母中较小的数作为公约数极限 gcd= (abs(numerator)>abs(denominator))?abs(numerator):abs(denominator);