这个是搜到的,感觉还不错粘过来看看
  1. #include <iostream> 
  2. #include <string> 
  3. using namespace std; 
  4. inline int compare(string str1, string str2) { 
  5.     if(str1.size() > str2.size()) //长度长的整数大于长度小的整数 
  6.         return 1; 
  7.     else if(str1.size() < str2.size()) 
  8.         return -1; 
  9.     else 
  10.         return str1.compare(str2); //若长度相等,从头到尾按位比较,compare函数:相等返回0,大于返回1,小于返回-1 
  11. string ADD_INT(string str1, string str2) {//高精度加法 
  12.     string SUB_INT(string str1, string str2); 
  13.     int sign = 1; //sign 为符号位 
  14.     string str; 
  15.     if(str1[0] == '-') { 
  16.         if(str2[0] == '-') { 
  17.             sign = -1; 
  18.             str = ADD_INT(str1.erase(0, 1), str2.erase(0, 1)); 
  19.         } else { 
  20.             str = SUB_INT(str2, str1.erase(0, 1)); 
  21.         } 
  22.     } else { 
  23.         if(str2[0] == '-'
  24.             str = SUB_INT(str1, str2.erase(0, 1)); 
  25.         else { 
  26.             //把两个整数对齐,短整数前面加0补齐 
  27.             string::size_type l1, l2; 
  28.             int i; 
  29.             l1 = str1.size(); l2 = str2.size(); 
  30.             if(l1 < l2) { 
  31.                 for(i = 1; i <= l2 - l1; i++) 
  32.                     str1 = "0" + str1; 
  33.             } else { 
  34.                 for(i = 1; i <= l1 - l2; i++) 
  35.                     str2 = "0" + str2; 
  36.             } 
  37.             int int1 = 0, int2 = 0; //int2 记录进位 
  38.             for(i = str1.size() - 1; i >= 0; i--) { 
  39.                 int1 = (int(str1[i]) - '0' + int(str2[i]) - '0' + int2) % 10; 
  40.                 int2 = (int(str1[i]) - '0' + int(str2[i]) - '0' +int2) / 10; 
  41.                 str = char(int1 + '0') + str; 
  42.             } 
  43.             if(int2 != 0) str = char(int2 + '0') + str; 
  44.         } 
  45.     } 
  46.     //运算后处理符号位 
  47.     if((sign == -1) && (str[0] != '0')) 
  48.         str = "-" + str; 
  49.     return str; 
  50.   
  51. string SUB_INT(string str1, string str2) {//高精度减法 
  52.     int sign = 1; //sign 为符号位 
  53.     string str; 
  54.     int i; 
  55.     if(str2[0] == '-'
  56.         str = ADD_INT(str1, str2.erase(0, 1)); 
  57.     else { 
  58.         str1.erase(0,str1.find_first_not_of('0')); 
  59.         str2.erase(0,str1.find_first_not_of('0'));//先取出s1,s2前面的0 
  60.         int res = compare(str1, str2); 
  61.         if(res == 0) return "0"
  62.         if(res < 0) { 
  63.             sign = -1; 
  64.             string temp = str1; 
  65.             str1 = str2; 
  66.             str2 = temp; 
  67.         }//将s1变为大的那个 
  68.         string::size_type tempint; 
  69.         tempint = str1.size() - str2.size(); 
  70.         for(i = str2.size() - 1; i >= 0; i--) { 
  71.             if(str1[i + tempint] < str2[i]) { 
  72.                 str1[i + tempint - 1] = char(int(str1[i + tempint - 1]) - 1);//不够就+10再减 
  73.                 str = char(str1[i + tempint] - str2[i] + ':') + str;//:在ascii中为9后面一位 
  74.             } else 
  75.                 str = char(str1[i + tempint] - str2[i] + '0') + str; 
  76.         } 
  77.         for(i = tempint - 1; i >= 0; i--) 
  78.             str = str1[i] + str; 
  79.     } 
  80.     //去除结果中多余的前导0 
  81.     str.erase(0, str.find_first_not_of('0')); 
  82.     if(str.empty()) str = "0"
  83.     if((sign == -1) && (str[0] != '0')) 
  84.         str = "-" + str; 
  85.     return str; 
  86.   
  87. string MUL_INT(string str1, string str2) {//高精度乘法 
  88.     int sign = 1; //sign 为符号位 
  89.     string str = "0"
  90.     if(str1[0] == '-') { 
  91.         sign *= -1; 
  92.         str1 = str1.erase(0, 1); 
  93.     } 
  94.     if(str2[0] == '-') { 
  95.         sign *= -1; 
  96.         str2 = str2.erase(0, 1); 
  97.     } 
  98.     int i, j; 
  99.     string::size_type l1, l2; 
  100.     l1 = str1.size(); l2 = str2.size(); 
  101.     for(i = l2 - 1; i >= 0; i --) {  //实现手工乘法 
  102.         string tempstr; 
  103.         int int1 = 0, int2 = 0, int3 = int(str2[i]) - '0'
  104.         if(int3 != 0) { 
  105.             for(j = 1; j <= (int)(l2 - 1 - i); j++) 
  106.                 tempstr = "0" + tempstr;//判断位数,补0 
  107.             for(j = l1 - 1; j >= 0; j--) { 
  108.                 int1 = (int3 * (int(str1[j]) - '0') + int2) % 10; 
  109.                 int2 = (int3 * (int(str1[j]) - '0') + int2) / 10; 
  110.                 tempstr = char(int1 + '0') + tempstr; 
  111.             } 
  112.             if(int2 != 0) tempstr = char(int2 + '0') + tempstr; 
  113.         }//如果s2末位不为0,temstr为末位乘str1 
  114.         else 
  115.         { 
  116.             tempstr = "0"
  117.         }//如果s2末位为0,temstr为末位乘str1=0 
  118.         str = ADD_INT(str, tempstr);//每算出一组数与上一组相加,放在str中 
  119.     } 
  120.     //去除结果中的前导0 
  121.     str.erase(0, str.find_first_not_of('0')); 
  122.     if(str.empty()) str = "0"
  123.     if((sign == -1) && (str[0] != '0')) 
  124.         str = "-" + str; 
  125.     return str; 
  126.   
  127. string DIVIDE_INT(string str1, string str2, int flag) {//高精度除法 
  128.     //flag = 1时,返回商; flag = 0时,返回余数 
  129.     string quotient, residue; //定义商和余数 
  130.     int sign1 = 1, sign2 = 1; 
  131.     if(str2 == "0") {  //判断除数是否为0 
  132.         quotient = "ERROR!"
  133.         residue = "ERROR!"
  134.         if(flag == 1) return quotient; 
  135.         else return residue; 
  136.     } 
  137.     if(str1 == "0") { //判断被除数是否为0 
  138.         quotient = "0"
  139.         residue = "0"
  140.     } 
  141.     if(str1[0] == '-') { 
  142.         str1 = str1.erase(0, 1); 
  143.         sign1 *= -1; 
  144.         sign2 = -1; 
  145.     } 
  146.     if(str2[0] == '-') { 
  147.         str2 = str2.erase(0, 1); 
  148.         sign1 *= -1; 
  149.     } 
  150.     int res = compare(str1, str2); 
  151.     if(res < 0) { 
  152.         quotient = "0"
  153.         residue = str1; 
  154.     } else if(res == 0) { 
  155.         quotient = "1"
  156.         residue = "0"
  157.     } else { 
  158.         string::size_type l1, l2; 
  159.         l1 = str1.size(); l2 = str2.size(); 
  160.         string tempstr; 
  161.         tempstr.append(str1, 0, l2 - 1);//最开始的时候取str1与str2相等的位数的字符串为tempstr 
  162.         //模拟手工除法 
  163.         for(int i = l2 - 1; i < l1; i++) { 
  164.             tempstr = tempstr + str1[i]; 
  165.             tempstr.erase(0, tempstr.find_first_not_of('0'));//zhao4zhong1添加 
  166.             if(tempstr.empty()) tempstr = "0";//zhao4zhong1添加 
  167.   
  168.             for(char ch = '9'; ch >= '0'; ch --) { //试商 
  169.                 string str; 
  170.                 str = str + ch; 
  171.                 if(compare(MUL_INT(str2, str), tempstr) <= 0) { 
  172.                     quotient = quotient + ch; 
  173.                     tempstr = SUB_INT(tempstr, MUL_INT(str2, str)); 
  174.                     break
  175.                 } 
  176.             }//一位位增加试商 
  177.         } 
  178.         residue = tempstr; 
  179.     } 
  180.     //去除结果中的前导0 
  181.     quotient.erase(0, quotient.find_first_not_of('0')); 
  182.     if(quotient.empty()) quotient = "0"
  183.     if((sign1 == -1) && (quotient[0] != '0')) 
  184.         quotient = "-" + quotient; 
  185.     if((sign2 == -1) && (residue[0] != '0')) 
  186.         residue = "-" + residue; 
  187.     if(flag == 1) return quotient; 
  188.     else return residue; 
  189.   
  190. string DIV_INT(string str1, string str2) {//高精度除法,返回商(不含小数) 
  191.     return DIVIDE_INT(str1, str2, 1); 
  192.   
  193. string MOD_INT(string str1, string str2) {//高精度除法,返回余数 
  194.     return DIVIDE_INT(str1, str2, 0); 
  195.   
  196. int main() { 
  197.     char ch; 
  198.     string s1, s2, res; 
  199.     while(cin >> ch) { 
  200.   
  201.         cin >> s1 >> s2; 
  202.         switch(ch) { 
  203.         case '+':  res = ADD_INT(s1, s2); break
  204.         case '-':  res = SUB_INT(s1, s2); break
  205.         case '*':  res = MUL_INT(s1, s2); break
  206.         case '/':  res = DIV_INT(s1, s2); break
  207.         case '%':  res = MOD_INT(s1, s2); break
  208.         default :  break
  209.         } 
  210.         cout << res << endl; 
  211.     } 
  212.     return(0);