The document is a Visual Basic code for a simple calculator application. It includes functionalities for basic arithmetic operations, square, square root, and factorial calculations. The application updates a display with user inputs and performs operations based on button clicks.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
16 views8 pages
Proyecto de Santiago
The document is a Visual Basic code for a simple calculator application. It includes functionalities for basic arithmetic operations, square, square root, and factorial calculations. The application updates a display with user inputs and performs operations based on button clicks.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8
Public Class Form1
Private currentNumber As Double = 0
Private result As Double = 0 Private currentOperation As String = "" Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles
Button10.Click txtDisplay.Text &= "0" End Sub
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles
Button12.Click txtDisplay.Text &= "1" End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles
Button11.Click txtDisplay.Text &= "2" End Sub
Private Sub Button16_Click(sender As Object, e As EventArgs) Handles
Button16.Click txtDisplay.Text &= "3" End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles
Button6.Click txtDisplay.Text &= "4" End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles
Button7.Click txtDisplay.Text &= "5" End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles
Button8.Click txtDisplay.Text &= "6" End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click txtDisplay.Text &= "7" End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
Button2.Click txtDisplay.Text &= "8" End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles
Button3.Click txtDisplay.Text &= "9" End Sub
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles
Button15.Click setOperation("+") End Sub Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click SetOperation("*") End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles
Button4.Click SetOperation("/") End Sub
Private Sub Button20_Click(sender As Object, e As EventArgs) Handles
Button20.Click SetOperation("-") End Sub
Private Sub Button19_Click(sender As Object, e As EventArgs) Handles
Button19.Click PerformOperation() End Sub
Private Sub Button18_Click(sender As Object, e As EventArgs) Handles
Button18.Click txtDisplay.Text = "" End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles
Button5.Click If Double.TryParse(txtDisplay.Text, currentNumber) Then result = Math.Pow(currentNumber, 2) txtDisplay.Text = result.ToString() Else MessageBox.Show("Por favor, ingresa un número válido.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End If End Sub
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles
Button13.Click If Double.TryParse(txtDisplay.Text, currentNumber) AndAlso currentNumber >= 0 AndAlso currentNumber = Math.Floor(currentNumber) Then result = Factorial(CInt(currentNumber)) txtDisplay.Text = result.ToString() Else MessageBox.Show("Por favor, ingresa un número entero positivo.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End If End Sub
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles
Button14.Click If Double.TryParse(txtDisplay.Text, currentNumber) AndAlso currentNumber >= 0 Then result = Math.Sqrt(currentNumber) txtDisplay.Text = result.ToString() Else MessageBox.Show("Por favor, ingresa un número no negativo.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End If End Sub
Private Sub btnPowerCustom_Click(sender As Object, e As EventArgs)
Handles Button17.Click If Double.TryParse(txtDisplay.Text, currentNumber) Then Dim exponentInput As String = InputBox("Ingresa el exponente:", "Potencia personalizada") Dim exponent As Double If Double.TryParse(exponentInput, exponent) Then result = Math.Pow(currentNumber, exponent) txtDisplay.Text = result.ToString() Else MessageBox.Show("Por favor, ingresa un exponente válido.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End If Else MessageBox.Show("Por favor, ingresa un número base válido.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End If End Sub Private Sub SetOperation(operation As String) If Double.TryParse(txtDisplay.Text, currentNumber) Then result = currentNumber currentOperation = operation txtDisplay.Text = "" End If End Sub
Private Sub PerformOperation()
If Double.TryParse(txtDisplay.Text, currentNumber) Then Select Case currentOperation Case "+" result += currentNumber Case "-" result -= currentNumber Case "*" result *= currentNumber Case "/" If currentNumber <> 0 Then result /= currentNumber Else MessageBox.Show("No se puede dividir entre cero", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Exit Sub End If End Select txtDisplay.Text = result.ToString() currentOperation = "" Else MessageBox.Show("Por favor, ingresa un número válido.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End If End Sub
Private Function Factorial(n As Integer) As Double
If n = 0 Or n = 1 Then Return 1 Else Return n * Factorial(n - 1) End If End Function End Class