C 高精度運算類bign 過載操作符

2021-09-07 05:50:25 字數 1376 閱讀 7233

大資料操作,有例如以下問題:

計算:456789135612326542132123+14875231656511323132

456789135612326542132123*14875231656511323132

比較:7531479535511335666686565>753147953551451213356666865 ?

long long型別儲存不了,儲存不了就實現

不成計算,怎麼辦???

為了解決以上問題,所以得定義一種結構型別以儲存這些資料,並過載運算子支援這些資料的操作。為了方便**的復用因此有了例如以下**:

#include#include#includeusing namespace std;

const int maxn = 200;

struct bign

bign(int num)

//定義為const引數,作用是 不能對const引數的值做改動

bign(const char* num)

/*以上是構造方法。初始化時對執行對應的方法*/

bign operator = (int num)

//函式定義後的constkeyword,它表明「x.str()不會改變x」

string str() const

void clean()

/* 下面是過載操作符 */

bign operator = (const char* num)

bign operator + (const bign& b) const

return c;

} bign operator * (const bign& b)

c.clean();

return c;

} bign operator - (const bign& b)

c.s[c.len++] = x;

}c.clean();

return c;

} bool operator < (const bign& b) const

bool operator > (const bign& b) const

bool operator <= (const bign& b)

bool operator == (const bign& b)

bign operator += (const bign& b)

};istream& operator >> (istream &in, bign& x)

ostream& operator << (ostream &out, const bign& x)

int main()

C 高精度運算

高精度加法vector int add vector int a,vector int b if t c.push back 1 return c 高精度減法 判斷a b 的大小,保證函式裡面是大的減小的 bool cmp vector int a,vector int b vector int a...

高精度過載運算子

高精度過載運算子模板 include include includeusing namespace std const int maxn 200 struct bign bign int num 定義為const引數,作用是 不能對const引數的值做修改 bign const char num 以...

高精度運算模版C語言

include include include include define maxx 100 using namespace std 高精度加法 void add char s1,char s2 b maxx 10 int l1 strlen s1 int l2 strlen s2 for i l...