Integer high precision operation--addition

Source: Internet
Author: User

High-precision operation is an important algorithm of information science. This algorithm is calculated using multiple storage units, so it is more computationally scoped than a typical storage unit. It is also a frequently-tested topic in some informatics competitions.

The high-precision operation mainly has the following several steps:

1, read the string, converted to a number in reverse chronological storage into an array of integers;

2, arithmetic, pay attention to carry and borrow;

3, the reverse output integer array, the addition of attention to the highest bit carry, subtraction note high in the useless 0 do not output;

High-precision Addition code:

#include <stdio.h>
#include <string.h>
Char s[1000]; When the array is large, it should be defined as a global variable.
int a[1000]={0},b[1000]={0};
int main ()
{
int l1,l2,l,i;
scanf ("%s", s); Read the first integer
L1=strlen (s); To find the length of an integer (string)
for (i=0;i<=l1-1;i++)//To store an integer in reverse in the integer array a
{
a[l1-1-i]=s[i]-' 0 '; S[i] is a character type that gets an integer type by subtracting 0 of the ASCII code, such as: Converts the character 1 to the number 1
}
scanf ("%s", s); Read the second integer
L2=strlen (s);
for (i=0;i<=l2-1;i++)
{
b[l2-1-i]=s[i]-' 0 ';
}
if (L1>L2)
L=L1;
Else
L=L2;
for (i=0;i<=l-1;i++)//addition operation
{
A[i]=a[i]+b[i];
if (a[i]>=10)//greater than 10 go to a high
{
a[i+1]=a[i+1]+1;
a[i]=a[i]-10;
}
}
if (a[l]!=0)//Determine if the highest bit has a rounding
l++;
for (i=l-1;i>=0;i--)//output
printf ("%d", a[i]);
}

High-precision Subtraction code:

#include <stdio.h>
#include <string.h>
Char s[1000];
int a[1000]={0},b[1000]={0},c[1000]={0};
int main ()
{
int i,j,z,l1,l2,l,o;
Freopen ("Hp-.in", "R", stdin);
Freopen ("Hp-.out", "w", stdout);
scanf ("%s", s); A
L1=strlen (s);
for (i=0;i<=l1-1;i++)//convert S (meiosis) to numbers and in reverse to a array
a[l1-1-i]=s[i]-' 0 ';
scanf ("%s", s);
L2=strlen (s);
for (i=0;i<=l2-1;i++)//convert S (meiosis) to numbers, and in reverse to B array
b[l2-1-i]=s[i]-' 0 ';

if (L1==L2) {
for (j=l1;j>=0;j--)
{
if (A[j]>b[j])//a Large
{
o=0;
Break
}
if (B[j]>a[j])//b Large
{
O=1;
Break
}
}
}
if (l1>l2| | o==0)//a Large, A-b
{
L=L1;
for (i=0;i<l;i++)
{
if (A[i]<b[i])
{
a[i]=a[i]+10;
A[i+1]=a[i+1]-1;
}
C[i]=a[i]-b[i];
}
while (l>1&&c[l-1]==0) l--; To give up the useless 0 in the high
for (i=l-1;i>=0;i--)//output
{
printf ("%d", c[i]);
}
}
Else
{
printf ("-");
L=L2;
for (i=0;i<l;i++)
{
if (A[i]>b[i])
{
b[i]=b[i]+10;
B[i+1]-=1;
}
C[i]=b[i]-a[i];
}
while (l>1&&c[l-1]==0) l--; To give up the useless 0 in the high
for (i=l-1;i>=0;i--)//output
{
printf ("%d", c[i]);
}
}

return 0;
}

Integer high precision operation--addition

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.