巨型整數運算符重載定義部分

巨型整型運算重載--定義部分(.h)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

關鍵詞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

};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章