大數模版

來源:互聯網
上載者:User

大數的乘,除,加,減,取餘。

 

#include <iostream>#include <string>using namespace std;inline int compare(string str1, string str2)//去掉前置0的情況下才能比較 {      if(str1.size() > str2.size()) //長度長的整數大於長度小的整數            return 1;      else if(str1.size() < str2.size())            return -1;      else            return str1.compare(str2); //若長度相等,從頭到尾位元壓縮,compare函數:相等返回0,大於返回1,小於返回-1}//高精度加法string ADD_INT(string str1, string str2){      string MINUS_INT(string str1, string str2);      int sign = 1; //sign 為符號位      string str;      if(str1[0] == '-'){           if(str2[0] == '-') {                 sign = -1;                 str = ADD_INT(str1.erase(0, 1), str2.erase(0, 1));           }else {                 str = MINUS_INT(str2, str1.erase(0, 1));           }      }else {           if(str2[0] == '-')                 str = MINUS_INT(str1, str2.erase(0, 1));           else {                 //把兩個整數對齊,短整數前面加0補齊                 string::size_type l1, l2;                 int i;                 l1 = str1.size(); l2 = str2.size();                 if(l1 < l2) {                       for(i = 1; i <= l2 - l1; i++)                       str1 = "0" + str1;                 }else {                       for(i = 1; i <= l1 - l2; i++)                       str2 = "0" + str2;                 }                 int int1 = 0, int2 = 0; //int2 記錄進位                 for(i = str1.size() - 1; i >= 0; i--) {                       int1 = (int(str1[i]) - 48 + int(str2[i]) - 48 + int2) % 10;  //48 為 '0' 的ASCII 碼                       int2 = (int(str1[i]) - 48 + int(str2[i]) - 48 +int2) / 10;                       str = char(int1 + 48) + str;                 }                 if(int2 != 0) str = char(int2 + 48) + str;          }     }     //運算後處理符號位     if((sign == -1) && (str[0] != '0'))          str = "-" + str;     return str;}//高精度減法string MINUS_INT(string str1, string str2){     int i;     string MULTIPLY_INT(string str1, string str2);     int sign = 1; //sign 為符號位     string str;     if(str2[0] == '-')            str = ADD_INT(str1, str2.erase(0, 1));     else {            int res = 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(str1[i + tempint] < str2[i]) {                       str1[i + tempint - 1] = char(int(str1[i + tempint - 1]) - 1);                       str = char(str1[i + tempint] - str2[i] + 58) + str;                 }                 else                       str = char(str1[i + tempint] - str2[i] + 48) + str;            }           for(i = tempint - 1; i >= 0; i--)                str = str1[i] + str;     }     //去除結果中多餘的前置0     str.erase(0, str.find_first_not_of('0'));     if(str.empty()) str = "0";     if((sign == -1) && (str[0] != '0'))          str = "-" + str;     return str;}//高精度乘法string MULTIPLY_INT(string str1, string str2)//結果是去除前置0 {     int sign = 1; //sign 為符號位     string str;     if(str1[0] == '-') {           sign *= -1;           str1 = str1.erase(0, 1);     }     if(str2[0] == '-') {           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 --) {  //實現手工乘法           string tempstr;           int int1 = 0, int2 = 0, int3 = int(str2[i]) - 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(str1[j]) - 48) + int2) % 10;                        int2 = (int3 * (int(str1[j]) - 48) + int2) / 10;                        tempstr = char(int1 + 48) + tempstr;                  }                  if(int2 != 0) tempstr = char(int2 + 48) + tempstr;           }           str = ADD_INT(str, tempstr);     }     //去除結果中的前置0     str.erase(0, str.find_first_not_of('0'));     if(str.empty()) str = "0";     if((sign == -1) && (str[0] != '0'))           str = "-" + str;     return str;}//高精度除法string DIVIDE_INT(string str1, string str2, int flag)//flag = 1時,返回商; flag = 0時,返回餘數 (結果去除前置0 ) {     string quotient, residue; //定義商和餘數     int sign1 = 1, sign2 = 1;     if(str2 == "0") {  //判斷除數是否為0           quotient = "ERROR!";           residue = "ERROR!";           if(flag == 1) return quotient;           else return residue;     }     if(str1 == "0") { //判斷被除數是否為0           quotient = "0";           residue = "0";     }     if(str1[0] == '-') {           str1 = str1.erase(0, 1);           sign1 *= -1;           sign2 = -1;     }     if(str2[0] == '-') {           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";           residue = "0";     }else {           string::size_type l1, l2;           l1 = str1.size(); l2 = str2.size();           string tempstr;           tempstr.append(str1, 0, l2 - 1);           //類比手工除法           for(int i = l2 - 1; i < l1; i++) {                 tempstr = tempstr + str1[i];                 for(char ch = '9'; ch >= '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;     }     //去除結果中的前置0     quotient.erase(0, quotient.find_first_not_of('0'));     if(quotient.empty()) quotient = "0";     if((sign1 == -1) && (quotient[0] != '0'))     quotient = "-" + quotient;     if((sign2 == -1) && (residue[0] != '0'))     residue = "-" + residue;     if(flag == 1) return quotient;     else return residue;}//高精度除法,返回商string DIV_INT(string str1, string str2)//去除前置0 {      return DIVIDE_INT(str1, str2, 1);}//高精度除法,返回餘數string MOD_INT(string str1, string str2)//去除前置0 {      return DIVIDE_INT(str1, str2,0);}string s1,s2;int main(){    int KASE,res,cas=1;    cin>>KASE;    while(KASE--){        cin>>s1>>s2;        cout<<"Case "<<(cas++)<<":"<<endl;        cout<<s1<<" + "<<s2<<" = "<<ADD_INT(s1,s2)<<endl;        if(KASE) cout<<endl;    }    return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.