求vb大神帮忙做一道题。要求;只需要要程序写出来,界面的截图;

超长正整数的加法,请设计一个算法完成两个超长正整数的加法。... 超长正整数的加法,
请设计一个算法完成两个超长正整数的加法。
展开
 我来答
syx54
2013-05-14 · TA获得超过7378个赞
知道大有可为答主
回答量:6567
采纳率:83%
帮助的人:2712万
展开全部

基本思路,是把数字作为字符串处理,然后运算的时候,先把它截取成长度一定的(比如4位)若干段,分段转换成数字求和(要考虑进位),然后转换成字符,在连接起来输出:

界面如下:


代码如下:


Option Explicit


Private Sub Command1_Click()

Dim ssss As String

ssss = PPP(Text1.Text, Text2.Text)

Text3.Text = ssss


End Sub


'自定义函数求两个长整数之和

Private Function PPP(ByVal s1 As String, ByVal s2 As String) As String

Dim dd1() As String

Dim n1 As Integer

Dim dd2() As String

Dim n2 As Integer

Dim s As String

Dim i As Integer

Dim d1 As Integer

Dim d2 As Integer

Dim t As Integer

Dim j As Integer

Dim JinWei As Boolean

Dim K As Integer

Dim SS1 As String

Dim SS2 As String


If Len(s1) > Len(s2) Then

   SS1 = s1

   SS2 = s2

Else

   SS1 = s2

   SS2 = s1

End If


n1 = 0

s = SS1

Do While Len(s) > 4

   n1 = n1 + 1

   ReDim Preserve dd1(1 To n1) As String

   dd1(n1) = Right(s, 4)

   s = Left(s, Len(s) - 4)

Loop

n1 = n1 + 1

ReDim Preserve dd1(1 To n1) As String

dd1(n1) = s


n2 = 0

s = SS2

Do While Len(s) > 4

   n2 = n2 + 1

   ReDim Preserve dd2(1 To n2) As String

   dd2(n2) = Right(s, 4)

   s = Left(s, Len(s) - 4)

Loop

n2 = n2 + 1

ReDim Preserve dd2(1 To n2) As String

dd2(n2) = s


JinWei = False

K = 0

For i = 1 To n1

   K = K + 1

   If K > n2 Then Exit For

   If Not JinWei Then

      t = Val(dd1(i)) + Val(dd2(K))

   Else

      t = Val(dd1(i)) + Val(dd2(K)) + 1

   End If

   If Len(CStr(t)) > 4 Then

      JinWei = True

   Else

      JinWei = False

   End If

   dd1(i) = Right(CStr(t), 4)

Next i

If JinWei Then

   For i = n2 + 1 To n1

      If JinWei Then

         t = Val(dd1(i)) + 1

      Else

         t = Val(dd1(i))

      End If

      If Len(CStr(t)) > 4 Then

         JinWei = True

      Else

         JinWei = False

      End If

      dd1(i) = Right(CStr(t), 4)

   Next i

End If


PPP = ""

For i = n1 To 1 Step -1

   PPP = PPP & dd1(i)

Next i

If JinWei Then PPP = "1" & PPP

End Function

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式