巨型整数运算符重载定义部分

来源:互联网 发布:java后端做什么 编辑:程序博客网 时间:2024/05/01 11:10

巨型整型运算重载--定义部分(.h)

关键词HugeInt    运算符重载    实现的功能:实现巨型整数的基本运算(关系运算,算术运算,复合运算,++,--...)

//处理的主要思想:采用双向循环链表存储巨型数据 每结点存储四位数字

#include < iostream >
#include< string >
#include< math.h >
using namespace std;

typedef struct node//自定义类型名(链表结点)
{int data;
struct node *left,*right;
} node;
//*------------------使用extern申明下列函数可以在本模块或其它模块中使用--------------*//

class HugeInt;//巨型整数类申明

//---------------------(重载关系运算)---------------------------------------//
extern bool operator>(HugeInt &m,HugeInt &n);
extern bool operator>(HugeInt &m,const long int &n);
extern bool operator>(const long int &m,HugeInt &n);
extern bool operator>( char* m,HugeInt &n);
extern bool operator>(HugeInt& m, char* n);

extern bool operator>=(HugeInt &m,HugeInt &n);
extern bool operator>=(HugeInt &m,const long int &n);
extern bool operator>=(const long int &m,HugeInt &n);
extern bool operator>=( char* m,HugeInt &n);
extern bool operator>=(HugeInt& m, char* n);

extern bool operator<(HugeInt &m,HugeInt &n);
extern bool operator<(HugeInt &m,const long int &n);
extern bool operator<(const long int &m,HugeInt &n);
extern bool operator<( char* m,HugeInt &n);
extern bool operator<(HugeInt& m, char* n);

extern bool operator<=(HugeInt &m,HugeInt &n);
extern bool operator<=(HugeInt &m,const long int &n);
extern bool operator<=(const long int &m,HugeInt &n);
extern bool operator<=( char* m,HugeInt &n);
extern bool operator<=(HugeInt& m, char* n);

extern bool operator==(HugeInt &m,HugeInt &n);
extern bool operator==(HugeInt &m,const long int &n);
extern bool operator==(const long int &m,HugeInt &n);
extern bool operator==( char* m,HugeInt &n);
extern bool operator==(HugeInt& m, char* n);

extern bool operator!=(HugeInt &m,HugeInt &n);
extern bool operator!=(HugeInt &m,const long int &n);
extern bool operator!=(const long int &m,HugeInt &n);
extern bool operator!=( char* m,HugeInt &n);
extern bool operator!=(HugeInt& m, char* n);

//------------------------(重载加法+)---------------------------------------//
extern HugeInt operator +(HugeInt& addend,const long int& augend);
extern HugeInt operator +(const long int& addend,HugeInt& augend);
extern HugeInt operator +(HugeInt& addend,HugeInt& augend);
extern HugeInt operator +(HugeInt& addend,char* augend);
extern HugeInt operator +(char* addend,HugeInt& augend);

//------------------------(重载减法-)---------------------------------------//
extern HugeInt operator -(HugeInt& minuend,HugeInt& subtrahend);
extern HugeInt operator -(const long int& minuend,HugeInt& subtrahend);
extern HugeInt operator -(HugeInt& minuend,const long int& subtrahend);
//-----------------------(重载乘法*)----------------------------------------//
extern HugeInt operator *(const long int& faciend,HugeInt& multiplicator);
extern HugeInt operator *(HugeInt& faciend,const long int& multiplicator);
extern HugeInt operator *(HugeInt& faciend,HugeInt& multiplicator);
extern HugeInt operator *(HugeInt& faciend,char* multiplicator);
extern HugeInt operator *(char* faciend,HugeInt& multiplicator);
//-----------------------(重载除法/)----------------------------------------//
extern HugeInt operator /(HugeInt dividend,HugeInt divisor);
extern HugeInt operator /(const long int dividend,HugeInt divisor);
extern HugeInt operator /(HugeInt dividend,const long int divisor);
extern HugeInt operator /(HugeInt dividend,char* divisor);
extern HugeInt operator /(char* dividend, HugeInt divisor);
//------------------------(重载求余%)---------------------------------------//
extern HugeInt operator %(HugeInt dividend,HugeInt divisor);
extern HugeInt operator %(const long int dividend,HugeInt divisor);
extern HugeInt operator %(HugeInt dividend,const long int divisor);
//------------------------(重载求反)-------------------------------//
extern HugeInt operator -(HugeInt h);

