Descargar

Programas de recursividad y otros, creados en Visual Basic 2005 (página 2)

Enviado por jaimemontoya


Partes: 1, 2

4. UN ENTERO ES O NO IMPAR ENTRE 1 Y 25 INCLUSIVE

Public Class Form1

Function imparonoentre1y25(ByVal x As Integer) As Boolean

If x < 1 Or x > 25 Then

Return False

Else

If (x Mod 2 = 0) Then

Return False

Else

Return True

End If

End If

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox2.Text = imparonoentre1y25(TextBox1.Text)

End Sub

End Class

5. MÁXIMO COMÚN DIVISOR SIN RECURSIVIDAD

Public Class Form1

Function mcd(ByVal a As Integer, ByVal b As Integer) As Integer

Dim c As Integer

Dim nuevob As Integer

c = a

If (a < b) Then

a = b

b = c

End If

While b <> 0

nuevob = a Mod b

a = b

b = nuevob

End While

Return a

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox3.Text = mcd(TextBox1.Text, TextBox2.Text)

End Sub

End Class

6. MAYOR DE TRES VALORES REALES

Public Class Form1

Function menor(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer) As Integer

If (a < b) And (a < c) Then

Return a

Else

If (b < c) Then

Return b

Else

Return c

End If

End If

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

d.Text = menor(a.Text, b.Text, c.Text) 'en vez de textbox1.text se pone a.text porque se le cambio el nombre a textbox1 y se le puso a.

End Sub

End Class

7. a ELEVADO A LA n, SIENDO a Y n NUMEROS REALES CON RECURSIVIDAD

Public Class Form1

Function potencia(ByVal x As Integer, ByVal y As Integer) As Integer

If (y = 0) Then

Return 1

Else

Return x * potencia(x, y – 1)

End If

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox3.Text = potencia(TextBox1.Text, TextBox2.Text)

End Sub

End Class

8. SUCESIÓN FIBONACCI CON RECURSIVIDAD

Public Class Form1

Function fibonacci(ByVal x As Integer) As Integer

If (x = 0) Or (x = 1) Then

Return 1

Else

Return fibonacci(x – 1) + fibonacci(x – 2)

End If

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

f.Text = fibonacci(n.Text) 'TextBox2.Text=fibonacci(TextBox1.Text)

Dim s As String

Dim i As Integer

s = ""

For i = 0 To Val(n.Text)

s = s + Str(fibonacci(i)) + ","

Next i

tf.Text = s

lista.Items.Clear()

For i = 0 To Val(n.Text)

lista.Items.Add(fibonacci(i))

Next i

End Sub

End Class

9. MÁXIMO COMÚN DIVISOR DE DOS NÚMEROS ENTEROS CON RECURSIVIDAD

Public Class Form1

Function mcd(ByVal a As Integer, ByVal b As Integer) As Integer

If b = 0 Then

Return a

Else

Return mcd(b, a Mod b)

End If

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox3.Text = mcd(TextBox1.Text, TextBox2.Text)

End Sub

End Class

10. COMBINATORIO (n C r) CON RECURSIVIDAD

Public Class Form1

Function combinatorio(ByVal n As Integer, ByVal r As Integer) As Integer

If (r = 0) Or (r = n) Then

Return 1

Else

If (r = 1) Then

Return n

Else

Return combinatorio(n – 1, r) + combinatorio(n – 1, r – 1)

End If

End If

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox3.Text = combinatorio(TextBox1.Text, TextBox2.Text)

End Sub

End Class

11. FACTORIAL DE UN NÚMERO USANDO RECURSIVIDAD

Public Class Form1

Function factorial(ByVal x As Integer) As Integer

If (x = 0) Or (x = 1) Then

Return 1

Else

Return x * factorial(x – 1)

End If

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox2.Text = factorial(TextBox1.Text)

End Sub

End Class

12. EDAD DE UNA PERSONA

Public Class Form1

Function edad(ByVal x As String) As Integer

Dim ano As Integer

Dim mes As Integer

