Large number of high degree of template of sharing

  • 2020-04-01 23:41:07
  • OfStack


#include <stdio.h>
#include <string.h> 
#include <stdlib.h> 
#include <math.h>
#include <assert.h>  
#include <ctype.h> 
#include <map>
#include <string>
#include <set>
#include <bitset>
#include <utility>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <iostream>
#include <fstream>
#include <list>
using  namespace  std;      

const  int MAXL = 500;      
struct  BigNum      
{      
    int  num[MAXL];      
    int  len;      
};      

//High precision comparison a> B return 1, a == b return 0; A. <B the return - 1; & have spent & have spent & have spent & have spent & have spent
int  Comp(BigNum &a, BigNum &b)      
{      
    int  i;      
    if(a.len != b.len) return (a.len > b.len) ? 1 : -1;      
    for(i = a.len-1; i >= 0; i--)      
        if(a.num[i] != b.num[i]) return  (a.num[i] > b.num[i]) ? 1 : -1;      
    return  0;      
}      

//High precision addition & NBSP; & have spent & have spent & have spent & have spent
BigNum  Add(BigNum &a, BigNum &b)      
{      
    BigNum c;      
    int  i, len;      
    len = (a.len > b.len) ? a.len : b.len;      
    memset(c.num, 0, sizeof(c.num));      
    for(i = 0; i < len; i++)      
    {      
        c.num[i] += (a.num[i]+b.num[i]);      
        if(c.num[i] >= 10)      
        {      
            c.num[i+1]++;      
            c.num[i] -= 10;      
        }      
    }      
    if(c.num[len])
  len++;      
    c.len = len;      
    return  c;      
}      
//High precision subtraction, guarantee a> = b  & have spent & have spent & have spent & have spent
BigNum Sub(BigNum &a, BigNum &b)      
{      
    BigNum  c;      
    int  i, len;      
    len = (a.len > b.len) ? a.len : b.len;      
    memset(c.num, 0, sizeof(c.num));      
    for(i = 0; i < len; i++)      
    {      
        c.num[i] += (a.num[i]-b.num[i]);      
        if(c.num[i] < 0)      
        {      
            c.num[i] += 10;      
            c.num[i+1]--;      
        }      
    }      
    while(c.num[len] == 0 && len > 1)
  len--;      
    c.len = len;      
    return  c;      
}      
//High precision multiplied by low precision, when b is very large, may happen to overflow int range, specific situation specific analysis & NBSP; & have spent & have spent & have spent & have spent
//If b is large, you can consider b as high precision & NBSP; & have spent & have spent & have spent & have spent
BigNum Mul1(BigNum &a, int  &b)      
{      
    BigNum c;      
    int  i, len;      
    len = a.len;      
    memset(c.num, 0, sizeof(c.num));      
    //Times 0, returns 0& PI; & have spent & have spent & have spent & have spent
    if(b == 0)       
    {      
        c.len = 1;      
        return  c;      
    }      
    for(i = 0; i < len; i++)      
    {      
        c.num[i] += (a.num[i]*b);      
        if(c.num[i] >= 10)      
        {      
            c.num[i+1] = c.num[i]/10;      
            c.num[i] %= 10;      
        }      
    }      
    while(c.num[len] > 0)      
    {      
        c.num[len+1] = c.num[len]/10;      
        c.num[len++] %= 10;      
    }      
    c.len = len;       
    return  c;      
}      

//High precision times high precision, be careful to carry in time, or it may cause overflow, but it will increase the complexity of the algorithm, & have spent & have spent & have spent & have spent
//If you are sure that no overflow will occur, change the inside while to if ; & have spent & have spent & have spent & have spent
BigNum  Mul2(BigNum &a, BigNum &b)      
{      
    int i, j, len = 0;      
    BigNum  c;      
    memset(c.num, 0, sizeof(c.num));      
    for(i = 0; i < a.len; i++)
 {
        for(j = 0; j < b.len; j++)      
        {      
            c.num[i+j] += (a.num[i]*b.num[j]);      
            if(c.num[i+j] >= 10)      
            {      
                c.num[i+j+1] += c.num[i+j]/10;      
                c.num[i+j] %= 10;      
            }      
        }
 }
    len = a.len+b.len-1;      
    while(c.num[len-1] == 0 && len > 1)
  len--;      
    if(c.num[len])
  len++;      
    c.len = len;      
    return  c;      
}      

