高精大數板子

2021-09-23 10:27:18 字數 3104 閱讀 7670

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

typedef long long ll;

/* * 完全大數模板

* 輸出cin>>a

* 輸出a.print();

* 注意這個輸入不能自動去掉前導0的,可以先讀入到char陣列,去掉前導0,再用建構函式。

* by kuangbin gg.

*/

#define maxn 9999

#define maxsize 1010

#define dlen 4

class bignum

//建構函式

bignum(const ll); //將乙個int型別的變數轉化成大數

bignum(const char*); //將乙個字串型別的變數轉化為大數

bignum(const bignum &); //拷貝建構函式

bignum &operator=(const bignum &); //過載賦值運算子,大數之間進行賦值運算

friend istream& operator>>(istream&,bignum&); //過載輸入運算子

friend ostream& operator<<(ostream&,bignum&); //過載輸出運算子

bignum operator+(const bignum &)const; //過載加法運算子,兩個大數之間的相加運算

bignum operator-(const bignum &)const; //過載減法運算子,兩個大數之間的相減運算

bignum operator*(const bignum &)const; //過載乘法運算子,兩個大數之間的相乘運算

bignum operator/(const int &)const; //過載除法運算子,大數對乙個整數進行相除運算

bignum operator^(const int &)const; //大數的n次方運算

int operator%(const int &)const; //大數對乙個int型別的變數進行取模運算

bool operator>(const bignum &t)const; //大數和另乙個大數的大小比較

bool operator>(const int &t)const; //大數和乙個int型別的變數的大小比較

void print(); //輸出大數

};

bignum::bignum(const ll b) //將乙個int型別的變數轉化為大數

a[len++]=d;

} bignum::bignum(const char *s) //將乙個字串型別的變數轉化為大數

} bignum::bignum(const bignum &t):len(t.len) //拷貝建構函式

b.a[count]=sum;

count++;

} b.len=count++;

return in;

} ostream& operator<<(ostream& out,bignum& b) //過載輸出運算子

return out;

} bignum bignum::operator+(const bignum &t)const //兩個大數之間的相加運算

} if(t.a[big]!=0)

t.len=big+1;

else t.len=big;

return t;

} bignum bignum::operator-(const bignum &t)const //兩個大數之間的相減運算

else

big=t1.len;

for(i=0;ii)

t1.a[j--]+=maxn;

t1.a[i]+=maxn+1-t2.a[i];

} else t1.a[i]-=t2.a[i];

} t1.len=big;

while(t1.a[len-1]==0 && t1.len>1)

if(flag)

t1.a[big-1]=0-t1.a[big-1];

return t1;

} bignum bignum::operator*(const bignum &t)const //兩個大數之間的相乘

else

} if(up!=0)

ret.a[i+j]=up;

} ret.len=i+j;

while(ret.a[ret.len-1]==0 && ret.len>1)ret.len--;

return ret;

} bignum bignum::operator/(const int &b)const //大數對乙個整數進行相除運算

ret.len=len;

while(ret.a[ret.len-1]==0 && ret.len>1)

ret.len--;

return ret;

} int bignum::operator%(const int &b)const //大數對乙個 int型別的變數進行取模

bignum bignum::operator^(const int &n)const //大數的n次方運算

return ret;

} bool bignum::operator>(const bignum &t)const //大數和另乙個大數的大小比較

else

return false;

} bool bignum::operator>(const int &t)const //大數和乙個int型別的變數的大小比較

void bignum::print() //輸出大數

ll scan()

bignum a, b, c, d,e;

int main()

return 0;

}

無符號整數壓位高精板子

using namespace std 繼承vector解決位數限制,操作方便 struct huge int vector huge int const string str 格式化,包括進製和去前導0,用的地方很多,先寫乙個 huge int format 位外進製 return this 為使...

高精乘 大數乘 詳解

高精乘就是兩個比較大的數字相乘,結果超過了基本型別的表示範圍,所以不能直接用普通的資料型別相乘,所以我們要用字串來表示,所以我們來用stl裡的string來表示字串.include using namespace std include define maxlen 100 string sum st...

c 高精除以高精

include using namespace std int a 1000 b 1000 int len1,len2,len int index int re 1000 p 0 記錄結果 intjudge return0 int main for i 0 isize i len1 n.size l...