You are on page 1of 2

CODIGO PARA VISUAL BASIC (EXCEL)

Dim cadena2 As String


Dim ch As String
Dim cadena3 As String
Dim ch2 As String
Dim i As Integer
Dim vector(10) As Integer
Dim vector2(10) As Integer
Dim vector2md(10) As Integer
Dim result(20) As Integer
Dim j As Integer
Dim dim1 As Integer
Dim dim2 As Integer
Worksheets("Hoja1").Cells(1, 1).Value = ("Calculadora Numeros Binarios")
Worksheets("Hoja1").Cells(2, 1).Value = ("Numero 1")
Worksheets("Hoja1").Cells(3, 1).Value = ("Numero 2")
Worksheets("Hoja1").Cells(4, 1).Value = ("Resultado Suma")
Worksheets("Hoja1").Cells(5, 1).Value = ("Resultado Resta")
MsgBox ("SUMA Y RESTA DE NUMEROS BINARIOS")
cadena2 = InputBox("Ingrese el numero:")
For i = 1 To Len(cadena2)
ch = Mid(cadena2, i, 1)
vector(i) = Val(ch)
Next i
dim1 = i
cadena3 = InputBox("Ingrese el numero:")
For i = 1 To Len(cadena3)
ch = Mid(cadena3, i, 1)
vector2(i) = Val(ch)
Next i
dim2 = i
j = 0
For i = 0 To dim1 - 1
If (i < (dim1 - dim2)) Then
vector2md(i) = 0
Else
vector2md(i) = vector2(j)
j = j + 1
End If
Next
For i = 1 To Len(cadena2)
Worksheets("Hoja1").Cells(2, i + 1).Value = ("" & vector(i))
Next i
For i = 1 To Len(cadena2)
Worksheets("Hoja1").Cells(3, i + 1).Value = ("" & vector2md(i))
Next i
Dim acarreo As Integer
acarreo = 0
For i = dim1 - 1 To o Step -1
If (acarreo = 1) Then
vector(i) = vector(i) + acarreo
If (vector(i) = 2) Then
acarreo = 1
vector(i) = 0
End If
End If
result(i + 1) = vector(i) + vector2md(i)
If (result(i + 1) = 2) Then
acarreo = 1

result(i + 1) = 0
Else
acarreo = 0
End If
Next
If (acarreo = 1) Then
result(0) = 1
Else
result(0) = 0
End If
For i = 1 To dim1
Worksheets("Hoja1").Cells(4, i + 1).Value = ("" & result(i))
Next i
Dim acarreo2 As Integer
Dim result2(20) As Integer
acarreo2 = 0
For i = dim1 - 1 To o Step -1
If (acarreo2 = 1) Then
vector(i) = vector(i) - acarreo2
If (vector(i) = 0) Then
vector(i) = 0
Else
If (vector(i) <> 0) Then
vector(i) = 1
End If
End If
End If
result2(i + 1) = vector(i) - vector2md(i)
If (result2(i + 1) = -1) Then
acarreo2 = 1
result2(i + 1) = 1
Else
If (result2(i + 1) = 1) Then
acarreo2 = 0
result2(i) = 1
Else
If (result2(i + 1) = 0) Then
acarreo2 = 0
result2(i) = 0
End If
End If
End If
Next
If (acarreo2 = 1) Then
result2(0) = 0
Else
result(0) = 0
End If
For i = 1 To dim1
Worksheets("Hoja1").Cells(5, i + 1).Value = ("" & result2(i))
Next i