有点问题,因为你没有给n赋初值,所以n的值不确定,也就是说可能为1,那该if语句就失效了。
另外,其实根本就没必要事先比较s1和s2的长度,模仿递增向量的合并方法,先计算长度相同的部分,之后计算剩余部分。
我用c++的string字符串写了一个,你参考下。
#include<iostream>
#include<string>
using
namespace
std
void
Reverse(string
&
str)
void
AddInt(string
&
c,
string
a,
string
b)
int
main()
{
string
a,
b,
c
cin
>>
a
>>
b
AddInt(c,
a,
b)
cout
<<
a
<<
"
+
"
cout
<<
b
<<
"
=
"
<<
endl
cout
<<
c
<<
endl
system("pause")
return
0
}
void
Reverse(string
&
str)
{
int
left,
right
left
=
0
right
=
str.size()-1
while
(left
<
right)
{
char
ch
=
str[left]
str[left]
=
str[right]
str[right]
=
ch
left++
right--
}
}
void
AddInt(string
&
c,
string
a,
string
b)//模仿递增向量的合并方法
{
c.resize(0)
Reverse(a)
Reverse(b)
//逆序计算a+b,则从低位开始计算
int
i,
carry
i
=
carry
=
0
while
(i
<
a.size()
&&
i
<
b.size())
{
c
+=
(a[i]-'0'
+
b[i]-'0'
+
carry)
%
10
+
'0'
carry
=
(a[i]-'0'
+
b[i]-'0'
+
carry)
/
10
i++
}
while
(i
<
a.size())
{
c
+=
(a[i]-'0'
+
carry)
%
10
+
'0'
carry
=
(a[i]-'0'
+
carry)
/
10
i++
}
while
(i
<
b.size())
{
c
+=
(b[i]-'0'
+
carry)
%
10
+
'0'
carry
=
(b[i]-'0'
+
carry)
/
10
i++
}
while
(carry
>
0)//计算进位部分
{
c
+=
carry
%
10
+
'0'
carry
/=
10
}
i
=
c.size()
-
1
while
(c[i]
==
'0')//消除多余的高位0
{
i--
}
c
=
c.substr(0,
i+1)
Reverse(c)
}
。。#include <stdio.h>
main()
{ int a[240]={0},b[240]={0},c[241]={0}
int i,ka,kb,k
char a1[240],b1[240]
gets(a1)ka=strlen(a1)
gets(b1)kb=strlen(b1)
if(ka>=kb) k=ka
else k=kb
for(i=0i<kai++) a[i]=a1[ka-i-1]-'0'
for(i=0i<kbi++) b[i]=b1[kb-i-1]-'0'
for(i=0i<ki++)
{c[i]=a[i]+b[i]+c[i]<br>c[i+1]=c[i+1]+c[i]/10<br>c[i]=c[i]%10<br> }
if(c[k]) k++
for(i=k-1i>=0i--) printf("%d",c[i])
system("pause")
}
高精度减法。。
#include <stdio.h>
main()
{ int a[240]={0},b[240]={0}
int i,la,lb
char a1[240],b1[240]
gets(a1)la=strlen(a1)
gets(b1)lb=strlen(b1)
for(i=0i<lai++) a[i]=a1[la-i-1]-'0'
for(i=0i<lbi++) b[i]=b1[lb-i-1]-'0'
if(la>lb)
{for(i=0i<lai++)<br> {if(a[i]<b[i])<br> {a[i+1]--<br> a[i]+=10<br> }
a[i]=a[i]-b[i]
}
while(!a[la-1])
{la--<br>if(!la) {printf("0")break}
}
for(i=la-1i>=0i--) printf("%d",a[i])
}
else
{for(i=0i<lbi++)<br> {if(b[i]<a[i])<br> {b[i+1]--<br> b[i]+=10<br> }
b[i]=b[i]-a[i]
}
while(!b[lb-1])
{lb--<br>if(!lb) {printf("0")break}
}
printf("-")
for(i=lb-1i>=0i--) printf("%d",b[i])
}
system("pause")
}
高精度乘法:
#include<stdio.h>
#include<string.h>
int a[2505],b[2505],x[5010]={0}
main()
{ int i,j,k,lena,lenb,t
char temp[10010]
scanf("%s",temp)
lena=strlen(temp)
if(lena%4)
{ t=4-lena%4
for(i=t+lena-1i>=ti--) temp[i]=temp[i-t]
temp[t+lena]='\0'
lena+=t
for(i=0i<ti++) temp[i]='0'
}
for(i=0,j=1i<lenai+=4,j++)
a[j]=(temp[i]-'0')*1000+(temp[i+1]-'0')*100+(temp[i+2]-'0')*10+temp[i+3]-'0'
lena=j
scanf("%s",temp)
lenb=strlen(temp)
if(lenb%4)
{
t=4-lenb%4
for(i=t+lenb-1i>=ti--) temp[i]=temp[i-t]
temp[t+lenb]='\0'
lenb+=t
for(i=0i<ti++) temp[i]='0'
}
for(i=0,j=1i<lenbi+=4,j++)
b[j]=(temp[i]-'0')*1000+(temp[i+1]-'0')*100+(temp[i+2]-'0')*10+temp[i+3]-'0'
lenb=j
for(i=lena-1i>0i--)
for(j=lenb-1j>0j--)
{
t=i+j
x[t]+=a[i]*b[j]
x[t-1]+=x[i+j]/10000
x[t]=x[t]%10000
}
while(!x[i]) i++
printf("%d",x[i])
i++ for(i<lena+lenb-1i++)
if(x[i]>1000) printf("%d",x[i])
else
if(x[i]>100) printf("0%d",x[i])
else
if(x[i]>10) printf("00%d",x[i])
else printf("000%d",x[i])
system("pause")
}
。。