高精度运算

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;
string clear(string &s){
   if(s=="")
     s='0';    
   while(s.length() >0 && s[0] == '0')
     s.erase(0,1);
   if(s=="")
     s = '0';
   return s;     
}

bool Bigger(string s1, string s2) {
   clear(s1);
   clear(s2);
   if(s1.length() > s2.length())
     return true;
   if(s1.length() == s2.length() && s1 >= s2)
     return true;
   return false;
  // return s1 >= s2;    
}

string add(string s1, string s2){
   //对齐 
   while(s1.length() < s2.length())  s1 = '0' + s1;
   while(s1.length() > s2.length())  s2 = '0' + s2;  
   //cout << x.length() << " " << y.length() << endl;
   //前面补0
   s1 = '0' + s1;
   s2 = '0' + s2; 
   for(int i=s1.length()-1; i>=0; i--)
   {
       s1[i] = s1[i] + s2[i] - '0';
       if(s1[i] > '9')
       {
          s1[i] = s1[i] - 10;
          s1[i-1] += 1;             
       }    
           
   }
   clear(s1);
   return s1;  
}

string substract(string s1, string s2){
     
     bool firstBig = true;
    //对齐
     while(s1.length() < s2.length())  s1 = '0' + s1;
     while(s1.length() > s2.length())  s2 = '0' + s2;
     if(s1 < s2 )
     {
         firstBig = false;
         string temp = s2;
         s2 = s1;
         s1 = temp;
     }  
      for(int i=s1.length()-1; i>=0; i--){
         if(s1[i] >=s2[i])
             s1[i] = s1[i]-(s2[i]-'0');
         else {
              s1[i] = s1[i] +10;
              s1[i-1] -= 1;
              s1[i] = s1[i] - (s2[i]-'0'); 
         }            
      }             
       
     clear(s1);
     if(firstBig == false)
      s1 = '-' + s1;  
     return s1;
}

string multiply(string s1, string s2){
     
     string result = "0";
     for(int i=s2.length()-1; i>=0; i--) {
        for(int k=1; k<= s2[i]-'0'; k++) {
          result = add(result, s1);
         //cout << result << endl;          
        }
        s1 = s1 + '0' ;        
     }
       
     clear(result);
     if(result == "")
       result = "0";
     return result;
}

string divide(string s1, string s2){
     string result = "";
     string remainder = "";
     for(int i=0; i<s1.length(); i++){
        remainder = remainder + s1[i];
        result = result + '0';
        while(Bigger(remainder, s2)) {
           result[result.length()-1]++;
           remainder = substract(remainder, s2);                     
        }     
     }  
     clear(result);
     return result;
}

string module(string s1, string s2)
{
    string result = "";
    string remainder = "";
    if(Bigger(s2,s1))
      return s1;
     for(int i=0; i<s1.length(); i++){
        remainder = remainder + s1[i];
        result = result + '0';
        while(Bigger(remainder, s2)) {
           result[result.length()-1]++;
           remainder = substract(remainder, s2);                     
        }     
     }  
     clear(remainder);
     return remainder;   
}

//两个高精度数求最大公约数 gcd(a,b)
//依赖求余 
string Gcd(string a, string b){
     if(!Bigger(a, b)){
         string buf = a;
         a = b;
         b = buf;
     }
     //使用辗转相除法求最大公约数
     if(b == "0"){
         return a;
     }else{
         return Gcd(b, module(a, b)); 
     }
}
 
//两个高精度数求最小公倍数 lcm(a,b)
//依赖乘法
//依赖除法
//依赖最大公约数 
string Lcm(string a, string b){
    string buf = multiply(a, b);
    if(buf == "0"){
        return "0";
    }else{
        return divide(buf, Gcd(a, b)); 
    }
}


int main()
{
    int command;
    command = -1;
    string s1, s2, result;
    while(command !=0){
         cout << "1 - Addtion  2 - Substraction" << endl
              << "3 - Multiply  4 - Division" << endl
              << "5 - Mod      " << endl
              << "6 - GCD      7 - LCM" << endl;
         cin >> command;
         cout << "A: " ;   
         cin >> s1;
         cout << "B: ";
         cin >> s2; 
         switch(command){
            case 1: 
                 result = add(s1, s2);
                 cout << "A+B = " << result << endl << endl;
                 break;
            case 2:
                 result = substract(s1, s2);
                 cout << "A-B = " << result << endl << endl;
                 break;
            case 3:
                 result = multiply(s1, s2);
                 cout << "A*B = " << result << endl << endl;
                 break;
            case 4:                
                 result = divide(s1, s2);
                 cout << "A/B = " << result << endl << endl;
                 break; 
            case 5:
                 result = module(s1,s2);
                 cout << "A%B = " << result << endl << endl;
                 break;      
            case 6:
                 result = Gcd(s1,s2);
                  cout << "Gcd(A,B) = " << result << endl << endl;
                 break; 
            case 7:
                 result = Lcm(s1,s2);
                  cout << "Lcm(A,B) = " << result << endl << endl;
                 break;      
         }
    }     
    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值