BigInteger 大整數類(C )

2021-10-09 01:45:05 字數 4012 閱讀 8479

有時候需要用到大整數,這裡放乙個自己寫的大整數結構體(biginteger),實現的功能如下:

其中乘法運算和除法運算都是 o(n

2)

o(n^2)

o(n2

) 的複雜度。

struct biginteger

biginteger

(const biginteger &y)

biginteger

(const biginteger *y)

biginteger

(const std::string &y)

biginteger

(long

long y=0)

void

squeeze()

biginteger abs()

const

// 賦值

biginteger &

operator=(

const biginteger &y)

biginteger &

operator=(

const std::string &y)

biginteger &

operator=(

long

long y)

squeeze()

;return

*this;}

// 取負號

biginteger operator-(

)const

// 四則運算

biginteger operator+(

const biginteger &y)

const

if(sign==1)

biginteger ret;

int i=

0,j=0;

while

(i<

size

(x)&& j<

size

(y.x)

) ret.x.

push_back

(x[i++

]+y.x[j++])

;while

(i<

size

(x)) ret.x.

push_back

(x[i++])

;while

(j<

size

(y.x)

) ret.x.

push_back

(y.x[j++])

; ret.x.

push_back(0

);for(i=

0;i+

1<

size

(ret.x)

;i++

) ret.x[i+1]

+=ret.x[i]/10

,ret.x[i]%=

10;ret.

squeeze()

;return ret;

} biginteger operator-(

const biginteger &y)

const

if(sign==1)

if(*this

biginteger ret;

int i=

0,j=0;

while

(j<

size

(y.x)

) ret.x.

push_back

(x[i++

]-y.x[j++])

;while

(i<

size

(x)) ret.x.

push_back

(x[i++])

;for

(int i=

0;i+

1<

size

(ret.x)

;i++)if

(ret.x[i]

<

0) ret.x[i]+=

10,ret.x[i+1]

--;ret.

squeeze()

;return ret;

} biginteger operator*(

const biginteger &y)

const

biginteger operator/(

const biginteger &y)

const

st.push

(s);

} tmp.x.

clear()

;while

(!st.

empty()

) tmp.x.

push_back

(st.

top())

,st.

pop();

delete

&st;

delete

&t1;

delete

&t2;

tmp.sign=sign^y.sign;

tmp.

squeeze()

;return tmp;

} biginteger operator%(

const biginteger &y)

const

biginteger operator+=

(const biginteger &y)

biginteger operator-=

(const biginteger &y)

biginteger operator*=

(const biginteger &y)

biginteger operator/=

(const biginteger &y)

biginteger operator%=

(const biginteger &y)

biginteger operator++(

) biginteger operator--(

) biginteger operator^(

const biginteger &y)

const

return ret;

}// 比較

bool

operator

<

(const biginteger &y)

constif(

size

(x)<

size

(y.x)

)return1;

elseif(

size

(x)>

size

(y.x)

)return0;

for(

int i=

size

(x)-

1;i>=

0;i--

)return0;

}bool

operator

>

(const biginteger &y)

const

bool

operator

<=

(const biginteger &y)

const

bool

operator

>=

(const biginteger &y)

const

bool

operator==(

const biginteger &y)

const

bool

operator!=(

const biginteger &y)

const

// 輸入輸出

friend std::ostream &

operator

<<

(std::ostream &out,

const biginteger &y)

friend std::istream &

operator

>>

(std::istream &in,biginteger &y)

};

大整數類運算 BigInteger

當數字超出型別上限就會出現溢位的情形。如果運算結果真的很大,那就需要運用到所謂的高精度運算,即用陣列來儲存整數,然後模擬手算的形式來實現。但還應考慮乙個易用性的問題 如果能像使用int一樣方便地使用大整數那該多好!那就是使用struct!結構體biginteger可用於儲存高精度非負整數 1 2 3...

C 大整數類

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

大整數類c 實現

在日常使用c 的過程中,經常會遇到數字太大越界的情況,對於這樣的大整數運算,我們可以用模擬比算的方法來實現,但是這樣每次運算都要實現這樣的演算法會帶來一定的不方便,我們希望能像int這樣的內建型別一樣使用大整數,所以我們實現乙個大整數struct 感謝劉汝佳老師的演算法競賽入門經典一書 struct...