Practica 11

6
Guía práctica # 11: PROCEDIMIENTOS Y FUNCIONES EN VB .NET 1. Escriba un procedimiento que imprima la hora actual. Código VB.NET: Module Module1 Sub Main() hora() Console.ReadLine() End Sub Sub hora() Console.WriteLine("Hora actual: " & TimeOfDay) End Sub End Module 2. Escriba una aplicación que capture un texto por teclado y que posea un procedimiento para mostrar el texto capturado en mayúsculas y color rojo y otro procedimiento para mostrarlo en minúsculas y color amarillo. Código VB.NET: Sub main() Dim texto As String Console.WriteLine("Ingrese un texto:") texto = Console.ReadLine() Console.WriteLine() RojoUp(texto) AmaLow(texto) Console.ReadLine() End Sub Sub RojoUp(ByVal texto As String) Console.Write("Rojo y Mayúsculas: ") Console.ForegroundColor = ConsoleColor.Red Console.WriteLine(texto.ToUpper) End Sub Sub AmaLow(ByVal texto As String) Console.ForegroundColor = ConsoleColor.Gray Console.Write("Amarillo y Minusculas: ") Console.ForegroundColor = ConsoleColor.Yellow Console.WriteLine(texto.ToLower) End Sub End Module

Transcript of Practica 11

Page 1: Practica 11

Guía práctica # 11:

PROCEDIMIENTOS Y FUNCIONES EN VB .NET

1. Escriba un procedimiento que imprima la hora actual.

Código VB.NET:

Module Module1 Sub Main() hora() Console.ReadLine() End Sub Sub hora() Console.WriteLine("Hora actual: " & TimeOfDay) End Sub End Module

2. Escriba una aplicación que capture un texto por teclado y que posea un procedimiento para mostrar el texto capturado en mayúsculas y color rojo y otro procedimiento para mostrarlo en minúsculas y color amarillo.

Código VB.NET: Sub main() Dim texto As String Console.WriteLine("Ingrese un texto:") texto = Console.ReadLine() Console.WriteLine() RojoUp(texto) AmaLow(texto) Console.ReadLine() End Sub Sub RojoUp(ByVal texto As String) Console.Write("Rojo y Mayúsculas: ") Console.ForegroundColor = ConsoleColor.Red Console.WriteLine(texto.ToUpper) End Sub Sub AmaLow(ByVal texto As String) Console.ForegroundColor = ConsoleColor.Gray Console.Write("Amarillo y Minusculas: ") Console.ForegroundColor = ConsoleColor.Yellow Console.WriteLine(texto.ToLower) End Sub End Module

Page 2: Practica 11

3. Escriba una aplicación para capturar el número de teléfono de 5 participantes y que posea un procedimiento que seleccione e imprima de forma aleatoria el número de teléfono ganador.

Código VB.NET: Module Module3 Sub main() Console.WriteLine("Ingrese 5 números telefonicos") Seleccion() Console.ReadLine() End Sub Sub Seleccion() Dim tel(4) As String For i = 0 To 4 tel(i) = Console.ReadLine() Next Dim re As New Random() Dim n As Integer n = re.Next(0, 5) Console.WriteLine() Console.Write("El telefono ganador es: " & tel(n)) End Sub End Module

4. Escriba una función para calcular el valor a pagar a un empleado por servicios, capturando por teclado el nombre y horas trabajadas, estableciendo el valor de la hora a $ 6.50.

Código en VB.NET: Module Module4 Sub main() Dim hora As Decimal Console.Write("Ingrese la cantidad de horas: ") hora = Console.ReadLine Console.WriteLine() Console.WriteLine("Monto a pagar: $" & sueldo(hora)) Console.ReadLine() End Sub Function sueldo(ByVal x As Integer) As Decimal Return x * 6.5 End Function End Module

Page 3: Practica 11

5. Escriba una función que indique si un año capturado por teclado es o no es bisiesto.

Código en VB.NET

Module Module5 Sub main() Dim año As Integer Console.WriteLine("AÑO BISIESTO") Console.Write("Ingrese un año: ") año = Console.ReadLine() Console.WriteLine() Console.WriteLine(bisiesto(año)) Console.ReadLine() End Sub Function bisiesto(ByVal x As Integer) If (x Mod 4 = 0) And ((x Mod 100 <> 0) Or (x Mod 400 = 0)) Then Return "El año es bisiesto" Else Return "El año no es bisiesto" End If End Function End Module

6. Escriba una función que retorne el mayor de dos números ingresados por teclado.

