C 高精度整型數四則運算

2022-07-29 04:48:10 字數 4823 閱讀 5256

高精度運算的實現思路,就是模擬人們在進行運算時的方法步驟。

高精度的數數字比較多,用 int 陣列實現的話會浪費很多空間,這裡我們用 string 去實現。考慮到我們在手動做加減乘法的時候,都是從低位向高位做,只有除法是高位向低位做,為方便運算的實現,這裡我們倒著去存乙個數,例如,我們用 "65248" 來代表整型數 84256。

下面說一下大致思路,

加法:

記錄乙個進製值carry當前位的和temp,從低位向高位,temp = 兩數當前位相加 + carry,結果的當前位 = temp % 10,carry = temp  / 10,如果此時當前位已經是較長的數的最高位且有進製,則結果上要進製,否則繼續迴圈。

減法:

首先要保證被減數大於等於減數,記錄乙個借位值borrow,從低位向高位,如果被減數當前位 >= 減數當前位 + borrow,那就直接減去,借位置0,否則被減數當前位 + 10再去減減數當前位和借位,並將借位置1。由於減法的結果位數小於等於被減數字數,所以最後要消除一下結果的前導零。

乘法:

模擬乘法需要二重迴圈,記錄進製值 carry, 記錄temp = 乘數1第 i 位 * 乘數2第 j 位 + carry,carry = (結果第 i + j 位 + temp) / 10,結果第 i + j 位 = (當前位 + temp) % 10。

乘法運算的結果位數可能是兩數字數相加,也可能是兩數字數相加再 + 1,要向加法一樣考慮最終進製的情況。

除法:

手動模擬一下除法我們可以發現,除法是乙個對齊->試商->減去當前積->再對齊->再試商....這樣的過程,對齊,我們可以通過在除數低位補 0 來實現,試商,我們則通過一次次的減去除數,直到被除數小於除數來實現。

舉個栗子:654178 / 25 = 26167  (整數除法捨去小數)

除法的核心步驟 —— 不停地減,是比較耗費時間的,我們可以去二分找到應該減的次數,一次減去那麼多,可以提高一些效率。

以上是核心的思路,具體實現時還需要注意一些邊邊角角的細節,可以參考**。

為了方便使用,乾脆寫乙個類,並且過載常用的運算子。

附上**:

/*

** @brief 整型數高精度

* 要求無負數, 無前導零

*/class

highprecision

if(carry)ans.num += char(carry + '0'

);

return

ans;

}public

: highprecision()

highprecision(

const highprecision&a)

highprecision(

const

string&a)

}highprecision(

const

int&a)

while

(temp1);

}bool

operator== (const highprecision& a) const

bool

operator== (const

int& a) const

bool

operator!= (const highprecision& a) const

bool

operator> (const highprecision& a) const

return

false

; }

bool

operator> (const

int& a) const

bool

operator

< (const highprecision& a) const

bool

operator>= (const highprecision& a) const

bool

operator

<= (const highprecision& a) const

highprecision

operator= (const highprecision&a)/**

* @brief 高精度加法

* @param a 加數

* @return 和

*/highprecision

operator+ (const highprecision& a) const

else

int carry = 0, temp1, max_len =max(temp_short.length(), temp_long.length());

for(int i = 0; i < temp_short.length(); i++)

temp1 = int(temp_long[i] - '

0') + int(temp_short[i] - '

0') +carry;

temp_short[i] = char(temp1 % 10 + '0'

); carry = temp1 / 10

;

if(i == temp_short.length() - 1 && (carry || i < max_len - 1))temp_short += '

0';//

最高位需要進製的情況

} highprecision ans;

ans.num =temp_short;

return

ans;

}highprecision

operator+ (const

int& a) const

highprecision

operator+= (const highprecision&a)/**

* @brief 高精度減法 - 必須保證被減數大於等於減數

* @param a 減數

* @return 差

*/highprecision

operator- (const highprecision &a) const

else

}while(ans.num[ans.num.length() - 1] == '

0' && ans.num.length() > 1)ans.num.erase(ans.num.length() - 1

);

return

ans;

}highprecision

operator- (const

int& a) const

highprecision

operator-= (const highprecision&a)/**

* @brief 高精度乘法

* @param a 乘數

* @return 積

*/highprecision

operator* (const highprecision& a) const

}return

ans;

}highprecision

operator* (const

int& a) const

highprecision

operator*= (const highprecision&a)/**

* @brief 高精度除法, 請自覺檢測除 0 的錯誤

* @param a 除數

* @return 商

*/highprecision

operator/ (const highprecision& a) const

dividend -=temp.multiply_one_digit(low);

ans[ans.length() - 1] +=low;

}if(temp.num.length() == a.num.length())break

; temp.num = temp.num.substr(1

); }

while(temp.num.length() >=a.num.length());

while(ans[0] == '

0')ans = ans.substr(1

);

return

highprecision(ans);

}highprecision

operator/ (const

int& a) const

highprecision

operator/= (const highprecision&a)

highprecision

operator% (const highprecision& a) const

highprecision

operator%= (const highprecision&a)

friend ostream& operator

<<(ostream&, const highprecision&);

};ostream& operator

<<(ostream& os, const highprecision&a)

return

os;}

高精度四則運算模板

1.讀取方式 利用 c string 讀取 2.儲存方式 利用c vector 儲存 示例 string a,b vector int a,b cin a b 對每個字元型數字減去 0 for int i a.size 1 i 0 i a.push back a i 0 數字逆序儲存 低位先存 高位...

ACM學習歷程25 高精度四則運算

在acm的題目中會涉及到一些四則運算,需要注意的是這裡參與四則運算的資料的資料型別並非整型。事實上,儘管整型可以分配4個位元組的儲存空間,但是它所能表示的資料範圍仍然是有限的,所以在比賽上遇到加法等一些涉及四則運算的題目時,一定要留意最終結果或者中間結果是否會超出基本資料型別的表示範圍。如果確定利用...

高精度四則運算 大數加減乘除

791 高精度加法 給定兩個正整數,計算它們的和。輸入格式 共兩行,每行包含乙個整數。輸出格式 共一行,包含所求的和。資料範圍 1 整數長度 100000 輸入樣例 1223 輸出樣例 35 include include define lenmax 100000 int main while i ...