大整數類 模板

2021-06-27 02:42:46 字數 4645 閱讀 7138

//bigint v2.0

//by kunsoft

#include #include #include #include #include #include #include #include #include using namespace std;

typedef long long llt;

class bigint

bigint(const llt);

bigint(const char*);

bigint(const string);

bigint(const bigint & x);

bigint & operator = (const bigint &);

friend istream & operator >> (istream &, bigint &);

friend ostream & operator << (ostream &, bigint);

bigint operator + (const bigint &) const;

bigint operator - (const bigint &) const;

bigint operator * (const bigint &) const;

bigint operator / (const llt &) const;

llt operator % (const llt &) const;

bool operator > (const bigint &) const;

bool operator == (const bigint &) const;

bool operator >= (const bigint &) const;

friend bigint abs(const bigint &);

bigint operator - () const;

private:

dequenum;

bool negative;

};bigint::bigint(const llt x)

}bigint::bigint(const char* str)

bigint::bigint(const string str)

bigint::bigint(const bigint &x)

bigint & bigint::operator = (const bigint &x)

istream & operator >> (istream &is, bigint & x)

ostream & operator << (ostream &os, bigint x)

bool bigint::operator > (const bigint & rhs) const

return false;

}bool bigint::operator == (const bigint & rhs) const

bool bigint::operator >= (const bigint & rhs) const

bigint abs(const bigint & rhs)

bigint bigint::operator - () const

bigint bigint::operator + (const bigint & y) const

if (temp != 0) res.num.push_front(temp);

return res;

}bigint bigint::operator * (const bigint & y) const

while (res.size() >= 2 && res.back() == 0)

res.pop_back();

reverse(res.begin(), res.end());

bigint ret; ret.negative = this->negative ^ y.negative; ret.num = res;

return ret;

}bigint bigint::operator - (const bigint & y) const

for (unsigned i = 0; i < res.size() - 1; ++i) if (res[i] < 0)

while (res.size() >= 2 && res.back() == 0)

res.pop_back();

reverse(res.begin(), res.end()); ret.num = res;

return ret;

}bigint bigint::operator / (const llt & y) const

while (res.num.size() >= 2 && res.num.front() == 0)

res.num.pop_front();

return res;

}llt bigint::operator % (const llt & y) const

int main()

下面是老版的v1.0版本提供參考,使用陣列實現,速度會快一些。

#include #include #include #include #include #include #include #define maxn 1024

using namespace std;

class bigint

; bigint(const int);

bigint(const char*);

bigint(const string);

bigint(const bigint &x);

bigint &operator = (const bigint &);

friend istream &operator >> (istream &, bigint &);

friend ostream &operator << (ostream &, bigint &);

bigint operator + (const bigint &);

bigint operator - (const bigint &);

bigint operator * (const bigint &);

bigint operator / (const long long int &);

long long int operator % (const long long int &);

bool operator > (const bigint &);

friend bigint rev(const bigint &x);

};//將乙個int型別的變數轉化為bigint型別

bigint::bigint(const int x)

else

t = x;

while (t > 0)

}//將乙個c風格字串的變數轉化為bigint型別

bigint::bigint(const char* str)

}//將乙個string字串的變數轉化為bigint型別

bigint::bigint(const string str)

//拷貝bigint物件

bigint::bigint(const bigint &x)

//過載賦值運算子

bigint & bigint::operator = (const bigint &x)

//過載輸入運算子

istream &operator >> (istream &is, bigint &x)

//過載輸出運算子

ostream &operator << (ostream &os, bigint &x)

//獲取翻轉bigint後的bigint

bigint rev(const bigint &x)

return false;

}}//兩個bigint之間的加法運算

bigint bigint::operator + (const bigint &y)

if (temp != 0)

res.num[cnt++] = temp;

res.len = cnt;

for (int i = 0; i < (res.len / 2); i++)

return res;

}//兩個bigint之間的乘法運算

bigint bigint::operator * (const bigint &y)

}if(res.len==0)

ret = rev(res);

return ret;

}//兩個bigint之間的減法運算

bigint bigint::operator - (const bigint &y)

for(int i=0, j=0; i=0; --i)

}if(res.len==0)

ret = rev(res);

return ret;

}//bigint除以乙個長整形運算

bigint bigint::operator / (const long long int &y)

{ long long int temp = 0;

bigint x = (*this);

bigint res;

for(int i=0; i

大整數類 模板

bigint v2.0 by kunsoft include include include include include include include include include using namespace std typedef long long llt class bigint ...

大整數類模板

為了方便寫,加了個符號變數neg.過載了各種比較符號,支援加減乘除取模以及模快速冪.快速冪不知道怎麼用位運算,效率會偏低.1 include2 include3 include 4 include5 include6 using namespace std 7const int base 10000...

C 大整數類

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!include include include include using namespace std define maxn 9999 define maxsize 10 define dlen 4 class bignum 建構函式 bignu...