Addition:
#include <stdio.h>
#include <string.h>
int a[1000]={0},b[1000]={0};
int main ()
{
Char *s1;
int l1=0,l2=0,l,i;
Freopen ("Hp+.in", "R", stdin);
Freopen ("Hp+.out", "w", stdout);
scanf ("%s", S1);
L1=strlen (S1);
for (i=0;i<=l1-1;i++)
{
a[l1-1-i]=s1[i]-' 0 ';
}
scanf ("%s", S1);
L2=strlen (S1);
for (i=0;i<=l2-1;i++)
{
b[l2-1-i]=s1[i]-' 0 ';
}
if (L1>L2)
L=L1;
Else
L=L2;
for (i=0;i<=l-1;i++)
{
A[i]=a[i]+b[i];
if (a[i]>=10)
{
a[i+1]=a[i+1]+1;
a[i]=a[i]-10;
}
}
if (a[l]!=0)
l++;
for (i=l-1;i>=0;i--)
printf ("%d", a[i]);
return 0;
}
Subtraction:
#include <stdio.h> #include <string.h> int a[1000]={0},b[1000]={0},c[1000]={0}; int main () {char *s1; int l1=0,l2=0,l,i,x; Freopen ("Hp-.in", "R", stdin); Freopen ("Hp-.out", "w", stdout); scanf ("%s", S1); L1=strlen (S1); for (i=0;i<=l1-1;i++) {a[l1-1-i]=s1[i]-' 0 '; } scanf ("%s", S1); L2=strlen (S1); for (i=0;i<=l2-1;i++) {b[l2-1-i]=s1[i]-' 0 '; } if (L1>L2) l=l1; else L=l2; for (i=0;i<=l-1;i++) {
if (A[i]<b[i]) { c[i]=a[i]+10-b[i]; c[i+1]=a[i+1 ]-1; } else c[i]=a[i]-b[i]; } for (i=l-1;i>=0;i++) { if (A[i] <b[i]) { x=1; } else & nbsp; break; } if (x==1) { printf ("-"); for (i=0;i<=l-1;i++) {
if (B[i]<a[i]) {c[i]=b[i]+10-a[i]; C[i+1]=b[i+1]-1; } else c[i]=b[i]-a[i]; }} for (i=l-1;i>=0;i--) {if (c[i]==0) l--; else break; } for (i=l-1;i>=0;i--) printf ("%d", c[i]); return 0; }
High-precision addition and subtraction. Addition is OK, subtraction took a lot of time.