//High precision divided by low precision, the result is c and the remainder is f& c. & have spent & have spent & have spent & have spent
void Div1(BigNum &a, int &b, BigNum &c, int &f)      
{      
    int  i, len = a.len;      
    memset(c.num, 0, sizeof(c.num));      
    f = 0;      
    for(i = a.len-1; i >= 0; i--)      
    {      
        f = f*10+a.num[i];      
        c.num[i] = f/b;      
        f %= b;      
    }      
    while(len > 1 && c.num[len-1] == 0)
  len--;      
    c.len = len;      
}      
//High precision * 10 & have spent & have spent & have spent & have spent & have spent
void  Mul10(BigNum &a)      
{      
    int  i, len = a.len;      
    for(i = len; i >= 1; i--)      
        a.num[i] = a.num[i-1];      
    a.num[i] = 0;      
    len++;      
    //if a == 0      
    while(len > 1 && a.num[len-1] == 0)
  len--;      
}      

//High precision divided by high precision, the divisor is c and the remainder is f& c; & have spent & have spent & have spent & have spent
void Div2(BigNum &a, BigNum &b, BigNum &c, BigNum &f)      
{      
    int  i, len = a.len;      
    memset(c.num, 0, sizeof(c.num));      
    memset(f.num, 0, sizeof(f.num));      
    f.len = 1;      
    for(i = len-1;i >= 0;i--)      
    {      
        Mul10(f);      
        //The remainder is multiplied by 10& NBSP; & have spent & have spent & have spent & have spent
        f.num[0] = a.num[i];      
        //And then the remainder plus the next digit & PI; & have spent & have spent & have spent & have spent
        /// the use of subtraction to replace division & NBSP; & have spent & have spent & have spent & have spent
        while(Comp(f, b) >= 0)      
        {
            f = Sub(f, b);      
            c.num[i]++;      
        }      
    }      
    while(len > 1 && c.num[len-1] == 0)
  len--;      
    c.len = len;      
}   
void  print(BigNum &a)   //Output large number & NBSP; & have spent
{      
    int  i;      
    for(i = a.len-1; i >= 0; i--)      
        printf("%d", a.num[i]);      
    puts("");      
}      
//Converts a string to a large number and stores it in the BigNum structure & NBSP; & have spent & have spent & have spent & have spent
BigNum ToNum(char *s)      
{      
    int i, j;      
    BigNum  a;      
    a.len = strlen(s);      
    for(i = 0, j = a.len-1; s[i] != '0'; i++, j--)      
        a.num[i] = s[j]-'0';      
    return  a;      
}      

void Init(BigNum &a, char *s, int &tag)   //Converts a string to a large number
{   
    int  i = 0, j = strlen(s); 
    if(s[0] == '-')
 {
  j--;
  i++;
  tag *= -1;
 }
    a.len = j;
    for(; s[i] != '0'; i++, j--)
        a.num[j-1] = s[i]-'0';
}   

int main(void)      
{      
    BigNum a, b;   
    char  s1[100], s2[100];   
    while(scanf("%s %s", s1, s2) != EOF)   
    {   
        int tag = 1;   
        Init(a, s1, tag);    //Converts a string to a large number
        Init(b, s2, tag);   
        a = Mul2(a, b);   
        if(a.len == 1 && a.num[0] == 0)   
        {   
            puts("0");   
        }   
        else    
        {   
            if(tag < 0) putchar('-');   
            print(a);   
        }   
    }   
    return 0;   
}

Related articles: