Precision addition, subtraction, multiplication, division

It is divided into five areas to carry out:

1. The high-precision number is stored, in accordance with the I "clear God book" int array comprises using a struct bign indicating int length to store data, of course, for convenience, we store our normal direction and the arrangement direction of the size of the figures On the contrary, we are storing an array of small low order numbers, calculated after so convenient

2. addition, the addition should be the direct simulation, remember to use to store a carry carry on the line, the other not much to say

2. subtraction, subtraction, then that is more a higher ground borrow, both a high place -1 +10 own position, and finally to eliminate the leading 0

4. multiplication, multiplication is also simple analog, two each and every number multiplied and then summed

5. division, the division is the most difficult one, although it has also analog, but you use a high-precision high-precision divided by the number of times a little more difficult, first of all the entire simulation process should be clear:

  Input dividend a, the divisor b

  Each cycle remainder = dividend + 10 * the number of the current position, with a dividend by a divisor (ie our divisor input divisor b), if

  (1) In addition to enough, then it would get business, and the new remainder

  (2) If not removed, the dividend assigned directly to the remainder

  Next enters a loop until all bits of a run

  There is a skill that is (1) how to be removed, because it is divided by the high-precision accuracy, so I can not use the built-division, it is used here to simulate subtraction division (ie continue to decrease until the reduction can not)

 

#include<iostream>
#include<stdio.h>
#include<string>
#include<cstring>
using namespace std;

const int max_size=1000;

struct bign{
    int d[max_size];
    int len;
    bign(){
        len=0;
        memset(d,0,sizeof(d));
    } 
};

bign to_bign(string str){
    bign big;
    int size=str.size();
    big.len= Size;
     for ( int I = 0 !; I = size; ++ I) 
        big.d [I] = STR [size- . 1 -i] - ' 0 ' ;   // Note that the string is converted to char int , use -'0 'work at 
    return Big; 
} 

bign the Add (bign A, B bign) { 
    bign C; 
    int with Carry = 0 ;
     for ( int I = 0 ; I <= a.len- . 1 || I < b.len- = . 1 ; ++ I) {
         int TEMP = AD + with Carry [I] + BD [I]; 
        CD [c.len ++] = TEMP%10;
        carry=temp/10;
    } 
    if(carry!=0){
        c.d[c.len++]=carry;
    }
    return c;
}

int cmp(bign a,bign b){
    int i;
    if(a.len>=b.len)
        i=a.len;
    else i=b.len;
    for(;i>=0;i--){
        if(a.d[i]>b.d[i]) return 1;
        else if(a.d[i]<b.d[i]) return- . 1 ; 
    } 
    return  0 ; 
} 

bign Sub (bign A, B bign) { // ab &, to ensure that A> B 
    bign C;
     for ( int I = 0 ; I <a.len; I ++) {   // A it is certainly a long 
        IF (AD [I] -bd [I] < 0 ) { 
            AD [I + . 1 ] - ; 
            AD [I] + = 10 ; 
        } 
        CD [c.len ++] = AD [ I] - BD [I]; 
    } 
    // eliminating preamble 0 
    the while (c.len- . 1 > = . 1 && c.d[c.len-1]==0){
        c.len--;
    }
    return c;
}

bign multi(bign a,bign b){
    bign c;
    for(int i=0;i<a.len;++i){
        int m=a.d[i];
        for(int j=0;j<b.len;++j){
            int temp=c.d[i+j]+m*b.d[j];
            c.d[i+j]=temp%10;
            c.d[i+j+1]+=temp/10;
        }
    } 
     I) {IF (CD [a.len + b.len- . 1 ] =! 0 ) 
        c.len = a.len + b.len;
     the else c.len + = a.len b.len- . 1 ;
     return C; 
} 

/ / precision divided by precision, so hard to write ah, the original card I where the analog divider subtraction 
bign result_n; 
bign remain_n; 
bign division (bign a, B bign) { // a / B 
    int bits = B. len; 
    bign REMAIN; 
    bign SUB1; 
    String result_str;
     for ( int I = a.len- . 1 ; I> = 0 ; - 
        REMAIN=multi(remain,to_bign("10")); //余数*10
        string str;
        str+=('0'+a.d[i]);
        sub1=add(remain,to_bign(str));
        int if_bigger=cmp(sub1,b);
        if(if_bigger>=0){
            //法模拟除法
            int count=0;
            while(cmp(sub1,b)>=0){
                sub1=sub(sub1,b);
                count++;
            }
            result_str+=('0'+count); //存下商 
            remain=sub1; //保留此次余数 
        }
        else{
            remain=sub1;
        }
    }
    result_n=to_bign(result_str);
    remain_n=remain;
}

void print_bign(bign b){
    for(int i=b.len-1;i>=0;--i)
        printf("%d", BD [I]); 
} 

int main () { 
    The freopen ( " in.txt " , " R & lt " , stdin); 
    
    string str1, str2; 
    CIN >> >> str1 str2;   // write string objects or with cin >> string and cout << string, do not use scanf, printf, otherwise there is a problem 
    bign b1 = to_bign (str1); 
    bign b2 = to_bign (str2); 

    Division (b1, b2); 
    print_bign (result_n); 
    printf ( "    " ); 
    print_bign (remain_n); 
    
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/chuan-chuan/p/11498452.html