Dim dia As Integer

Dim valor As Integer

ano = Val(Strings.Right(x, 4))

mes = Val(Strings.Right(Strings.Left(x, Strings.Len(x) – 5), 2))

dia = Val(Strings.Right(Strings.Left(x, Strings.Len(x) – 8), 2))

If (mes < 10) Then 'este programa es con la suposicion que ahora es 13 de octubre de 2006

valor = 2006 – ano

Else

If (mes = 10) Then

If (dia <= 13) Then

valor = 2006 – ano

Else

If (dia > 13) Then

valor = 2005 – ano

End If

End If

Else

If (mes > 10) Then

valor = 2005 – ano

End If

End If

End If

Return valor

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox2.Text = edad(TextBox1.Text)

End Sub

End Class

13. PROGRAMA PARA OBTENER LAS 6 FUNCIONES TRIGONOMÉTRICAS DE UN ÁNGULO INTRODUCIDO EN RADIANES

Public Class Form1

Function seno(ByVal x As Double) As Double

Dim varseno As Double

varseno = System.Math.Sin(TextBox1.Text)

Return varseno

End Function

Function coseno(ByVal x As Double) As Double

Dim varcoseno As Double

varcoseno = System.Math.Cos(TextBox1.Text)

Return varcoseno

End Function

Function tangente(ByVal x As Double) As Double

Dim varseno As Double

Dim varcoseno As Double

Dim vartangente As Double

varseno = System.Math.Sin(TextBox1.Text)

varcoseno = System.Math.Cos(TextBox1.Text)

vartangente = varseno / varcoseno

Return vartangente

End Function

Function cotangente(ByVal x As Double) As Double

Dim varseno As Double

Dim varcoseno As Double

Dim varcotangente As Double

varseno = System.Math.Sin(TextBox1.Text)

varcoseno = System.Math.Cos(TextBox1.Text)

varcotangente = varcoseno / varseno

Return varcotangente

End Function

Function secante(ByVal x As Double) As Double

Dim varcoseno As Double

Dim varsecante As Double

varcoseno = System.Math.Cos(TextBox1.Text)

varsecante = 1 / varcoseno

Return varsecante

End Function

Function cosecante(ByVal x As Double) As Double

Dim varseno As Double

Dim varcosecante As Double

varseno = System.Math.Sin(TextBox1.Text)

varcosecante = 1 / varseno

Return varcosecante

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox2.Text = seno(TextBox1.Text)

TextBox3.Text = coseno(TextBox1.Text)

TextBox4.Text = tangente(TextBox1.Text)

TextBox5.Text = cotangente(TextBox1.Text)

TextBox6.Text = secante(TextBox1.Text)

TextBox7.Text = cosecante(TextBox1.Text)

End Sub

End Class

14. PROGRAMA PARA CONVERTIR UN VALOR INTRODUCIDO EN RADIANES, A GRADOS-MINUTOS-SEGUNDOS

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim valorparagrados As Double

Dim previogrados As Double

Dim grados As Double

Dim valorparaminutos As Double

Dim previominutos As Double

Dim minutos As Double

Dim valorparasegundos As Double

Dim previosegundos As Double

Dim segundos As Double

valorparagrados = Val(TextBox1.Text)

previogrados = Val(TextBox1.Text) * 180 / 3.141592654

grados = Int(previogrados)

valorparaminutos = previogrados – grados

previominutos = valorparaminutos * 60

minutos = Int(previominutos)

valorparasegundos = previominutos – minutos

previosegundos = valorparasegundos * 60

segundos = Int(previosegundos)

TextBox3.Text = grados

TextBox4.Text = minutos

TextBox5.Text = segundos

TextBox2.Text = Str(TextBox3.Text) & "º" & Str(TextBox4.Text) & "'" & Str(TextBox5.Text) & "''"

End Sub

End Class

 

Jaime Oswaldo Montoya Guzmán

Santa Ana, 14 de octubre de 2006

El Salvador

Partes: 1, 2
 Página anterior Volver al principio del trabajoPágina siguiente