Modulo Ec2Solver
Código
Module Ec2Solver '*----------------------------------------------------------------------------------------------* '| Author: [L]ord [R]NA | '| Country: Dominican Republic | '*----------------------------------------------------------------------------------------------* ' Description: A Module to solve 2nd Degree Equations. The Module Ec2Solver has a Function that | ' Get the First 3 constant value of the equation And 2 Double Variables are public in the module| ' to get the possible values of X. If everything is Ok the Function return True and if it's | ' something wrong Return False. | '*----------------------------------------------------------------------------------------------* '|Website: http://lordrna.co.cc/ http://foro.h-sec.org/ | '|Mail: lordrna@h-sec.org | '*----------------------------------------------------------------------------------------------* Public X1 As Double, X2 As Double Dim A1 As Double, B1 As Double, C1 As Double Public Function Ec2Solver(ByVal A As String, ByVal B As String, ByVal C As String) As Boolean If Double.TryParse(A, A1) And Double.TryParse(B, B1) And Double.TryParse(C, C1) Then Ec2Solver = Ec2Solver(A1, B1, C1) Else Ec2Solver = False End If End Function Public Function Ec2Solver(ByVal A As Double, ByVal B As Double, ByVal C As Double) As Boolean Dim temp As Double X1 = 0 X2 = 0 If A = 0 Then Ec2Solver = False If B = 0 And A <> 0 Then If C / A > 0 Then Ec2Solver = False Else If C = 0 Then Ec2Solver = True Else X1 = -System.Math.Sqrt(System.Math.Abs(C / A)) X2 = System.Math.Sqrt(System.Math.Abs(C / A)) Ec2Solver = True End If End If ElseIf C = 0 Then X1 = B / A Ec2Solver = True Else temp = (System.Math.Pow(B, 2)) - 4 * A * C If temp < 0 Then Ec2Solver = False Else temp = System.Math.Sqrt(temp) / (2 * A) X1 = -B + temp X2 = -B - temp Ec2Solver = True End If End If End Function End Module
Source
Código
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click A.Clear() B.Clear() C.Clear() X1.Clear() X2.Clear() End Sub Private Sub A_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A.Click A.Text = "" End Sub Private Sub B_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B.Click B.Text = "" End Sub Private Sub C_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles C.Click C.Text = "" End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If Len(A.Text) = 0 Or A.Text = "0" Then A.Text = "1" If Len(B.Text) = 0 Then B.Text = "0" If Len(C.Text) = 0 Then C.Text = "0" If Ec2Solver.Ec2Solver(A.Text, B.Text, C.Text) = True Then X1.Text = Str(Ec2Solver.X1) X2.Text = Str(Ec2Solver.X2) Else X1.Text = "Sin Solucion" X2.Text = "Real" End If End Sub End Class