//------------------------(重载求绝对值abs)---------------------------------//
extern HugeInt abs(HugeInt);
//------------------------(重载输出<<)--------------------------------------//
extern ostream &operator<<(ostream & out,HugeInt &m);

//*-------------------------HugeInt类及相关函数的定义------------------------*//
//使用双向循环链表存储巨型数据,每四位数字为一个结点 如1,2653,0006,5632//
class HugeInt
{


public:

HugeInt(char *data);//使用字符串初始化

HugeInt(const long);//使用长整型初始化

HugeInt(HugeInt&);//拷贝构造函数

HugeInt();//使用默认值0初始化

bool Empty() const{return n==0;}//判断双链表是否为空

void PrintList()const;//输出链表

void DelList();//删除链表

HugeInt& Insert(const short int x);//插入结点

bool IsRight(char *data);//判断数据串是否合法

char* abs(char*);//求数据串绝对值

node* GetEndNode(){return RightEnd;}//获得链表尾结点

int GetN(){return n;}//获得结点个数

node* GetHeadNode(){return head;}//获得头结点

HugeInt& SetHeadData(long int a){head->data=a;return *this;}//设置头结点值(巨型数符号)

bool IsZero(){return (GetHeadNode()->right->data==0&&n==1);}//判断巨型数据值是否为零

~HugeInt();//析构函数

//-----------------------以下是各运算符重载的定义---------------------------------//

//------------------------(重载求绝对值abs)----------------------------//
friend HugeInt abs(HugeInt);

//-------------------------(重载关系运算)------------------------------//
friend bool operator>(HugeInt &m,HugeInt &n);
friend bool operator>(HugeInt &m,const long int &n);
friend bool operator>(const long int &m,HugeInt &n);
friend bool operator>(HugeInt &m, char* n);
friend bool operator>( char* m,HugeInt &n);

friend bool operator>=(HugeInt &m,HugeInt &n);
friend bool operator>=(HugeInt &m,const long int &n);
friend bool operator>=(const long int &m,HugeInt &n);
friend bool operator>=(HugeInt &m,const char* &n);
friend bool operator>=(const char*m,HugeInt &n);

friend bool operator<(HugeInt &m,HugeInt &n);
friend bool operator<(HugeInt &m,const long int &n);
friend bool operator<(const long int &m,HugeInt &n);
friend bool operator<(HugeInt &m, char*n);
friend bool operator<( char*m,HugeInt &n);

friend bool operator<=(HugeInt &m,HugeInt &n);
friend bool operator<=(HugeInt &m,const long int &n);
friend bool operator<=(const long int &m,HugeInt &n);
friend bool operator<=(HugeInt &m,const char*n);
friend bool operator<=(const char*m,HugeInt &n);

friend bool operator==(HugeInt &m,HugeInt &n);
friend bool operator==(HugeInt &m,const long int &n);
friend bool operator==(const long int &m,HugeInt &n);
friend bool operator==(HugeInt &m,const char* &n);
friend bool operator==(const char*m,HugeInt &n);

friend bool operator!=(HugeInt &m,HugeInt &n);
friend bool operator!=(HugeInt &m,const long int &n);
friend bool operator!=(const long int &m,HugeInt &n);
friend bool operator!=(HugeInt &m, char*n);
friend bool operator!=(char*m,HugeInt &n);

//-------------------------(重载输出)-------------------------------//
friend ostream &operator<<(ostream & out,HugeInt &m);

//------------------------(重载乘法)-------------------------------//
friend HugeInt operator *(const long int& faciend,HugeInt& multiplicator);
friend HugeInt operator *(HugeInt& faciend,const long int& multiplicator);
friend HugeInt operator *(HugeInt& faciend,HugeInt& multiplicator);
friend HugeInt operator *(HugeInt& faciend,char* multiplicator);
friend HugeInt operator *(char* faciend,HugeInt& multiplicator);

//-----------------------(重载乘等于)------------------------------//
HugeInt& operator *=(const long int& faciend);
HugeInt& operator *=(HugeInt& faciend);
HugeInt& operator *=(char* faciend);

//------------------------(重载除法)-------------------------------//
friend HugeInt operator /(HugeInt dividend,HugeInt divisor);
friend HugeInt operator /(const long int dividend,HugeInt divisor);
friend HugeInt operator /(HugeInt dividend,const long int divisor);
friend HugeInt operator /(HugeInt dividend,char* divisor);
friend HugeInt operator /(char* dividend, HugeInt divisor);
//------------------------(重载除等于)-----------------------------//
HugeInt& operator /=(HugeInt& dividend);
HugeInt& operator /=(const long int& dividend);
HugeInt& operator /=(char* dividend);
//------------------------(重载求余)-------------------------------//
friend HugeInt operator %(HugeInt dividend,HugeInt divisor);
friend HugeInt operator %(const long int dividend,HugeInt divisor);
friend HugeInt operator %(HugeInt dividend,const long int divisor);
friend HugeInt operator %(HugeInt dividend,char* divisor);
friend HugeInt operator %(char* dividend, HugeInt divisor);
//------------------------(重载求余等于)---------------------------//
HugeInt& operator %=(HugeInt& div);
HugeInt& operator %=(const long int& div);
HugeInt& operator %=(char* div);
//------------------------(重载加法)-------------------------------//
friend HugeInt operator +(const long int& addend,HugeInt& augend);
friend HugeInt operator +(HugeInt& addend,const long int& augend);
friend HugeInt operator +(HugeInt& addend,HugeInt& augend);
friend HugeInt operator +(HugeInt& addend,char* augend);
friend HugeInt operator +(char* addend,HugeInt& augend);

//------------------------(重载加等于)-----------------------------//
HugeInt& operator +=(HugeInt& augend);
HugeInt& operator +=(const long int& augend);
HugeInt& operator +=(char* augend);
//------------------------(重载前++)-------------------------------//
HugeInt& operator ++();
//------------------------(重载后++)-------------------------------//
HugeInt operator ++(int);
//------------------------(重载前--)-------------------------------//
HugeInt& operator --();
//------------------------(重载后--)-------------------------------//
HugeInt operator --(int);

//------------------------(重载减法)-------------------------------//
friend HugeInt operator -(const long int& minuend,HugeInt& subtrahend);
friend HugeInt operator -(HugeInt& minuend,const long int& subtrahend);
friend HugeInt operator -(HugeInt& minuend,HugeInt& subtrahend);
friend HugeInt operator -(HugeInt& minuend,char* subtrahend);
friend HugeInt operator -(char* minuend,HugeInt& subtrahend);
//------------------------(重载减等于)-----------------------------//
HugeInt& operator -=(HugeInt& subtrahend);
HugeInt& operator -=(const long int& subtrahend);
HugeInt& operator -=(char* subtrahend);


//------------------------(重载赋值)-------------------------------//
HugeInt& operator =(const long int& k);
HugeInt& operator =(char* k);
HugeInt& operator =(HugeInt& h);
//------------------------(重载求反)-------------------------------//
friend HugeInt operator -(HugeInt h);

//私有数据成员
private:

int n;//表示结点个数

node *RightEnd,*head;//头结点与尾结点 头结点用于存储数据的符号 正为0负为1

};

原创粉丝点击