|
-
Nov 11th, 2002, 09:19 AM
#1
Thread Starter
New Member
Adding "VERY" long integers
Hey,
We have an asignment to create a calculator which can add and subtract un-signed integers, we have two variables containing validated integers up to 60 digits long.
How can we make VB6 add these together without createing an exponential answer???
We've only been working with Vb for a couple of moths... and god we're stuck with this 1!!!
-
Nov 11th, 2002, 10:17 AM
#2
Fanatic Member
I presume you are holding these large numbers as strings. If this is the case, and you only want to do adding then you could try using the long hand method you probably learnt at school.
ie:
Starting at the right most digit add the two together, store any overflow in a variable and move left one digit. Add these two together plus any overflow and keep doing this untill you rech the left most digit.
eg:
Num1 = "123456"
Num2 = "789123"
Step 1: 6+3 = 9
Step 2: 5+2 = 7
Step 3: 4+1 = 5
Step 4: 3+9 = 1 with 1 overflow
Step 5: 2+8 +1 = 1 with 1 overflow
Step 6: 1+7 + 1 = 9
so final result = 911579.
Subtracting can be done in a similar manner.
Does this help?
Martin J Wallace (Slaine)
-
Nov 11th, 2002, 10:29 AM
#3
Hyperactive Member
Slaine's method in code would look something like:
VB Code:
Function Add(Str1 As String, Str2 As String) As String
'Simulates working out an addition problem on paper:
' 1 1
' 91028
' 98191
'+ ______
' 189219
'Reverse the strings, so that we don't have to do more calculations
'revsing mid$()
Str1 = StrReverse(Str1)
Str2 = StrReverse(Str2)
Dim i As Double
Dim TmpHold(1) As Double
Dim CarryOver As Double
Dim Total As String
Dim miniTotal As Double
For i = 1 To IIf(Len(Str1) > Len(Str2), Len(Str1), Len(Str2))
If Len(Str1) >= i Then
TmpHold(0) = Mid$(Str1, i, 1)
Else
TmpHold(0) = 0
End If
If Len(Str2) >= i Then
TmpHold(1) = Mid$(Str2, i, 1)
Else
TmpHold(1) = 0
End If
TmpHold(0) = TmpHold(0) + CarryOver
miniTotal = TmpHold(0) + TmpHold(1)
If miniTotal >= 10 Then
CarryOver = 1
Else
CarryOver = 0
End If
If i = IIf(Len(Str1) > Len(Str2), Len(Str1), Len(Str2)) Then
Total = Total & StrReverse(miniTotal)
Else
Total = Total & Right$(miniTotal, 1)
End If
Next
Add = StrReverse(Total) 'Function gives the answer in reverse form, revert.
End Function
Last edited by Hampster; Nov 11th, 2002 at 11:07 AM.
-
Nov 11th, 2002, 01:18 PM
#4
Thread Starter
New Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|