Código en VB.NET: Module Module6 Sub main() Dim num1, num2 As Integer Console.WriteLine("Cual numero es mayor?") Console.WriteLine("Ingrese dos numeros:") num1 = Console.ReadLine num2 = Console.ReadLine Console.WriteLine() Console.Write("El número mayor es: " & mayor(num1, num2)) Console.ReadLine() End Sub Function mayor(ByVal x As Integer, ByVal y As Integer) If x > y Then Return x Else Return y End If End Function End Module

Page 4: Practica 11

7. Escriba una función que retorne si un número capturado por teclado es par o impar.

Código en VB.NET: Module Module7 Sub main() Dim k As Integer Console.WriteLine("PROGRAMA PARA SABER SI UN NUMERO ES PAR O IMPAR<<<<<<") Console.Write("Introduzca un número: ") k = Console.ReadLine() Console.WriteLine() Console.Write("Él número es " & ParImpar(k)) Console.ReadLine() End Sub Function ParImpar(ByVal x As Integer) As String If x Mod 2 = 0 Then Return "par" Else Return "impar" End If End Function End Module

8. Escriba una función que retorne el factorial de un número capturado por teclado.

Código en VB.NET: Module Module8 Sub main() Dim num As Integer Console.WriteLine("Factorial de un numero") Console.Write("Introduzca un número: ") num = Integer.Parse(Console.ReadLine()) Console.WriteLine() Console.WriteLine(num & "! = " & facto(num)) Console.Read() End Sub Function facto(ByVal num) As Integer If num = 1 Then Return num Else Return num * facto(num - 1) End If End Function End Module

Page 5: Practica 11

9. Escriba una aplicación para calcular la nota final de un alumno, utilizando el sistema de evaluación de la materia programación I, crear una sola función que permita calcular la nota promedio de cada periodo.

Código en VB.NET:

Module Module9 Sub main() Dim nota1, nota2, nota3, prom1, prom2, prom3, final As Decimal Console.WriteLine() Console.WriteLine("PERIODO 1") Console.WriteLine("Ingrese nota 1") nota1 = Console.ReadLine() Console.WriteLine("Ingrese nota 2") nota2 = Console.ReadLine() Console.WriteLine("Ingrese nota 3") nota3 = Console.ReadLine() prom1 = prom(nota1, nota2, nota3) Console.Clear() '--------------------------------------------- Console.WriteLine("PERIODO 2") Console.WriteLine("Ingrese nota 1") nota1 = Console.ReadLine() Console.WriteLine("Ingrese nota 2") nota2 = Console.ReadLine() Console.WriteLine("Ingrese nota 3") nota3 = Console.ReadLine() prom2 = prom(nota1, nota2, nota3) Console.Clear() '--------------------------------------------- Console.WriteLine("PERIODO 3") Console.WriteLine("Ingrese nota 1") nota1 = Console.ReadLine() Console.WriteLine("Ingrese nota 2") nota2 = Console.ReadLine() Console.WriteLine("Ingrese nota 3") nota3 = Console.ReadLine() prom3 = prom(nota1, nota2, nota3) Console.Clear() Console.WriteLine("RESULTADOS") Console.WriteLine() Console.WriteLine("Periodo 1: " & prom1) Console.WriteLine("Periodo 2: " & prom2) Console.WriteLine("Periodo 3: " & prom3) final = (prom1 + prom2 + prom3) / 3 Console.WriteLine() Console.WriteLine("Nota final: " & FormatNumber(final, 1)) Console.ReadLine() End Sub Function prom(ByVal x As Decimal, ByVal y As Decimal, ByVal z As Decimal) Dim total As Decimal total = ((x * 0.25) + (y * 0.25) + (z * 0.5)) Return FormatNumber(total, 1) End Function End Module

Page 6: Practica 11

10. Escribir una aplicación que capture por teclado 4 números y posea 3 funciones de nombre sumar pero que la primera permita sumar 2 números, la segunda 3 y la cuarta 4 números.

Código en VB.NET: Module Module10 Sub main() Dim n1, n2, n3, n4 As Integer Console.WriteLine(" números a sumar") n1 = Console.ReadLine() n2 = Console.ReadLine() n3 = Console.ReadLine() n4 = Console.ReadLine() Console.WriteLine() Console.WriteLine("Probando la función:") Console.WriteLine("Sumando 4 números: " & sumar(n1, n2, n3, n4)) Console.WriteLine("Sumando 3 números: " & sumar(n1, n2, n3)) Console.WriteLine("Sumando 2 números: " & sumar(n1, n2)) Console.ReadLine() End Sub Function sumar(ByVal x As Integer, ByVal y As Integer) As Integer Return x + y End Function Function sumar(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Integer Return x + y + z End Function Function sumar(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer, ByVal a As Integer) As Integer Return x + y + z + a End Function End Module