



下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、-5 -注意:減法、除法要用到compare函數(shù)乘法需要加法的部分,加法需要減法部分#include <iostream>#include <string>using namespace std;int compare(string strl, 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;while
2、(cin >> ch) (cin >> s1>> s2;switch(ch) (高精度加法高精度減法高精度乘法高精度除法,返回商高精度除法,返回余數(shù)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; /case
3、'm': res = MOD_INT(s1, s2); break; / default : break;cout << res<< endl;return(0);/高精度加法string ADD_INT(string strl, string str2)string MINUS_INT(string strl, string st;int sign = 1;string str;if(str10 = '-') if(str20 = '-') sign = -1;str = ADD_INT(str1.erase(0, 1)
4、, 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 for(i = 1; i
5、 <= (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 + in t2) %10;int2 = (int(str1i) - 48 + int(str2i) - 48 +int2) / 10;str = char(int1 + 48) + str;if(int2 != 0) str = char(int2 + 48) + str;/運算
6、后處理符號位if(sign = -1) && (str0 !='0')str = "-" + str;return str;/高精度減法string MINUS_INT(string strl, string ststring MULTIPLY_INT(string strl, string st;int i,sign = 1;string str;if(str20='-')str = ADD_INT(str1, str2.erase(0, 1);else int res = compare(str1, str2);if(re
7、s = 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 + tempint - 1) - 1);str = char(str1i
8、 + tempint - str2i + 58) + str;elsestr = char(str1i + tempint - str2i + 48) + str;for(i = tempint - 1; i >= 0; i-) str = str1i + str;/去除結果中多余的前導0str.erase(0, str.find_first_not_of('0');if(str.empty() str = "0"if(sign = -1) && (str0 !='0')str = "-" + str
9、;return str;/高精度乘法string MULTIPLY_INT(string strl, string stint 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.size(); l2 = str2.size();for(i = l2 - 1; i >= 0; i -
10、) /實現(xiàn)手工乘法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 = (int3 * (int(str1j) - 48) + int2) / 10;tempstr = char(i
11、nt1 + 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 = "-" + str;return str;/高精度除法string DIVIDE_INT(stri
12、ng strl, string str2, int flag)/flag = 1 時,返回W ; flag = 0時,返回余數(shù)string quotient, residue;int sign1 = 1, sign2 = 1, i;if(str2 = "0") /判斷除數(shù)是否為0quotient = "ERROR!"residue = "ERROR!"if(flag = 1) return quotient;else return residue;if(str1 = "0") /判斷被除數(shù)是否為0quotient
13、= "0"residue = "0"if(str10 = '-') strl = str1.erase(0, 1);signl *= -1;sign2 = -1;if(str20 = '-') str2 = str2.erase(0, 1);sign1 *= -1;int res = compare(str1, str2);if(res < 0) quotient = "0"residue = str1;else if(res = 0) quotient = "1"residu
14、e = "0"else string:size_type 11, 12;11 = str1.size(); 12 = str2.size();string tempstr;tempstr. append(str1,0, 12 - 1);/模擬手工除法for(i = 12 - 1; i < 11; i+) tempstr = tempstr + str1i;for(char ch = '9' ch >= '0' ch -) string str;str = str + ch;if(compare(MULTIPLY_INT(str2,
15、 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') res
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 項目的可行性分析與評估計劃
- 2025年電子廚房秤項目建議書
- 2025年微球載體材料合作協(xié)議書
- 醫(yī)院銷售合同
- 電商交易平臺的商品展示與服務免責協(xié)議書
- Oxadixyl-Standard-生命科學試劑-MCE
- Dimethenamide-P-Standard-生命科學試劑-MCE
- 4-Aminonicotinic-acid-生命科學試劑-MCE
- 2-3-Isopropylideneguanosine-生命科學試劑-MCE
- 幼兒繪本小藍和小黃讀后感
- 五年級下冊音樂課程綱要
- 《酒后駕車的危害》課件
- 食材配送、包裝、運輸、驗收、售后服務方案應急預案
- 萬千教育學前讀懂兒童的思維:支持自主游戲中的圖式探索
- 無障礙設施監(jiān)理實施細則
- 中石化YC分公司易捷便利店市場營銷策略研究
- 可轉換病區(qū)應急預案與流程
- 《燃放煙花的利與弊》課件
- 醫(yī)院護理培訓課件:《病區(qū)環(huán)境管理查房》
- 《小羊和蝴蝶》繪本故事
- 鋼筋工理論考試題庫及答案
評論
0/150
提交評論