大整数模拟

大整数模拟,很常用,记录下。

1. 加法模拟:

string res,a,b;

res = a + b

string &_string_add_string(const string &a, const string &b, string &res)  
{  
    int sum_value = 0, add_bit = 0;  
    const int alen = a.length(), blen = b.length();  
    res = "0" + (alen > blen ? a : b);  
    for (int i = alen-1, j = blen-1, k = res.length() - 1;   
         i >= 0 || j >= 0 || add_bit > 0;   
         --i, --j, --k){  
        sum_value = (i>=0 ? a[i]-48: 0) + (j>=0 ? b[j]-48: 0) + add_bit;  
        add_bit = sum_value / 10;   
        res[k] = sum_value%10 + '0';  
    }  
    if (res[0] == '0') res = res.substr(1, res.length() - 1);  
    return res;  
}  

2. 减法:

void sub(char *szLine1, char *szLine2, int nLen1, int nLen2)  
{  
     int i , j = 0;  
     for( i = nLen1 - 1;i >= 0 ; i --)  
          an1[j++] = szLine1[i] - '0';      
  
     j = 0;  
     for( i = nLen2 - 1;i >= 0 ; i--)  
          an2[j++] = szLine2[i] - '0';  
  
     for( i = 0;i < MAX_LEN ; i ++ )   
     {   
         if(an1[i] >= an2[i]) //逐位相减  
                    an1[i] -= an2[i];              
         else  
         {  
             an1[i+1]--;  
             an1[i] += 10;  
             an1[i] -= an2[i];    
         }   
     }  
  
     bool bStartOutput = false;     //此变量用于跳过多余的0  
     for( i = MAX_LEN; i >= 0; i-- )   
     {  
          if( bStartOutput)      
              printf("%d", an1[i]);  //如果多余的0已经都跳过,则输出       
          else if( an1[i] ) {             
               printf("%d", an1[i]);  
               bStartOutput = true; //碰到第一个非0的值,就说明多余的0已经都跳过                    
          }  
     }  
     if(!bStartOutput)      //结果为0特殊处理   
          printf("0");  
  
}  


3.乘法

依赖于加法

string res,a,b;

res = a * b;

和小学生计算乘法的方法一样

string &_del_zeros_before_dot(string &a)  
{  
    if (a.length() <= 0 || a[0] != '0') return a;  
    int i = 0;  
    while (i < a.length() && a[i] == '0') ++i;  
    a = a.substr(i, a.length() - i);  
    return a;  
}  
  
string &_string_add_string(const string &a, const string &b, string &res)  
{  
    int sum_value = 0, add_bit = 0;  
    const int alen = a.length(), blen = b.length();  
    res = "0" + (alen > blen ? a : b);  
    for (int i = alen-1, j = blen-1, k = res.length() - 1;   
         i >= 0 || j >= 0 || add_bit > 0;   
         --i, --j, --k){  
        sum_value = (i>=0 ? a[i]-48: 0) + (j>=0 ? b[j]-48: 0) + add_bit;  
        add_bit = sum_value / 10;   
        res[k] = sum_value%10 + '0';  
    }  
    if (res[0] == '0') res = res.substr(1, res.length() - 1);  
    return res;  
}  
  
  
string &_gen_zeros_string(int n, string &res) {  
    string temp = "0";  
    res = "";  
    while (n) {  
        if (n&1) res += temp;  
        temp += temp;  
        n >>= 1;  
    }  
    return res;  
}  
  
string &_string_multiply_char(  
    const string &a,   
    char c, int n_zeros, string &res) {  
  
    int ch = c - '0';  
    string zeros_string;  
    _gen_zeros_string(n_zeros, zeros_string);  
    res = "0" + a + zeros_string;  
      
    const int alen = a.length();  
    for (int i = alen - 1, k = alen, add_bit = 0;   
        i >= 0 || add_bit > 0; --i, --k) {  
        int v = (i>=0 ? a[i]-48: 0) * ch + add_bit;  
        add_bit = v / 10;  
        res[k] = v % 10 + '0';  
    }  
    if (res[0] == '0') res = res.substr(1, res.length() - 1);  
  
    return res;  
}  
  
string &_string_multiply_string(const string &a, const string &b, string &res) {  
    string c = a, d = b;  
    _del_zeros_before_dot(c);  
    _del_zeros_before_dot(d);  
    int clen = c.length(), dlen = d.length();  
  
    if (clen < dlen) {   
        string t = c; c = d; d = t;  
        int x = clen; clen = dlen; dlen = x;  
    }  
  
    res = "0";  
    for (int i = dlen - 1; i >= 0; --i) {  
        string temp_res;  
        _string_multiply_char(c, d[i], (dlen - 1 - i), temp_res);  
  
        string add_res;  
        _string_add_string(res, temp_res, add_res);  
        res = add_res;  
    }  
    return res;  
}  
  



4.除法


哥不会,没有好的想法

好像也不常用


posted @ 2013-03-30 22:22  海滨银枪小霸王  阅读(103)  评论(0编辑  收藏  举报