Large number (high precision) template (sharing) _c language

Source: Internet
Author: User
Copy Code code as follows:

#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 accuracy Comparison a > B return 1, a = = b return 0; A < b return-1;
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
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 to ensure a >= b
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 times low precision, when B is very large may occur overflow int range, specific situation analysis
If B is big, think of B as a high precision.
Bignum Mul1 (bignum &a, int &b)
{
Bignum C;
int I, Len;
len = A.len;
memset (c.num, 0, sizeof (c.num));
Multiply by 0 and return directly to 0.
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, note to carry in time, otherwise Ken can cause overflow, but this will increase the complexity of the algorithm,
If you are sure that no overflow will occur, you can change the inside while to if
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, except the result is C, the remainder is F
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
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, except the result is C, the remainder is F
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 per
F.num[0] = A.num[i];
And then the remainder plus the next one
Replacing division with subtraction
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
{
int i;
for (i = a.len-1 i >= 0; i--)
printf ("%d", a.num[i]);
Puts ("");
}
Converting a string to a large number exists in the BIGNUM structure
Bignum Tonum (char *s)
{
int I, J;
Bignum A;
A.len = strlen (s);
for (i = 0, j = a.len-1 S[i]!= ' "; 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]!= ' n '; 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.