加法:
#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;
}
减法:
#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 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; }