Tengo que hacer una calculadora como trabajo, he buscado sobre el tema i al
intentar hacerla no me ha funcionado. El problema esta en la tecla =. Al usarla el
resultado me pone lo que he escrito en la primera caja de texto. Creo que el
problema esta muy claro. Pero hay alguna manera para que funcione sin que se
hayan de canviar muchas cosas?
Código:
Private Sub Command1_Click()
Text1 = Text1 & "*"
End Sub
Private Sub Command10_Click()
Text1 = Text1 & "4"
End Sub
Private Sub Command11_Click()
Text1 = Text1 & "5"
End Sub
Private Sub Command12_Click()
Text1 = Text1 & "6"
End Sub
Private Sub Command13_Click()
Text1 = Text1 & "7"
End Sub
Private Sub Command14_Click()
Text1 = Text1 & "8"
End Sub
Private Sub Command15_Click()
Text1 = Text1 & "9"
End Sub
Private Sub Command16_Click()
Text1 = Text1 & "."
End Sub
Private Sub Command17_Click()
Text1 = Text1 & "0"
End Sub
Private Sub Command18_Click()
Text3 = Text1
End Sub
Private Sub Command2_Click(Index As Integer)
Text1 = Text1 & "+"
End Sub
Private Sub Command3_Click()
Text1 = Text1 & "-"
End Sub
Private Sub Command4_Click()
Text1 = Text1 & "/"
End Sub
Private Sub Command5_Click()
Text1 = Text1 & "1"
End Sub
Private Sub Command6_Click()
Text1 = Text1 & "2"
End Sub
Private Sub Command7_Click()
Text1 = Text1 & "3"
End Sub
no entendi muy bien :xD solo veo que concatenas cadenas xD
o bien si creo lo que es ahi solo unis cadenas :P
pero mas que todo las operaciones sencillas las podes hacer asi
TextResultado = Cint(Text1) OPERADOR Cint(Text2)
Gracias, pero este no esto no me sirve, ya que no quiero poner dos cajas de texto. solo una i el resultado.
Si lo haces de esa manera tendras muchos problemas... es decir, que tendras que crear un Parse matematico...
Dulces Lunas!¡.
Olle tengo una calculadora pero con 3 text si la quieres te paso el programa con el source :)
Saludos
No entendí bien la pregunta, pero si te refieres que al darle al botón de igual (=) no te hace el cálculo, efectivamente, como dijo Raul100 solamente concatenas los números y los operadores, y así no llevas a cabo ningún cálculo.
Es decir, si quieres sumar 2 y 5 no debes hacer:
Text1.Text = "2+5" 'Lo escirto entre comillas es un String, y sale tal cual.
Debes hacer lo siguiente:
Text1.Text = 2 + 5 'Si dejamos las comillas a un lado, VB hará el cálculo
Por tanto, si tienes una calculadora con 1 TextBox y 4 CommandButtons (2, 5, +, =) puedes jugar con variables.
Un saludo,
atte. Herio
Editado
Encontré una calculadora muy simple que hice hace algún tiempo, aquí el source (código):
Descargar desde Multiupload: http://www.multiupload.com/BKK03LQ2MM
Descargar desde 4shared: http://www.4shared.com/file/BM3yH1Bm/Source_Calculadora.html
si lo que haces son expresiones tipo
(((A+B)-(X+Y))+H)
y quieres que al termino (al pulsar la tecla "=" ) te de el resultado, entonces tienes 2 formas de hacerlo:
1.- Usar un buffer de resultados ( Ans como en las calculadoras cientificas ).
2.- usar un parse matematico que te lo resuelva... por ejemplo este que no termine pero funciona con expresiones simples...
'
' ////////////////////////////////////////////////////////////////
' // Autor: BlackZeroX ( Ortega Avila Miguel Angel ) //
' // //
' // Web: http://InfrAngeluX.Sytes.Net/ //
' // //
' // |-> Pueden Distribuir Este Código siempre y cuando //
' // no se eliminen los créditos originales de este código //
' // No importando que sea modificado/editado o engrandecido //
' // o achicado, si es en base a este código //
' ////////////////////////////////////////////////////////////////
' //
' ////////////////////////////////////////////////////////////////
' 50x-9+114-32x
' (50-32)x = 9-114
' x = (9-114) / (50-32)
Option Explicit
Enum eOperandos
eParentesisI = 0
eParentesisF = 1
ePotencia = 2
eRaiz = 3
eMultiplicacion = 4
eDivicion = 5
eSuma = 6
eResta = 7
End Enum
Private Operandos(0 To 7) As String
' // Ecuación de 1er grado.
'Public Function EcuacionLineal(ByVal vExpresion$) As String'
'Dim str_Exp$()
' str_expr$() = Split(vExpresion$, "=", 2)'
' If UBound(str_expr$) = 1 Then
' ReDim Preserve str_expr$(0 To 1)
' End If
' // Hubicamos los terminos (Incognitas en el lado izquierdo y las constantes en el derecho)
'End Function
Public Function GetParentesis(ByVal vExpresion$) As String
Dim lng_op&(0 To 1) ' // Posicion Inicial/Final
Dim str_bloq$
Dim boo_res As Boolean
lng_op&(1) = InStr(1, vExpresion$, Operandos(eOperandos.eParentesisF))
If (lng_op&(1) <> 0) Then
lng_op&(0) = InStrRev(vExpresion$, Operandos(eOperandos.eParentesisI), lng_op&(1))
If (lng_op&(0) = 0) Then
lng_op&(0) = 1
Else
lng_op&(0) = lng_op&(0) + 1
End If
GetParentesis = Mid$(vExpresion$, lng_op&(0), lng_op&(1) - lng_op&(0))
Else
GetParentesis = vExpresion$
End If
End Function
' // Terminos Semejantes ( Con incognita ).
Public Function ReduccionDeOperandos(ByVal vExpresion$, Optional ByVal Incognita As String = "x") As String
Dim str_spl$()
Dim lng_val#(0 To 1)
Dim lng_ing&(0 To 1)
Dim str_coll$()
Dim lng_c&
Dim str_res$
Dim lng_Opd&
vExpresion$ = Replace$(vExpresion$, " ", "")
vExpresion$ = Replace$(vExpresion$, ",", ".")
If (Len(vExpresion$)) Then
Do
lng_Opd& = BuscarOperando(vExpresion$)
If (lng_Opd& > -1) Then
str_spl$ = Split(vExpresion$, Operandos(lng_Opd&), 2)
If (lng_Opd& = eOperandos.eRaiz) Then
lng_val#(0) = GetVal(str_spl$(UBound(str_spl$)), &H0, False)
str_res$ = Sqr(lng_val#(0))
vExpresion$ = Replace$(vExpresion$, Operandos(lng_Opd&) & lng_val#(0), str_res$)
ElseIf (lng_Opd& <= eOperandos.eResta) Then
lng_val#(0) = GetVal(str_spl$(0), &H0, True)
lng_val#(1) = GetVal(str_spl$(1), &H0, False)
lng_ing&(0) = InStr(1, str_spl$(0), Incognita, vbTextCompare)
lng_ing&(1) = InStr(1, str_spl$(1), Incognita, vbTextCompare)
Select Case lng_Opd&
Case eOperandos.ePotencia
str_res$ = lng_val#(0) ^ lng_val#(1)
Case eOperandos.eMultiplicacion
str_res$ = lng_val#(0) * lng_val#(1)
Case eOperandos.eDivicion
str_res$ = FormatNumber(lng_val#(0) / lng_val#(1), 9)
Case eOperandos.eSuma
str_res$ = lng_val#(0) + lng_val#(1)
Case eOperandos.eResta
str_res$ = lng_val#(0) - lng_val#(1)
End Select
vExpresion$ = Replace$(vExpresion$, lng_val#(0) & Operandos(lng_Opd&) & lng_val#(1), str_res$)
Else
ReduccionDeOperandos = vExpresion$
Exit Function
End If
End If
Loop Until lng_Opd& = -1
End If
ReduccionDeOperandos = vExpresion$
End Function
Public Function BuscarOperando(ByVal vExpresion$, Optional ByVal Reverse As Boolean = False, Optional ByRef Inpos&) As Long
Dim lng_Opd&
lng_Opd& = -1
If (Len(vExpresion$)) Then
For lng_Opd& = 2 To UBound(Operandos)
If (Reverse) Then
Inpos& = InStrRev(vExpresion$, Operandos(lng_Opd&), Len(vExpresion$))
Else
Inpos& = InStr(1, vExpresion$, Operandos(lng_Opd&))
End If
If (Inpos&) Then
Exit For
End If
Next lng_Opd&
If (lng_Opd& = UBound(Operandos) + 1) Then
BuscarOperando = -1
Else
BuscarOperando = lng_Opd&
End If
End If
End Function
Public Function GetVal(ByVal vExpresion$, ByRef OutPos As Long, Optional ByVal Reverse As Boolean = False) As Double
Dim str_res$
If (Len(vExpresion$)) Then
str_res$ = BuscarOperando(vExpresion$, Reverse, OutPos)
If (Reverse) Then
If (str_res$ = -1) Then
OutPos = 1
End If
GetVal = Val(Mid$(vExpresion$, OutPos))
Else
If (str_res$ = -1) Then
GetVal = Val(Mid$(vExpresion$, 1))
OutPos = Len(vExpresion$)
Else
GetVal = Val(Mid$(vExpresion$, 1, OutPos))
End If
End If
End If
End Function
Private Sub Class_Initialize()
Operandos(eOperandos.eParentesisI) = "("
Operandos(eOperandos.eParentesisF) = ")"
Operandos(eOperandos.ePotencia) = "^"
Operandos(eOperandos.eRaiz) = "sqrt"
Operandos(eOperandos.eMultiplicacion) = "*"
Operandos(eOperandos.eDivicion) = "/"
Operandos(eOperandos.eSuma) = "+"
Operandos(eOperandos.eResta) = "-"
End Sub
Private Sub Form_Load()
Dim cls_ecuLineal As New cls_ecuLineal
With cls_ecuLineal
Dim str$, str2$
str2$ = "((7 + 4 * 5 + 4)) + 54 + (42) * (4 * (8 / (45 * 10)))*sqrt(9)"
Do
DoEvents
str$ = .GetParentesis(str2$)
If Len(str2$) <> Len(str$) Then
str2$ = Replace(str2, "(" & str$ & ")", .ReduccionDeOperandos(str$))
Else
MsgBox .ReduccionDeOperandos(str$) & vbNewLine & ((7 + 4 * 5 + 4)) + 54 + (42) * (4 * (8 / (45 * 10))) * Sqr(9)
Exit Do
End If
Loop
End With
End Sub
Dulces Lunas!¡.
Yo hice una hace poco a ver que te parece:
Public Class Form1
Dim tipo, base As Byte
Dim todo As Boolean
Dim num1, num2, num, total As Double
Dim operador As String
Dim totalh As String
' función que convierte de número Hexadecimal a Decimal
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function HexToDec(ByVal HexStr As String) As Double
Dim mult As Double
Dim DecNum As Double
Dim ch As String
mult = 1
DecNum = 0
Dim i As Integer
For i = Len(HexStr) To 1 Step -1
ch = Mid(HexStr, i, 1)
If (ch >= "0") And (ch <= "9") Then
DecNum = DecNum + (Val(ch) * mult)
Else
If (ch >= "A") And (ch <= "F") Then
DecNum = DecNum + ((Asc(ch) - Asc("A") + 10) * mult)
Else
If (ch >= "a") And (ch <= "f") Then
DecNum = DecNum + ((Asc(ch) - Asc("a") + 10) * mult)
Else
HexToDec = 0
Exit Function
End If
End If
End If
mult = mult * 16
Next i
HexToDec = DecNum
End Function
' función que convierte de número Decimal a Hexadecimal
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function DecToHex(ByVal DecNum As Double) As String
Dim remainder As Integer
Dim HexStr As String
HexStr = ""
Do While DecNum <> 0
remainder = DecNum Mod 16
If remainder <= 9 Then
HexStr = Chr(Asc(remainder)) & HexStr
Else
HexStr = Chr(Asc("A") + remainder - 10) & HexStr
End If
DecNum = DecNum \ 16
Loop
If HexStr = "" Then HexStr = "0"
DecToHex = HexStr
End Function
' función que convierte de número Decimal a Binario
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function DecToBin(ByVal DecNum As Double) As String
Dim BinStr As String
BinStr = ""
Do While DecNum <> 0
If (DecNum Mod 2) = 1 Then
BinStr = "1" & BinStr
Else
BinStr = "0" & BinStr
End If
DecNum = DecNum \ 2
Loop
If BinStr = "" Then BinStr = "0000"
DecToBin = BinStr
End Function
' función que convierte de número Binario a número decimal
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function BinToDec(ByVal BinStr As String) As Double
Dim mult As Double
Dim DecNum As Double
mult = 1
DecNum = 0
Dim i As Integer
For i = Len(BinStr) To 1 Step -1
If Mid(BinStr, i, 1) = "1" Then
DecNum = DecNum + mult
End If
mult = mult * 2
Next i
BinToDec = DecNum
End Function
' función que convierte de número Hexadecimal a número binario
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function HexToBin(ByVal HexStr As String) As String
Dim BinStr As String
BinStr = ""
Dim i As Integer
For i = 1 To Len(HexStr)
BinStr = BinStr & DecToBin(HexToDec(Mid(HexStr, i, 1)))
Next i
HexToBin = BinStr
End Function
' función que convierte de número binario a Hexadecimal
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function BinToHex(ByVal BinStr As String) As String
Dim HexStr As String
HexStr = ""
Dim i As Integer
For i = 1 To Len(BinStr) Step 4
HexStr = HexStr & DecToHex(BinToDec(Mid(BinStr, i, 4)))
Next i
BinToHex = HexStr
End Function
' función que convierte de número octal a decimal
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function octToDec(ByVal octStr As String) As String
Dim digitos, numero, resultado As Double
digitos = Len(num)
For cuenta = 1 To digitos
numero = Mid(num, cuenta, 1)
resultado = resultado + numero * 8 ^ (digitos - cuenta)
Next cuenta
octToDec = resultado
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Height = 281
Me.Width = 256
todo = False
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
Me.Text = "Calculadora"
RadioButton1.Checked = True
'Button25.Enabled = False
'Button24.Enabled = False
'Button23.Enabled = False
'Button22.Enabled = False
'Button21.Enabled = False
base = 10
End Sub
Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click
If todo = False Then
Me.Height = 281
Me.Width = 415
todo = True
Else
Me.Height = 281
Me.Width = 256
todo = False
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click, Button10.Click, Button12.Click, Button25.Click, Button24.Click, Button23.Click, Button22.Click, Button21.Click
TextBox1.Text &= sender.text
End Sub
Private Sub Button27_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button27.Click
TextBox1.Clear()
End Sub
Private Sub Button26_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button26.Click
TextBox1.Clear()
num1 = 0
num2 = 0
total = 0
End Sub
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click, Button14.Click, Button13.Click, Button11.Click, Button19.Click, Button18.Click, Button17.Click, Button16.Click
Select Case sender.text
Case "/"
tipo = 1
num1 = CDbl(TextBox1.Text)
TextBox1.Clear()
Case "x"
tipo = 2
num1 = CDbl(TextBox1.Text)
TextBox1.Clear()
Case "+"
tipo = 3
num1 = CDbl(TextBox1.Text)
TextBox1.Clear()
Case "-"
tipo = 4
num1 = CDbl(TextBox1.Text)
TextBox1.Clear()
Case "1/x"
tipo = 5
num1 = CDbl(TextBox1.Text)
TextBox1.Text = 1 / num1
Case "Log"
tipo = 6
num1 = CDbl(TextBox1.Text)
TextBox1.Text = Math.Log10(num1)
Case "√"
tipo = 7
num1 = 1 / CDbl(TextBox1.Text)
TextBox1.Clear()
End Select
End Sub
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
num2 = CDbl(TextBox1.Text)
TextBox1.Clear()
Select Case tipo
Case 1
total = num1 / num2
Case 2
total = num1 * num2
Case 3
total = num1 + num2
Case 4
total = num1 - num2
Case 7
total = num1 ^ num2
End Select
TextBox1.Text = total
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
Select Case base
Case 10
TextBox1.Text = DecToBin(TextBox1.Text)
base = 2
Case 8
total = octToDec(TextBox1.Text)
TextBox1.Text = DecToBin(total)
base = 2
Case 16
TextBox1.Text = HexToBin(TextBox1.Text)
base = 2
End Select
End Sub
Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
Select Case base
Case 10
TextBox1.Text = Oct(TextBox1.Text)
base = 8
Case 2
total = BinToDec(TextBox1.Text)
TextBox1.Text = Oct(TextBox1.Text)
base = 8
Case 16
totalh = HexToDec(TextBox1.Text)
TextBox1.Text = Oct(TextBox1.Text)
base = 8
End Select
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
Select Case base
Case 10
TextBox1.Text = DecToHex(TextBox1.Text)
base = 16
Case 2
TextBox1.Text = BinToHex(TextBox1.Text)
base = 16
Case 8
total = octToDec(TextBox1.Text)
TextBox1.Text = DecToHex(TextBox1.Text)
base = 16
End Select
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
Select Case base
Case 2
TextBox1.Text = BinToDec(TextBox1.Text)
base = 10
Case 8
TextBox1.Text = octToDec(TextBox1.Text)
base = 10
Case 16
TextBox1.Text = HexToDec(TextBox1.Text)
base = 10
End Select
End Sub
End Class
darkerek, eso es vb.net, esto es Visual Basic 6