' Multiplicar números usando cadenas de caracteres
' Realizar operaciones con números enteros de cualquier longitud
' Operaciones soportadas:
' Multiplicación, Potencias, Suma y Resta
'
' '------------------------------------------------------------------------------
Option Explicit On
Option Strict On
Imports vb = Microsoft.VisualBasic
Imports System
Module MultStr
Dim ad() As String
'
Sub Main(ByVal args() As String)
'
'Console.WriteLine("125 ^ 9 = {0}", 125 ^ 9)
'Console.WriteLine()
'
Dim num1 As String = "123"
Dim num2 As String = "123"
Dim op As String = "*"
If args.Length > 1 Then
num1 = args(0)
num2 = args(1)
End If
If num2.Length > num1.Length Then
swap(num1, num2)
End If
'
If args.Length = 3 Then
op = args(2)
End If
'
Select Case op
Case "+"
Console.WriteLine(Suma2(num1, num2))
Case "-"
Console.WriteLine(Resta2(num1, num2))
Case "**"
Console.WriteLine(PotN(num1, num2))
Case "*", "x"
Console.WriteLine(Mult2(num1, num2))
'
' Para mostrar todas las cifras que intervienen
' en las operaciones, comenta esto si sólo quieres el resultado
Dim res As String = Mult2(num1, num2)
Dim k As Integer = res.Length
Console.WriteLine(" {0," & k.ToString & "}", num1)
Console.WriteLine("x {0," & k.ToString & "}", num2)
Console.WriteLine(New String("-"c, k + 2))
For i As Integer = 0 To ad.Length - 1
Console.WriteLine(" {0," & k.ToString & "}", ad(i))
Next
Console.WriteLine(New String("-"c, k + 2))
Console.WriteLine(" {0," & k.ToString & "}", res)
End Select
End Sub
'
'
Public Function PotN(ByVal num1 As String, ByVal elevado As String) As String
Dim i As Integer = CInt(elevado) - 1
Dim p(i) As String
For k As Integer = 0 To i
p(k) = num1
Next
Return Mult2(p)
End Function
'
Public Function Mult2(ByVal ParamArray nums() As String) As String
Dim total As String = nums(0)
For i As Integer = 1 To nums.Length - 1
Dim n1 As String = nums(i)
total = Mult2(total, n1)
Next
'
Return total
End Function
'
Public Function Mult2(ByVal num1 As String, ByVal num2 As String) As String
'
' Poner el más largo como primer número
If num2.Length > num1.Length Then
swap(num1, num2)
End If
'
' El número de operaciones necesarias
' será la cantidad de cifras del más pequeño
ReDim ad(num2.Length - 1)
Dim n As Integer = -1
Dim resto As String = "0"
Dim res As String = ""
'
' Multiplicar formando filas con los resultados (al estilo manual)
For i As Integer = num2.Length - 1 To 0 Step -1
n += 1
ad(n) = ""
' Añadir espacios a la derecha según la cifra (fila) que se procese
For k As Integer = 1 To n
ad(n) &= " "
Next
Dim c1 As String = num2.Substring(i, 1)
' Para simplificar las cosas
' se comprueba si se multiplicará por ceo o por uno
' de forma que no sea necesario hacer estas operaciones
If c1 = "0" Then
ad(n) = New String("0"c, num1.Length) & ad(n)
ElseIf c1 = "1" Then
ad(n) = num1 & ad(n)
Else
For j As Integer = num1.Length - 1 To 0 Step -1
Dim c2 As String = num1.Substring(j, 1)
res = (CInt("0" & c1) * CInt("0" & c2) + CInt(resto)).ToString
ad(n) = vb.Right(res, 1) & ad(n)
If res.Length - 1 < 1 Then
resto = "0"
Else
resto = res.Substring(0, res.Length - 1)
End If
Next
If resto <> "0" Then
ad(n) = resto & ad(n)
resto = "0"
End If
End If
Next
'
Return sumarFilas()
End Function
'
Public Function Suma2(ByVal ParamArray nums() As String) As String
'
ReDim ad(nums.Length - 1)
For i As Integer = 0 To nums.Length - 1
ad(i) = nums(i)
Next
'
Return sumarFilas()
End Function
'
Public Function Suma2(ByVal num1 As String, ByVal num2 As String) As String
ReDim ad(1)
ad(0) = num1
ad(1) = num2
'
Return sumarFilas()
End Function
'
Public Function Resta2(ByVal ParamArray nums() As String) As String
Dim total As String = nums(0)
For i As Integer = 1 To nums.Length - 1
Dim n1 As String = nums(i)
total = Resta2(total, n1)
Next
'
Return total
End Function
'
Public Function Resta2(ByVal num1 As String, ByVal num2 As String) As String
ReDim ad(1)
ad(0) = num1
ad(1) = num2
'
Return restar2Filas()
End Function
'
Private Function sumarFilas() As String
Dim m As Integer = 0
Dim n As Integer = ad.Length - 1
'
' m será el número de mayor longitud
For k As Integer = 0 To n
If ad(k).Length > m Then m = ad(k).Length
Next
'
' sumar las filas obtenidas
Dim resto As String = "0"
Dim total As String = ""
Dim res As String = ""
'
For k As Integer = 0 To n
ad(k) = vb.Right(New String(" "c, m) & ad(k), m)
Next
For k As Integer = m - 1 To 0 Step -1
res = resto
For i As Integer = 0 To n
res = (CInt(res) + CInt("0" & ad(i).Substring(k, 1))).ToString
Next
total = vb.Right(res, 1) & total
If res.Length - 1 < 1 Then
resto = "0"
Else
resto = res.Substring(0, res.Length - 1)
End If
Next
If resto <> "0" Then
total = resto & total
End If
'
Return total
End Function
'
Private Function restar2Filas() As String
Dim m As Integer = 0
Dim n As Integer = ad.Length - 1
'
For k As Integer = 0 To n
If ad(k).Length > m Then m = ad(k).Length
Next
'
' restar las filas obtenidas
Dim resto As String = "0"
Dim total As String = ""
Dim res As String = ""
'
For k As Integer = 0 To n
ad(k) = vb.Right(New String(" "c, m) & ad(k), m)
Next
For k As Integer = m - 1 To 0 Step -1
res = (CInt("0" & ad(0).Substring(k, 1)) - (CInt("0" & ad(1).Substring(k, 1)) + CInt(resto))).ToString
If CInt(res) < 0 Then
res = (CInt(res) + 10).ToString
End If
total = vb.Right(res, 1) & total
If res.Length - 1 < 1 Then
resto = "0"
Else
resto = res.Substring(0, res.Length - 1)
End If
Next
If resto <> "0" Then
total = resto & total
End If
'
Return total
End Function
'
Private Sub swap(ByRef n1 As String, ByRef n2 As String)
Dim t As String = n1
n1 = n2
n2 = t
End Sub
End Module
