Dim total As Double
Dim number As Double
Dim secondNumber As Double
Dim more As String
Dim moreNumbers As String
Dim operation As String
total = 0
more = "y"
Open "calculadora.txt" For Output As #1 Rem "Abro archivo nuevo calculadora.txt"
Rem Open calculofile$ For Append As #1 Rem "Abro archivo calculadora.txt para anadir"
Cls
Screen 12
While more = "y"
Color 6
GoSub rectangulo
GoSub operadores
Color 7
Locate 3, 21: Input "N1"; num#
Cls
total = round(num#)
Print #1, " ";
a = total
GoSub formatF
While operation$ <> "t"
Color 6
Locate 1, 5: Print " "; Chr$(6); " Calculadora "; Chr$(6)
GoSub rectangulo
Color 14
Locate 3, 23: GoSub formato
GoSub operadores
Color 7
Locate 4, 22: Input ""; operation$
operation$ = LCase$(operation$)
If operation$ = "+" Then
Rem sumar
Color 7
Locate 5, 20: Input "N2"; secondNumber#
num = secondNumber#
secondNumber = round(num)
Print #1, "+ ";
a = secondNumber#
GoSub formatF
total = total + secondNumber
num = total#
total = round(num)
Color 14
a = total
Locate 4, 40: GoSub formato
Print #1, " ----"
a = total
Print #1, " # ";
GoSub formatF
Print #1, ""
Color 7
Else
If operation$ = "-" Then
Rem Resta
Color 7
Locate 5, 20: Input "N2"; secondNumber#
num = secondNumber#
secondNumber = round(num)
Print #1, "- ";
a = secondNumber#
GoSub formatF
total = total - secondNumber
num = total#
total = round(num)
Color 14
a = total
Locate 4, 40: GoSub formato
Print #1, " ----"
a = total
Print #1, " # ";
GoSub formatF
Print #1, ""
Color 7
Else
If operation$ = "*" Then
Rem multiplicacion
Color 7
Locate 5, 20: Input "N2"; secondNumber#
num = secondNumber#
secondNumber = round(num)
Print #1, "* ";
a = secondNumber#
GoSub formatF
total# = secondNumber * total
num = total#
If num > 10000000 Then total = 0
If num < 10000000 Then total = round(num)
If total = 0 Then Locate 4, 40: Print "limite"
Rem * multiplicacion
Color 14
a = total
Locate 4, 40: GoSub formato
If total = 0 Then Locate 4, 40: Print "limite Exc"
Print #1, " ----"
a = total
Print #1, " # ";
GoSub formatF
Print #1, ""
Color 7
Else
If operation$ = "/" Then
Rem division
Color 7
Locate 5, 20: Input "N2"; secondNumber#
num = secondNumber#
secondNumber = round(num)
Print #1, "/ ";
a = secondNumber#
GoSub formatF
If secondNumber = 0 Then
Color 4
Locate 7, 5: Print "No se puede dividir por 0"
Color 7
Else
total# = total / secondNumber
num = total#
total = round(num)
Rem / division
End If
a = total
Color 14
If secondNumber = 0 Then Locate 4, 40: Print " "; 0
If secondNumber > 0 Then Locate 4, 40: GoSub formato
Print #1, " ----"
a = total
Print #1, " # ";
GoSub formatF
Print #1, ""
Color 7
Else
5
If operation$ <> "t" Then Locate 7, 5: Print "debes seleccionar una operacion"
End If
End If
End If
End If
Sleep 1
Cls
Wend
Color 6
GoSub rectangulo
GoSub operadores
Color 12
Locate 3, 40: Print "Total= "
Locate 4, 40: GoSub formato
Print #1, " -----------"
a = total
Print #1, " Total= ";
GoSub formatF
Print #1, " -----------"
Color 7
Locate 7, 6: Input "Nueva operacion (y - n)"; more
Cls
operation$ = ""
moreNumbers = ""
total = 0
Rem Si no resteamos el total a 0
Rem seguiremos sumadolo
Wend
Close #1
End
operadores:
Color 12
Locate 1, 5: Print " "; Chr$(6); " Calculadora "; Chr$(6)
Color 7
Locate 4, 6: Print "(T)"
Color 12
Locate 4, 9: Print "(+)"
Locate 4, 12: Print "(-)"
Locate 4, 15: Print "(*)"
Locate 4, 18: Print "(/)"
Return
rectangulo:
Rem dibuja rectangulo
p1 = 20
p2 = 30
p3 = 90
p4 = 300
Line (p2, p1)-(p4, p1), 7
Line (p2, p3)-(p4, p3), 7
Line (p2, p1)-(p2, p3), 7
Line (p4, p1)-(p4, p3), 7
p1 = 20
p2 = 300
p3 = 90
p4 = 430
Line (p2, p1)-(p4, p1), 7
Line (p2, p3)-(p4, p3), 7
Line (p2, p1)-(p2, p3), 7
Line (p4, p1)-(p4, p3), 7
Return
formato: Rem subrutina que implementa los separadores de miles
If a <= 9999 Then GoTo fin1
If a <= 99999 Then GoTo fin2
If a <= 999999 Then GoTo fin3
If a <= 9999999 Then GoTo fin4
If a <= 99999999 Then GoTo fin5
fin1:
Print Using "#,###.##"; a
Return
fin2:
Print Using "##,###.##"; a
Return
fin3:
Print Using "###,###.##"; a
Return
fin4:
Print Using "#,###,###.##"; a
Return
fin5:
Print Using "##,###,###.##"; a
Return
formatF: Rem subrutina que implementa los separadores de miles en un archivo
If a <= 999 Then GoTo fn0
If a <= 9999 Then GoTo fn1
If a <= 99999 Then GoTo fn2
If a <= 999999 Then GoTo fn3
If a <= 9999999 Then GoTo fn4
If a <= 99999999 Then GoTo fn5
fn0:
Print #1, Using "###.##"; a
Return
fn1:
Print #1, Using "#,###.##"; a
Return
fn2:
Print #1, Using "##,###.##"; a
Return
fn3:
Print #1, Using "###,###.##"; a
Return
fn4:
Print #1, Using "#,###,###.##"; a
Return
fn5:
Print #1, Using "##,###,###.##"; a
Return
Function round# (num As Double) Rem funcion que redondea a 2 los decimales
'WARNING: USE "#" at the end of constant values,
'or else you will get rounding errors:
' "num = .45" >> "num = .449999988079071
' "num = .45#" >> "num = .45"
dp = 2
Dim exp1 As Long, num2 As Long
exp1 = 10 ^ dp: num2 = num * exp1: round# = num2 / exp1
End Function
La función round redondea a dos los decimales de un número. (dp=2)