Título: guardar un numero y mostrarlo de nuevo programa calculadora
Publicado por: creiko en 24 Agosto 2016, 20:45 pm
el problema es el siguiente debo hacer uan calculadora cientifica en visual basic ya tengo casi todas las funciones pero no logro hacer bien las siguientes si alguien me ayude o explique MS, M-, M+ MR un ejemplo seria asi supongamos ingreso el numero 9. le doy el boton C. se borra el contenido e la pantalla, y al pulsar MR deberia volver a mostrar el 9 Public Class Form1 Dim b As Integer Dim c As Integer Dim num1 As Double Dim num2 As Double Dim resul As Integer Private Sub Button2_Click(sender As Object, e As EventArgs) Handles B1.Click Tpantalla.Text = Tpantalla.Text & "1" End Sub Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Bsuma.Click b = 1 num1 = Tpantalla.Text Tpantalla.Text = "" End Sub Private Sub B0_Click(sender As Object, e As EventArgs) Handles B0.Click Tpantalla.Text = Tpantalla.Text & "0" End Sub Private Sub B2_Click(sender As Object, e As EventArgs) Handles B2.Click Tpantalla.Text = Tpantalla.Text & "2" End Sub Private Sub B3_Click(sender As Object, e As EventArgs) Handles B3.Click Tpantalla.Text = Tpantalla.Text & "3" End Sub Private Sub B4_Click(sender As Object, e As EventArgs) Handles B4.Click Tpantalla.Text = Tpantalla.Text & "4" End Sub Private Sub B5_Click(sender As Object, e As EventArgs) Handles B5.Click Tpantalla.Text = Tpantalla.Text & "5" End Sub Private Sub B6_Click(sender As Object, e As EventArgs) Handles B6.Click Tpantalla.Text = Tpantalla.Text & "6" End Sub Private Sub B7_Click(sender As Object, e As EventArgs) Handles B7.Click Tpantalla.Text = Tpantalla.Text & "7" End Sub Private Sub B8_Click(sender As Object, e As EventArgs) Handles B8.Click Tpantalla.Text = Tpantalla.Text & "8" End Sub Private Sub B9_Click(sender As Object, e As EventArgs) Handles B9.Click Tpantalla.Text = Tpantalla.Text & "9" End Sub Private Sub Bigual_Click(sender As Object, e As EventArgs) Handles Bigual.Click num2 = Tpantalla.Text If b = 1 Then Tpantalla.Text = num1 + num2 End If If b = 2 Then Tpantalla.Text = num1 - num2 End If If b = 3 Then Tpantalla.Text = num1 * num2 End If If b = 4 Then If num2 <> 0 Then Tpantalla.Text = num1 / num2 Else Tpantalla.Text = "No divisible" End If End If If b = 5 Then Tpantalla.Text = num1 ^ num2 End If End Sub Private Sub Bresta_Click(sender As Object, e As EventArgs) Handles Bresta.Click b = 2 Bcoma.Enabled = True num1 = Tpantalla.Text Tpantalla.Text = "" End Sub Private Sub Bmulti_Click(sender As Object, e As EventArgs) Handles Bmulti.Click b = 3 Bcoma.Enabled = True num1 = Tpantalla.Text Tpantalla.Text = "" End Sub Private Sub Bdivi_Click(sender As Object, e As EventArgs) Handles Bdivi.Click b = 4 Bcoma.Enabled = True num1 = Tpantalla.Text Tpantalla.Text = "" End Sub Private Sub Blim_Click(sender As Object, e As EventArgs) Handles Blim.Click Tpantalla.Text = "" Bcoma.Enabled = True End Sub Private Sub Bcoma_Click(sender As Object, e As EventArgs) Handles Bcoma.Click Tpantalla.Text = Tpantalla.Text & "," Bcoma.Enabled = False End Sub Private Sub Tpantalla_TextChanged(sender As Object, e As EventArgs) Handles Tpantalla.TextChanged End Sub Private Sub Buno_Click(sender As Object, e As EventArgs) Handles Buno.Click Try Dim lar As Integer If Tpantalla.Text <> "" Then lar = Tpantalla.Text.Length Tpantalla.Text = Mid(Tpantalla.Text, 1, lar - 1) End If Catch ex As Exception End Try End Sub Private Sub Bx_Click(sender As Object, e As EventArgs) Handles Bx.Click b = 5 num1 = Tpantalla.Text Tpantalla.Text = "" If b = 5 Then Tpantalla.Text = num1 ^ 2 End If End Sub Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click b = 5 num1 = Tpantalla.Text Tpantalla.Text = "" End Sub Private Sub Braiz_Click(sender As Object, e As EventArgs) Handles Braiz.Click b = 7 num1 = Tpantalla.Text Tpantalla.Text = "" If b = 7 Then Tpantalla.Text = Math.Sqrt(num1) End If End Sub Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click b = 8 num2 = Tpantalla.Text Tpantalla.Text = "" If b = 8 Then Tpantalla.Text = 1 / num2 End If End Sub Private Sub Bmm_Click(sender As Object, e As EventArgs) Handles Bmm.Click b = 9 num1 = Tpantalla.Text Tpantalla.Text = "" If b = 9 Then Tpantalla.Text = num1 * (-1) End If End Sub Private Sub Bfac_Click(sender As Object, e As EventArgs) Handles Bfac.Click Dim a, b, c As Integer a = (Tpantalla.Text) b = 1 For c = 1 To a b = b * c Tpantalla.Text = (b) Next End Sub Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) End Sub Private Sub Bsen_Click(sender As Object, e As EventArgs) Handles Bsen.Click b = 10 num1 = Tpantalla.Text Tpantalla.Text = "" If b = 10 Then Tpantalla.Text = Math.Sin(Val(num1) * Math.PI / 180) End If End Sub Private Sub Bcos_Click(sender As Object, e As EventArgs) Handles Bcos.Click b = 11 num1 = Tpantalla.Text Tpantalla.Text = "" If b = 11 Then Tpantalla.Text = Math.Cos(Val(num1) * Math.PI / 180) End If End Sub Private Sub Btan_Click(sender As Object, e As EventArgs) Handles Btan.Click b = 12 num1 = Tpantalla.Text Tpantalla.Text = "" If b = 12 Then Tpantalla.Text = Math.Tan(Val(num1) * Math.PI / 180) End If End Sub Private Sub Bmem_Click(sender As Object, e As EventArgs) Handles Bmem.Click End Sub End Class
Título: Re: guardar un numero y mostrarlo de nuevo programa calculadora
Publicado por: Eleкtro en 25 Agosto 2016, 18:49 pm
Esto no es Visual Basic, es Visual Basic.NET, pro favor, publica correctamente las preguntas en el futuro. no logro hacer bien las siguientes si alguien me ayude o explique
MS, M-, M+ MR Se hace muy tedioso de leer el código que has publicado, y los nombres de muchos de los controles que has definido (los botones) no describen su función ni tampoco has dejado pistas, ni tampoco te has molestado en explicarnos al menos en lo que debemos fijarnos de tu código, así que disculpa pero en vez de ponerme a revisar y buscar un fallo en tu código directamente prefiero mostrarte un ejemplo (el cual te debería resultar muy sencillo adaptarlo) de como puedes implementar las funciones de memoria: ' El formato del separador de decimales. Private ReadOnly numberFormat As NumberFormatInfo = CultureInfo.GetCultureInfo("en-US").NumberFormat Private memValue As Decimal ' M+ Private Sub Button_MemAdd_Click(ByVal sender As Object, ByVal e As EventArgs) _ Handles Button_MemAdd.Click Me.memValue += Decimal.Parse(Me.TextBox_CalcDisplay.Text, Me.numberFormat) End Sub ' M- Private Sub Button_MemSubstract_Click(ByVal sender As Object, ByVal e As EventArgs) _ Handles Button_MemSubstract.Click Me.memValue -= Decimal.Parse(Me.TextBox_CalcDisplay.Text, Me.numberFormat) End Sub ' MR Private Sub Button_MemRecall_Click(ByVal sender As Object, ByVal e As EventArgs) _ Handles Button_MemRecall.Click Me.TextBox_Input.Text = Convert.ToString(If(Me.memValue = 0.0D, 0D, Me.memValue), Me.numberFormat) End Sub ' MC Private Sub Button_MemClear_Click(ByVal sender As Object, ByVal e As EventArgs) _ Handles Button_MemClear.Click Me.memValue = 0D End Sub
Aparte de eso, veo muchas partes del código cuyo algoritmo lo puedes simplificar a un par de lineas, es comprensible que todavía no te des cuenta puesto que estarás iniciándote en el lenguaje empezando por programar una calculadora, pero conviene que sepas desde ya sobre malas costumbres de programación que solo te complicarán la vida y hacer un esfuerzo por evitar escribir cosas como esta: Private Sub B0_Click(sender As Object, e As EventArgs) Handles B0.Click Tpantalla.Text = Tpantalla.Text & "0" End Sub Private Sub B2_Click(sender As Object, e As EventArgs) Handles B2.Click Tpantalla.Text = Tpantalla.Text & "2" End Sub Private Sub B3_Click(sender As Object, e As EventArgs) Handles B3.Click Tpantalla.Text = Tpantalla.Text & "3" End Sub Private Sub B4_Click(sender As Object, e As EventArgs) Handles B4.Click Tpantalla.Text = Tpantalla.Text & "4" End Sub Private Sub B5_Click(sender As Object, e As EventArgs) Handles B5.Click Tpantalla.Text = Tpantalla.Text & "5" End Sub Private Sub B6_Click(sender As Object, e As EventArgs) Handles B6.Click Tpantalla.Text = Tpantalla.Text & "6" End Sub Private Sub B7_Click(sender As Object, e As EventArgs) Handles B7.Click Tpantalla.Text = Tpantalla.Text & "7" End Sub Private Sub B8_Click(sender As Object, e As EventArgs) Handles B8.Click Tpantalla.Text = Tpantalla.Text & "8" End Sub Private Sub B9_Click(sender As Object, e As EventArgs) Handles B9.Click Tpantalla.Text = Tpantalla.Text & "9" End Sub
Piensa en lo siguiente: tienes 10 botones, esos 10 botones llevan por nombre un número (0...9), y los 10 cumplen una misma funcionalidad que es añadir su "nombre" o número correspondiente al textbox, entonces, ¿por que no unir todos los botones en el mismo controlador de eventos con la sentencia Handles?: Private Sub MultiHandler_NumericButtons_Click(ByVal sender As Object, ByVal e As EventArgs) _ Handles Button_Num1.Click, Button_Num2.Click, Button_Num3.Click, Button_Num4.Click, Button_Num5.Click, Button_Num6.Click, Button_Num7.Click, Button_Num8.Click, Button_Num9.Click, Button_Num0.Click Me.TextBox_CalcDisplay.AppendText(DirectCast(sender, Button).Text) End Sub
PD: En caso de que la propiedad TextBox.Text no sea un número (es de suponer que si, al tratarse de una calculadora), siempre puedes utilizar la propiedad TextBox.TagSaludos!
|