



下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、高精度計算模板注意:減法、除法要用到 compare 函數乘法需要加法的部分,加法需要減法部分#include <iostream>#include <string>using namespace std;int compare(string str1, string str2)if(str1.size() > str2.size()return 1;else if(str1.size() < str2.size()return -1;elsereturn pare(str2);int main()char ch;string s1, s2, res;whi
2、le(cin >> ch) cin >> s1>> s2;高精度加法高精度減法高精度乘法 高精度除法 ,返回商 高精度除法 , 返回余數switch(ch) case '+': res = ADD_INT(s1, s2); break; / case '-': res = MINUS_INT(s1, s2); break; / case '*': res = MULTIPLY_INT(s1, s2); break; / case '/': res = DIV_INT(s1, s2); break
3、; / case 'm': res = MOD_INT(s1, s2); break; / default : break;cout << res<< endl;return(0);/ 高精度加法string ADD_INT(string str1, string str2)string MINUS_INT(string str1, string str2);int sign = 1;string str;if(str10 = '-') if(str20 = '-') sign = -1;str = ADD_INT(str
4、1.erase(0, 1), str2.erase(0, 1);else str = MINUS_INT(str2, str1.erase(0, 1);else if(str20 = '-')str = MINUS_INT(str1, str2.erase(0, 1);else string:size_type l1, l2;int i;l1 = str1.size(); l2 = str2.size();if(l1 < l2) for(i = 1; i <= (int)(l2 - l1); i+)str1 = "0" + str1;else f
5、or(i = 1; i <= (int)(l1 - l2); i+)str2 = "0" + str2;int int1 = 0, int2 = 0;for(i = str1.size() - 1; i >= 0; i-) int1 = (int(str1i) - 48 + int(str2i) - 48 + int2) %10;int2 = (int(str1i) - 48 + int(str2i) - 48 +int2) / 10; str = char(int1 + 48) + str;if(int2 != 0) str = char(int2 + 48)
6、 + str;/ 運算后處理符號位if(sign = -1) && (str0 !='0')str = "-" + str;return str;/ 高精度減法 string MINUS_INT(string str1, string str2) string MULTIPLY_INT(string str1, string str2); int i,sign = 1;string str; if(str20 = '-') str = ADD_INT(str1, str2.erase(0, 1);else int res =
7、compare(str1, str2); if(res = 0) return "0" if(res < 0) sign = -1; string temp = str1; str1 = str2; str2 = temp; string:size_type tempint; tempint = str1.size() - str2.size(); for(int i = str2.size() - 1; i >= 0; i-) if(str1i + tempint < str2i) str1i + tempint - 1 = char(int(str1i
8、 + tempint - 1) - 1); str = char(str1i + tempint - str2i + 58) + str; else str = char(str1i + tempint - str2i + 48) + str; for(i = tempint - 1; i >= 0; i-) str = str1i + str;/ 去除結果中多余的前導 0 str.erase(0, str.find_first_not_of('0'); if(str.empty() str = "0" if(sign = -1) &&
9、 (str0 !='0') str = "-" + str;return str; / 高精度乘法string MULTIPLY_INT(string str1, string str2)int sign = 1;string str;if(str10 = '-') sign *= -1;str1 = str1.erase(0, 1);if(str20 = '-') sign *= -1;str2 = str2.erase(0, 1);int i, j;string:size_type l1, l2;l1 = str1.siz
10、e(); l2 = str2.size();for(i = l2 - 1; i >= 0; i -) / 實現手工乘法string tempstr;int int1 = 0, int2 = 0, int3 = int(str2i) - 48;if(int3 != 0) for(j = 1; j <= (int)(l2 - 1 - i); j+)tempstr = "0" + tempstr;for(j = l1 - 1; j >= 0; j-) int1 = (int3 * (int(str1j) - 48) + int2) % 10; int2 = (i
11、nt3 * (int(str1j) - 48) + int2) / 10; tempstr = char(int1 + 48) + tempstr;if(int2 != 0) tempstr = char(int2 + 48) + tempstr;str = ADD_INT(str, tempstr);/ 去除結果中的前導 0str.erase(0, str.find_first_not_of('0');if(str.empty() str = "0"if(sign = -1) && (str0 !='0')str = &qu
12、ot;-" + str;return str;/ 高精度除法string DIVIDE_INT(string str1, string str2, int flag)/flag = 1 時,返回商; flag = 0 時,返回余數string quotient, residue;int sign1 = 1, sign2 = 1, i;if(str2 = "0") / 判斷除數是否為 0 quotient = "ERROR!"residue = "ERROR!" if(flag = 1) return quotient; el
13、se return residue;if(str1 = "0") / 判斷被除數是否為 0 quotient = "0"residue = "0"if(str10 = '-') str1 = str1.erase(0, 1);sign1 *= -1; sign2 = -1;if(str20 = '-') str2 = str2.erase(0, 1); sign1 *= -1;int res = compare(str1, str2);if(res < 0) quotient = "0&
14、quot; residue = str1;else if(res = 0) quotient = "1" residue = "0"else string:size_type l1, l2; l1 = str1.size(); l2 = str2.size(); string tempstr;tempstr.append(str1, 0, l2 - 1);/ 模擬手工除法 for(i = l2 - 1; i < l1; i+) tempstr = tempstr + str1i;for(char ch = '9' ch >=
15、'0' ch -) string str;str = str + ch;if(compare(MULTIPLY_INT(str2, str), tempstr) <= 0)quotient = quotient + ch;tempstr = MINUS_INT(tempstr, MULTIPLY_INT(str2,str);break;residue = tempstr; / 去除結果中的前導 0quotient.erase(0, quotient.find_first_not_of('0'); if(quotient.empty() quotient = "0"if(sign1 = -1) && (quotient0 !='0') quotient = "-" + quotient;if(sign2 = -1) && (residue0 !='0') residue = "-&quo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權】 ISO 19880-5:2025 EN Gaseous hydrogen - Fuelling stations - Part 5: Dispenser hoses and hose assemblies
- 獸藥合理用藥在獸醫實踐中的案例分析考核試卷
- 環保材料在化妝品配方中的生物降解性分析考核試卷
- 藥品注冊與審批流程風險管理考核試卷
- 印刷機械壓力監控技術考核試卷
- 部編人教版小學四年級語文下冊《小英雄雨來》教學課件
- 2025年中國PET彩色鍍鋁膜數據監測研究報告
- 2025年中國IC卡手持機數據監測報告
- 2025年中國DG-鈀觸媒數據監測報告
- 2025年中國11度啤酒數據監測報告
- 職業行為習慣課件
- 高校智能化教學評價體系變革的技術創新路徑研究
- 高中復讀協議書
- 2024年甘肅省臨澤縣教育局公開招聘試題含答案分析
- 2025-2030中國戊烷發泡劑市場深度解析及前景運行動態研究報告
- 移植物抗宿主病分期及護理
- 2024年深圳市中考生物試卷真題(含答案解析)
- DB31/T 1402-2023養老機構認知障礙照護單元設置和服務要求
- 防腐工程項目建議書(立項報告)
- 2025年安全管理員安全培訓考試試題附參考答案(綜合題)
- 河南省新鄉市2023-2024學年高一物理下學期7月期末考試含答案
評論
0/150
提交評論