求vb大神帮忙做一道题.要求;只需要要程序写出来,界面的截图;超长正整数的加法,请设计一个算法完成两个超长正整数的加法.
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/19 01:37:48
求vb大神帮忙做一道题.要求;只需要要程序写出来,界面的截图;超长正整数的加法,请设计一个算法完成两个超长正整数的加法.
求vb大神帮忙做一道题.要求;只需要要程序写出来,界面的截图;
超长正整数的加法,
请设计一个算法完成两个超长正整数的加法.
求vb大神帮忙做一道题.要求;只需要要程序写出来,界面的截图;超长正整数的加法,请设计一个算法完成两个超长正整数的加法.
基本思路,是把数字作为字符串处理,然后运算的时候,先把它截取成长度一定的(比如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