大整數加減乘除(樸素版)

2021-09-10 21:28:37 字數 1239 閱讀 7969

思路見注釋。

#include#include#includeusing namespace std;

int cmp(string a, string b)

//兩個正整數相加

string add(string a, string b)

} reverse(a.begin(), a.end());//逆轉回正常的數字

return a;

}//兩個正整數相減

string sub(string a, string b) //否則,調整被減數大於減數,如果進行了交換,則結果為負

reverse(a.begin(), a.end()); //逆轉數字

reverse(b.begin(), b.end());

for (int i = 0; i < a.size(); i++)

} reverse(a.begin(), a.end()); //逆轉回正常的數字

//刪除前導 0

int i = 0; //令 i 指向第一位

while (a[i] == '0') i++; //如果該位置為 0,看下一位,迴圈終止於第乙個非零位

a.erase(a.begin(), a.begin() + i);//刪除前導 0

if (flag) return "-" + a; //如果結果為負,加上前導負號

else return a; //否則直接返回被減數

}//高精度正整數乘以低精度(int)正整數

string multi(string a, string b)

while (carry)

reverse(c.begin(), c.end()); //逆轉回正常的數字

return c;

}//高精度正整數除以低精度(int)正整數

string divide(string a, string b, int& r)

} //刪除前導 0

int i = 0; //令 i 指向第一位

while (a[i] == '0') i++; //如果該位置為 0,看下一位,迴圈終止於第乙個非零位

a.erase(a.begin(), a.begin() + i);//刪除前導 0

if (a.empty()) return "0"; //如果結果為 0,返回 0

else return a; //否則直接返回被除數

}int main()

加減乘除混合版

psp2.1 personal software process stages 預估耗時 分鐘 實際耗時 分鐘 planning 計畫 estimate 估計這個任務需要多少時間 2030 development 開發 analysis 需求分析 包括學習新技術 600679 design spec...

大整數的加減乘除求餘

include include define n 50 n是計算的位數 class integer 大整數類 過載建構函式 友元函式實現運算子過載 friend integer operator integer integer friend integer operator integer int ...

大整數的加減乘除取模

include include using namespace std inline int compare string str1,string str2 高精度加法 string add int string str1,string str2 else else else int int1 0,...