Visual Basic 2012 Modo Consola

119
Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 1 - PRACTICAS DEL JUEVES 16 DE ABRIL DEL 2015 TURNO DE 9-11 1. El programa de hola mundo HOLA MUNDO Module Module1 Sub Main() Console .WriteLine( "hola mundo" ) Console .ReadLine() End Sub End Module Solución Ejercicio 2. Sumar dos números enteros Module Module1 Dim NRO1, NRO2, suma As Integer Sub Main() Console .Write( "INGRESE PRIMER Numero" ) NRO1 = Console .ReadLine Console .Write( "INGRESE Segundo Numero" ) NRO2 = Console .ReadLine suma = NRO1 + NRO2 Console .WriteLine( "LA SUMA ES {0}" , suma) Console .ReadLine() End Sub End Module Ejercicico 3 realizar las 4 operaciones Module Module1 Dim NRO1, NRO2, suma As Single Sub Main() Console .Write( "INGRESE PRIMER Numero" )

description

visual basic

Transcript of Visual Basic 2012 Modo Consola

Page 1: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 1 -

PRACTICAS DEL JUEVES 16 DE ABRIL DEL 2015

TURNO DE 9-111. El programa de hola mundo HOLA MUNDO

Module Module1 Sub Main() Console.WriteLine("hola mundo") Console.ReadLine() End SubEnd Module

Solución

Ejercicio 2. Sumar dos números enteros

Module Module1 Dim NRO1, NRO2, suma As Integer Sub Main() Console.Write("INGRESE PRIMER Numero") NRO1 = Console.ReadLine Console.Write("INGRESE Segundo Numero") NRO2 = Console.ReadLine suma = NRO1 + NRO2 Console.WriteLine("LA SUMA ES {0}", suma) Console.ReadLine() End SubEnd Module

Ejercicico 3 realizar las 4 operacionesModule Module1 Dim NRO1, NRO2, suma As Single Sub Main() Console.Write("INGRESE PRIMER Numero") NRO1 = Console.ReadLine Console.Write("INGRESE Segundo Numero") NRO2 = Console.ReadLine Console.WriteLine("LA SUMA ES {0}", NRO1 + NRO2) Console.WriteLine("LA RESTA ES {0}", NRO1 - NRO2) Console.WriteLine("LA MULTIPLICACION ES {0}", NRO1 * NRO2) Console.WriteLine("LA DIVISION ENTERA ES {0}", NRO1 \ NRO2)

Page 2: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 2 -

Console.WriteLine("LA DIVISION REAL ES {0}", NRO1 / NRO2) Console.WriteLine("EL MODULO ES ES {0}", NRO1 Mod NRO2) Console.ReadLine() End SubEnd Module

Problema 4 Realizar las operaciones matematicas

Module Module1 Dim NRO1, NRO2, suma As Single Sub Main() Console.Write("INGRESE PRIMER Numero") NRO1 = Console.ReadLine Console.Write("INGRESE Segundo Numero") NRO2 = Console.ReadLine Console.WriteLine("LA SUMA ES {0}", NRO1 + NRO2) Console.WriteLine("LA RESTA ES {0}", NRO1 - NRO2) Console.WriteLine("LA MULTIPLICACION ES {0}", NRO1 * NRO2) Console.WriteLine("LA DIVISION ENTERA ES {0}", NRO1 \ NRO2) Console.WriteLine("LA DIVISION REAL ES {0}", NRO1 / NRO2) Console.WriteLine("EL MODULO ES ES {0}", NRO1 Mod NRO2) Console.WriteLine("EL MODULO ES ES {0}", Int(NRO1) Mod Int(NRO2)) 'Console.WriteLine("{0} ELEVADO A {1} es = {2}", NRO1, NRO2,nro ) Console.WriteLine("{0} ELEVADO A {1} es = {2}", NRO1, NRO2, Math.Pow(NRO1, NRO2)) Console.WriteLine("raiz de {0} es {1}", NRO1, Math.Pow(NRO1, 0.333)) Console.ReadLine() End SubEnd Module

Problema 5 Ejercicio de Select case menu de opciones

Page 3: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 3 -

Option Explicit OnModule Module1 Dim n1, n2 As Single Dim opcion As Integer Sub Main() Do Console.WriteLine("1. Ingresar 2.sumar 3.resto 4.multiplico, 5 divido 6 Salir ") Console.Write("ingrese opcion") opcion = Console.ReadLine Select Case opcion Case 1 ' es la opcion ingresar" Console.Write("INGRESE PRIMER Numero") : n1 = Console.ReadLine Console.Write("INGRESE Segundo Numero") : n2 = Console.ReadLine Case 2 ' es la opcion sumar" Console.WriteLine("LA SUMA ES {0}", n1 + n2) Case 3 ' es la opcion RESTA" Console.WriteLine("LA RESTA ES {0}", n1 - n2) Case 4 ' es la opcion MULTIPLICACION" Console.WriteLine("LA MULTIPLICACION ES {0}", n1 * n2) Case 5 ' es la opcion DIVISION" Console.WriteLine("LA SUMA ES {0}", n1 / n2) Case 6 Case Else Console.WriteLine("opcion erronea") End Select Loop While opcion <> 6 End SubEnd Module

LABORATORIO DE SISTEMAS DE INFORMACION JUEVES 16-ABRIL DEL 2015 TURNO DE 11-13 HORAS

EJERCICIO NRO 1Module Module1 Sub Main() Console.WriteLine("HOLA MUNDO") Console.ReadLine() End SubEnd Module

Page 4: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 4 -

2. Sumar dos numerous realesModule Module1 Dim n1, N2 As Single Dim Suma As Single Sub Main() Console.Write("INGRESE PRIMER NUMERO") n1 = Console.ReadLine() Console.Write("INGRESE SEGUNDO NUMERO") N2 = Console.ReadLine() SUMA = n1 + N2 Console.WriteLine("la suma es {0} ", Suma) Console.ReadLine() End SubEnd Module

Ejercicio Nro 3

Module Module1 Dim NRO1, NRO2, suma As Single Sub Main() Console.Write("INGRESE PRIMER Numero") NRO1 = Console.ReadLine Console.Write("INGRESE Segundo Numero") NRO2 = Console.ReadLine Console.WriteLine("LA SUMA ES {0}", NRO1 + NRO2) Console.WriteLine("LA RESTA ES {0}", NRO1 - NRO2) Console.WriteLine("LA MULTIPLICACION ES {0}", NRO1 * NRO2) Console.WriteLine("LA DIVISION ENTERA ES {0}", NRO1 \ NRO2) Console.WriteLine("LA DIVISION REAL ES {0}", NRO1 / NRO2) Console.WriteLine("EL MODULO ES ES {0}", NRO1 Mod NRO2) Console.ReadLine() End SubEnd Module

Page 5: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 5 -

Ejercicio Nro 4 operaciones matematicas y numeros aleatorios

Sub Main() Randomize() Console.Write("INGRESE PRIMER NUMERO") n1 = Console.ReadLine() Console.Write("INGRESE SEGUNDO NUMERO") N2 = Console.ReadLine() Console.WriteLine("la suma es {0} ", n1 + N2) Console.WriteLine("la resta es {0} ", n1 - N2) Console.WriteLine("la multiplicacion es {0} ", n1 * N2) Console.WriteLine("la division entera es {0} ", n1 \ N2) Console.WriteLine("la division reales {0} ", n1 / N2) Console.WriteLine("el modulo es {0} ", n1 Mod N2) Console.WriteLine("LA potencia de {0} elevado a {1} es = {2} ", n1, N2, Math.Pow(n1, N2)) Console.WriteLine("LA raiz de {0} es {1} ", n1, Math.Sqrt(n1)) Console.WriteLine("numero aleatorio {0} ", Int(Rnd() * 20)) Console.ReadLine() End SubEnd Module

Ejercicio Nro 5 Menu de opciones

Module Module1 Dim n1, N2 As Single Dim opcion As Integer Sub Main() Do Console.WriteLine("1. Ingresar 2. sumar 3. restar 4.multiplicar 5.dividir 6.salir") Console.Write("ingrese opcion ")

Page 6: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 6 -

opcion = Console.ReadLine Select Case opcion Case 1 Console.Write("INGRESE PRIMER NUMERO") n1 = Console.ReadLine() Console.Write("INGRESE SEGUNDO NUMERO") N2 = Console.ReadLine() Case 2 : Console.WriteLine("la suma es {0} ", n1 + N2) Case 3 Console.WriteLine("la resta es {0} ", n1 - N2) Case 4 Console.WriteLine("la multiplicacion es {0} ", n1 * N2) Case 5 Console.WriteLine("la division reales {0} ", n1 / N2) Case Else Console.WriteLine("Numero erroneo ") End Select 'Loop While opcion <> 6 Loop Until opcion = 6 Console.ReadLine() End SubEnd Module

Elaborar un Cuadro de colores

Module Module1 Sub Main() Dim Ancho As Integer = 14 Dim alto As Integer = 5 Dim px As Integer = 20 Dim py As Integer = 2 Dim color As Integer = 11 Dim fila, col As Integer Console.ForegroundColor = color Console.BackgroundColor = color

Page 7: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 7 -

For fila = 0 To alto For col = 0 To Ancho Console.SetCursorPosition(px + col, py + fila) Console.Write("*") Next Next Console.ReadLine() End SubEnd Module

PLAN DE PRACTICAS SEMANA 20 AL 27 DE ABRIL DEL 2015

1. Procedimientos de tipo function factorial

Module Module1 Function fact(ByVal N As Integer) As Integer Dim f = 1, i As Integer For i = 1 To N f = f * i Next Return f End Function Sub Main() Dim n, r, combi As Integer Console.Write("Ingrese un n ") n = CInt(Console.ReadLine()) Console.Write("Ingrese r ") r = CInt(Console.ReadLine()) Console.WriteLine("El factorial de {0} es {1} ", n, fact(n)) combi = fact(n) / (fact(r) * fact(n - r)) Console.WriteLine("la combinatoria de {0} en {1} es ={2} ", n, r, combi) Console.ReadLine() End SubEnd Module

Page 8: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 8 -

2 Procedimientos de tipo Sub Elaborar un cuadro con funciones

Module Module1 Sub Cuadro(cx As Integer, cy As Integer, ancho As Integer, alto As Integer, color1 As Integer, color2 As Integer) Console.ForegroundColor = color1 Console.BackgroundColor = color2 Dim fila, col As Integer For fila = 0 To alto For col = 0 To ancho Console.SetCursorPosition(cx + col, cy + fila) Console.Write("*") Next Next End Sub Sub Main() Dim Ancho As Integer = 14 Dim alto As Integer = 5 Dim px As Integer = 20 Dim py As Integer = 2 Dim color1 As Integer = 9 Dim color2 As Integer = 12 Cuadro(px, py, Ancho, alto, color1, color2) Console.ReadLine() End SubEnd Module

3. Generar cuadros de colores en forma aleatoria con sonido

Page 9: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 9 -

Module Module1 Sub Main() Dim Ancho As Integer = 60 Dim alto As Integer = 20 Dim px As Integer = 20 Dim py As Integer = 2 Dim rancho, ralto, rx, ry, color1, color2, frec, duracion As Integer Randomize() Dim opcion As Integer = 0 Dim cont As Integer = 0 'Dim frec As inte Do rancho = Int(Rnd() * 60) ralto = Int(Rnd() * 20) rx = 1 + Int(Rnd() * 60) ry = 1 + Int(Rnd() * 20) rx = Int(Rnd() * 15) ry = Int(Rnd() * 15) color1 = 1 + Int(Rnd() * 15) color2 = 1 + Int(Rnd() * 15) frec = 37 + Int(Rnd() * 5000) duracion = 10 + Rnd(200) If (color1 > 15) Then color1 = 15 If (color2 > 15) Then color2 = 15 If (rx + rancho < 80 And ry + ralto < 24) Then Cuadro(rx, ry, rancho, ralto, color1, color1) Console.Beep(frec, duracion) 'System.Threading.Thread.Sleep(duracion) ' 1 segundo End If cont = cont + 1 If cont Mod 10 = 0 Then Console.BackgroundColor = 0 Console.Clear() End If Loop Until cont > 1000 Console.ReadLine() End SubEnd Module

4. Modificar el programa para mover aleatoriamente un objeto

Module Module2 Sub Cuadro(cx As Integer, cy As Integer, ancho As Integer, alto As Integer, color1 As Integer, color2 As Integer) Console.ForegroundColor = color1 Console.BackgroundColor = color2 Dim fila, col As Integer For fila = 0 To alto

Page 10: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 10 -

For col = 0 To ancho Console.SetCursorPosition(cx + col, cy + fila) Console.Write("*") Next Next End SubEnd Module

Module Module1 Sub main() Dim cx As Integer = 40 Dim cy As Integer = 12 Dim cont As Integer = 0 Dim color1 As Integer = 9 Dim color2 As Integer = 10 Dim opcion As Integer Dim ancho As Integer = 4 Dim alto As Integer = 3 Do opcion = 1 + Int(Rnd() * 12) Select Case opcion Case 1 : If cx < 78 Then cx = cx + 1 Case 2 : If cx > 1 Then cx = cx - 1 Case 3 : If cy < 24 Then cy = cy + 1 Case 4 : If cy > 1 Then cy = cy - 1 Case 5 : If ancho < 10 Then ancho = ancho + 1 Case 6 : If ancho > 1 Then ancho = ancho - 1 Case 7 : If alto < 6 Then alto = alto + 1 Case 8 : If alto > 1 Then alto = alto - 1 Case 9 : If color1 < 15 Then color1 = color1 + 1 Case 10 : If color1 > 1 Then color1 = color1 - 1 Case 11 : If color2 < 15 Then color2 = color2 + 1 Case 12 : If color2 > 1 Then color2 = color2 - 1 End Select Console.BackgroundColor = 0 Console.Clear() Cuadro(cx, cy, ancho, alto, color1, color2) System.Threading.Thread.Sleep(20) cont = cont + 1 Loop Until cont > 1000 End SubEnd Module

5-Mueva un mensaje con las teclas direccionales(vea el ejercicio de leeteclas del libro)

Module Module1 Sub main() Dim cki As ConsoleKeyInfo Dim x1 As Integer = 2 ' limite x inferior Dim y1 As Integer = 2 ' limite x superior Dim x2 As Integer = 60 ' limite y inferior Dim y2 As Integer = 20 ' limite y superior

Page 11: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 11 - Dim x As Integer = 40 Dim y As Integer = 12 Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.") Console.WriteLine("Press the Escape (Esc) key to quit: " + vbCrLf) Console.SetCursorPosition(x, y) Console.Write("hola") Do cki = Console.ReadKey() If cki.Key = ConsoleKey.LeftArrow And x > x1 Then x = x - 1 If cki.Key = ConsoleKey.RightArrow And x < x2 Then x = x + 1 If cki.Key = ConsoleKey.UpArrow And y > y1 Then y = y - 1 If cki.Key = ConsoleKey.DownArrow And y < y2 Then y = y + 1 Console.Clear() Console.SetCursorPosition(x, y) Console.Write("hola") Loop While cki.Key <> ConsoleKey.Escape End SubEnd Module

Module Module1 Sub main() Dim cki As ConsoleKeyInfo Dim x1 As Integer = 2 ' limite x inferior Dim y1 As Integer = 2 ' limite x superior Dim x2 As Integer = 60 ' limite y inferior Dim y2 As Integer = 20 ' limite y superior Dim x As Integer = 40 Dim y As Integer = 12

Page 12: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 12 -

Dim letra As String = "X" Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.") Console.WriteLine("Press the Escape (Esc) key to quit: " + vbCrLf) Console.SetCursorPosition(x, y) Console.Write("{0}", letra) Do cki = Console.ReadKey() If cki.Key = ConsoleKey.LeftArrow And x > x1 Then x = x - 1 If cki.Key = ConsoleKey.RightArrow And x < x2 Then x = x + 1 If cki.Key = ConsoleKey.UpArrow And y > y1 Then y = y - 1 If cki.Key = ConsoleKey.DownArrow And y < y2 Then y = y + 1 'Console.Clear() Console.SetCursorPosition(x, y) Console.Write("{0}", letra) If cki.Key = ConsoleKey.LeftArrow Then Console.SetCursorPosition(x - 1, y)

Loop While cki.Key <> ConsoleKey.Escape End SubEnd Module

Parámetros por valor y por referencia

RECURSIVIDAD

3.9.1 Recursividad directa.-Ej. factorial de un Número n! = n*(n-1)! si n>0 parte recursiva 1 si n =0 parte de terminación6: Recursividad directa

Option Explicit OnOption Strict OnModule Module1 Function fact(ByVal n As Integer) As Integer

Page 13: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 13 -

Dim f As Integer If (n > 1) Then f = n * fact(n - 1) REM parte recursiva Else f = 1 REM parte no recursiva End If Return f End Function Sub main() Dim f, n As Integer Console.Write("Ingrese un numero ") n = CInt(Console.ReadLine()) Console.WriteLine("El factorial de ese numero es {0} ", fact(n)) Console.ReadLine() End SubEnd Module

A ba)Secuencia de llamadas recursivas b) valores regresados de cada llamada recursiva

7 función recursiva de tipo void

Option Explicit OnOption Strict OnModule Module1 Sub imprimir(ByVal x As Integer) If (x > 1) Then imprimir(x - 1) Console.Write("{0} ", x) End Sub Sub main() Dim num As Integer Console.Write("Ingrese un Numero ")

Page 14: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 14 -

num = CInt(Console.ReadLine()) imprimir(num) Console.ReadLine() End SubEnd Module

Al cambiar el orden se obtieneSub imprimir(ByVal x As Integer) Console.Write("{0} ", x) If (x > 1) Then imprimir(x - 1) End Sub

8 Dibujar una línea con recursividad

REM Dibujar una línea con recursividadModule Module1 Function distancia(ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single) As Single distancia = CSng(Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2))) End Function Sub linea(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer) Dim cx, cy As Integer Console.SetCursorPosition(x1, y1) Console.Write("*") Console.SetCursorPosition(x2, y2) Console.Write("*") If distancia(x1, y1, x2, y2) >= 2 Then cx = (x1 + x2) / 2 cy = (y1 + y2) / 2 linea(x1, y1, cx, cy) linea(cx, cy, x2, y2) End If End Sub Sub Main() Dim x1, y1, x2, y2, x3, y3 As Integer

Page 15: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 15 -

x1 = 20 : y1 = 22 x2 = 40 : y2 = 2 x3 = 60 : y3 = 22 Console.ForegroundColor = 9 linea(x1, y1, x2, y2) Console.ForegroundColor = 10 linea(x2, y2, x3, y3) Console.ForegroundColor = 12 linea(x3, y3, x1, y1) Console.ReadLine() End SubEnd Module

5.4. EJEMPLOS SOBRE MATRICES PAG 321. (metodos numéricos con arreglosCADENAS ESTRUCTURAS Y ARCHIVOS

2. Ejemplo de archivos a) generar los valores de una función y mostrarlos en Excel Elaborar tabla de funciones trigonométricas en grados grabarlos para exportalos a excel

Page 16: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 16 -

Option Explicit OnOption Strict OnImports System.IOModule MODULE1 Function gradosRad(ByVal grados As Single) As Single gradosRad = CSng(grados * Math.PI / 180) End Function Sub Main() Dim grados As Single, rad As Single Dim contador As Integer = 1 grados = 5 Console.WriteLine(" {0,4} {1,6} {2,8} {3,8} {4,8} {5,8}", "NRO", "GRADOS", "RADIANES", "SENO", "COSENO", "TANGENTE") For grados = 0 To 360 Step 5 rad = gradosRad(grados) Console.WriteLine(" {0,4:d} {1,6:F2} {2,8:F4} {3,8:F4} {4,8:F4} {5,8:F4} ", contador, grados, rad, Math.Sin(rad), Math.Cos(rad), Math.Tan(rad)) contador += 1 Next Console.ReadLine() REM grabar los datos para exportarlos a excel Dim NombreArchivo As String = "E:\DATOS\TRIGONOTRIA.txt" Dim swEscritor As StreamWriter contador = 0 swEscritor = New StreamWriter(NombreArchivo) grados = 5 swEscritor.WriteLine(" {0,4} {1} {2,6} {3} {4,8} {5} {6,8} {7}{8,8}{9} {10,10}", "NRO", Chr(9), "GRADOS", Chr(9), "RADIANES", Chr(9), "SENO", Chr(9), "COSENO", Chr(9), "TANGENTE") For grados = 0 To 360 Step 5 rad = gradosRad(grados) swEscritor.WriteLine("{0,4:d3} {1} {2,6:f2} {3} {4,8:f4} {5} {6,8:F4} {7}{8,8:F4}{9} {10,10:F4} ", contador, Chr(9), grados, Chr(9), rad, Chr(9), Math.Sin(rad), Chr(9), Math.Cos(rad), Chr(9), Math.Tan(rad)) contador += 1 Next swEscritor.Close() Console.WriteLine(" Archivo {0} grabado satisfactoriamente", NombreArchivo) Console.ReadLine()

Page 17: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 17 -

End SubEnd Module

DICCIONARIO. Este diccionario ha sido bajado de internet , se ha hecho el program en visual basic modo consola y modo formulario

Imports System.IOModule Module1 Public A(25000, 2) As String Public nf As Integer Sub LeerArchivo(A(,) As String, ByRef nf As Integer) Dim srLector As StreamReader = New StreamReader("e:\DATOS\DIC1.txt") Dim Linea As String Dim cont As Integer = 0 Dim pos As Integer Linea = srLector.ReadLine() Do While Not (Linea Is Nothing) pos = InStr(1, Linea, Chr(9))

Page 18: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 18 -

A(cont, 0) = Mid(Linea, 1, pos - 1) A(cont, 1) = Mid(Linea, pos + 1, Len(Linea)) cont = cont + 1 Linea = srLector.ReadLine() Loop nf = cont End Sub Sub BuscarInglesCastellano(A(,) As String, Ingles As String, nf As Integer) Dim i As Integer Dim largo As Integer Dim cont1 As Integer = 0 For i = 0 To nf - 1 largo = InStr(A(i, 0), Ingles) If largo > 0 Then Console.WriteLine(" {0}===> {1} ====>{2}", cont1, A(i, 0), A(i, 1)) cont1 = cont1 + 1 End If Next Console.WriteLine("Palabras encontradas{0}", cont1) End Sub Sub BuscarCastellanoIngles(A(,) As String, Castellano As String, nf As Integer) Dim i As Integer Dim largo As Integer Dim cont1 As Integer = 0 For i = 0 To nf - 1 largo = InStr(A(i, 1), Castellano) If largo > 0 Then Console.WriteLine(" {0}===> {1} ====>{2}", cont1, A(i, 1), A(i, 0)) cont1 = cont1 + 1 End If Next Console.WriteLine("Palabras encontradas{0}", cont1) End Sub

Sub Main() LeerArchivo(A, nf) Dim opcion As Integer Dim Ingles, Castellano As String Do Console.WriteLine("1. Ingles castellano 2. Castellano Ingles 3.Salir ") Console.Write("ingrese opcion ") opcion = Console.ReadLine() Select Case opcion Case 1 Console.WriteLine("Ingrese palabra en Inglés ") Ingles = Console.ReadLine BuscarInglesCastellano(A, Ingles, nf) Case 2 Console.WriteLine("Ingrese palabra en Castellano ") Castellano = Console.ReadLine BuscarCastellanoIngles(A, Castellano, nf) End Select

Page 19: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 19 -

Loop While opcion <> 3 End SubEnd Module

Practicas de estructuras y archivos mover un ser con las teclas direccionales

Module Module2 Const maxcol As Integer = 10 Public Structure punto Dim nro As Integer Dim color As Integer Dim caracter As Integer Dim x As Single Dim y As Single End Structure Public A(maxcol) As punto Sub ImprimirSer(ByVal A() As punto, ByVal n As Integer) Dim fila As Integer For fila = 0 To n - 1 Console.BackgroundColor = CType(A(fila).color, ConsoleColor) Console.ForegroundColor = CType(A(fila).color, ConsoleColor) Console.SetCursorPosition(A(fila).x, A(fila).y) Console.Write("{0}", Chr(A(fila).caracter)) Next End SubEnd Module

Imports System.IOModule Module1 Dim cki As ConsoleKeyInfo Sub main() Dim n As Integer = 8, fila As Integer Dim cx As Integer = 40 Dim cy As Integer = 12 A(0).x = 40 : A(0).y = 10 : A(0).color = 9 : A(0).caracter = 219 A(1).x = 39 : A(1).y = 10 : A(1).color = 10 : A(1).caracter = 219 A(2).x = 38 : A(2).y = 10 : A(2).color = 12 : A(2).caracter = 219 A(3).x = 37 : A(3).y = 10 : A(3).color = 13 : A(3).caracter = 219 A(4).x = 36 : A(4).y = 10 : A(4).color = 14 : A(4).caracter = 177 A(5).x = 35 : A(5).y = 10 : A(5).color = 15 : A(5).caracter = 178

Page 20: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 20 -

A(6).x = 34 : A(6).y = 10 : A(6).color = 11 : A(6).caracter = 177 A(7).x = 33 : A(7).y = 10 : A(7).color = 7 : A(7).caracter = 220 Call ImprimirSer(A, n) cx = CInt(A(0).x) cy = CInt(A(0).y) Do cki = Console.ReadKey() If cki.Key = ConsoleKey.LeftArrow And cx > 1 Then cx = cx - 1 If cki.Key = ConsoleKey.RightArrow And cx < 79 Then cx = cx + 1 If cki.Key = ConsoleKey.UpArrow And cy > 1 Then cy = cy - 1 If cki.Key = ConsoleKey.DownArrow And cy > 1 Then cy = cy + 1 ' modificamos el ser For fila = n - 1 To 1 Step -1 A(fila).x = A(fila - 1).x A(fila).y = A(fila - 1).y Next A(0).x = cx A(0).y = cy REM System.Threading.Thread.Sleep(20) Console.BackgroundColor = 0 Console.Clear() Call ImprimirSer(A, n) Loop While cki.Key <> ConsoleKey.Escape End SubEnd Module

Modificación del programa

Module Module2 Const maxcol As Integer = 10 Public coordX As Integer = 40 Public coordY As Integer = 12 Public Structure punto Dim nro As Integer Dim color As Integer Dim caracter As Integer Dim x As Single Dim y As Single

Page 21: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 21 -

End Structure Public A(maxcol) As punto Sub Cuadro(cx As Integer, cy As Integer, lado As Integer) Dim fila, col As Integer For fila = 0 To lado - 1 For col = 0 To lado - 1 Console.SetCursorPosition(cx - lado / 2 + col, cy - lado / 2 + fila) Console.Write("*") Next Next 'Console.SetCursorPosition(cx, cy) 'Console.Write("X") End Sub

Sub ImprimirSer(ByVal A() As punto, ByVal n As Integer, lado As Integer) Dim fila As Integer For fila = 0 To n - 1 Console.BackgroundColor = CType(A(fila).color, ConsoleColor) Console.ForegroundColor = CType(A(fila).color, ConsoleColor) Cuadro(coordX + A(fila).x * lado, coordY + A(fila).y * lado, lado) Next End SubEnd Module

CODIGO DEL MODULO 1

Imports System.IOModule Module1 Dim cki As ConsoleKeyInfo Sub main() Dim ex As Integer = 4 Dim cx As Integer = 0 Dim cy As Integer = 0 Dim n As Integer = 8, fila As Integer A(0).x = 0 : A(0).y = 0 : A(0).color = 9 : A(0).caracter = 219 A(1).x = -1 : A(1).y = 0 : A(1).color = 10 : A(1).caracter = 219 A(2).x = -2 : A(2).y = 0 : A(2).color = 12 : A(2).caracter = 219 A(3).x = -3 : A(3).y = 0 : A(3).color = 13 : A(3).caracter = 219 A(4).x = -4 : A(4).y = 0 : A(4).color = 14 : A(4).caracter = 177 A(5).x = -5 : A(5).y = 0 : A(5).color = 15 : A(5).caracter = 178 A(6).x = -6 : A(6).y = 0 : A(6).color = 11 : A(6).caracter = 177 A(7).x = -7 : A(7).y = 0 : A(7).color = 7 : A(7).caracter = 220 Call ImprimirSer(A, n, ex) cx = CInt(A(0).x) cy = CInt(A(0).y) Dim valor As Integer = 0 Do valor = 0 cki = Console.ReadKey() If cki.Key = ConsoleKey.LeftArrow And cx > -Int(38 / ex) Then cx = cx - 1

Page 22: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 22 -

valor = 1 End If If cki.Key = ConsoleKey.RightArrow And cx < Int(38 / ex) Then cx = cx + 1 valor = 1 End If If cki.Key = ConsoleKey.UpArrow And cy > -Int(10 / ex) Then cy = cy - 1 valor = 1 End If If cki.Key = ConsoleKey.DownArrow And cy < Int(10 / ex) Then cy = cy + 1 valor = 1 End If ' modificamos el ser solo si es posible If valor = 1 Then For fila = n - 1 To 1 Step -1 A(fila).x = A(fila - 1).x A(fila).y = A(fila - 1).y Next A(0).x = cx A(0).y = cy End If REM System.Threading.Thread.Sleep(20) Console.BackgroundColor = 0 Console.Clear() Call ImprimirSer(A, n, ex) Loop While cki.Key <> ConsoleKey.Escape End SubEnd Module

El mismo ejercicio en modo formulario

Public Class Form1

Page 23: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 23 -

Public brocha As SolidBrush Dim Grafico As Graphics Dim coordX As Integer = 200 Dim CoordY As Integer = 200 Const maxcol As Integer = 20 Dim ancho As Integer = 20 Dim AnchoFigura As Integer = 500 Dim AltoFigura As Integer = 400 Dim alto As Integer = 20 Dim cx As Integer = 0 Dim cy As Integer = 0 Dim n As Integer = 15 Private Structure punto Dim nro As Integer Dim rojo As Integer Dim verde As Integer Dim azul As Integer Dim caracter As Integer Dim x As Single Dim y As Single End Structure Dim A(maxcol) As punto Private Sub ImprimirSer(ByVal n As Integer, ancho As Integer, alto As Integer) Dim fila, rojo, verde, azul As Integer For fila = 0 To n - 1 rojo = A(fila).rojo verde = A(fila).verde azul = A(fila).azul brocha.Color = Drawing.Color.FromArgb(rojo, verde, azul) Grafico.FillRectangle(brocha, coordX + A(fila).x * ancho, CoordY + A(fila).y * alto, ancho, alto) Next End Sub Sub iniciar() A(0).x = 0 : A(0).y = 0 : A(0).rojo = 0 : A(0).verde = 0 : A(0).azul = 255 : A(0).caracter = 219 ' azul A(1).x = -1 : A(1).y = 0 : A(1).rojo = 0 : A(1).verde = 255 : A(1).azul = 0 : A(1).caracter = 219 ' verde A(2).x = -2 : A(2).y = 0 : A(2).rojo = 255 : A(2).verde = 0 : A(2).azul = 0 : A(2).caracter = 219 ' rojol A(3).x = -3 : A(3).y = 0 : A(3).rojo = 255 : A(3).verde = 255 : A(3).azul = 0 : A(3).caracter = 219 ' amarillo A(4).x = -4 : A(4).y = 0 : A(4).rojo = 0 : A(4).verde = 255 : A(4).azul = 255 : A(4).caracter = 219 ' celeste A(5).x = -5 : A(5).y = 0 : A(5).rojo = 255 : A(5).verde = 0 : A(5).azul = 255 : A(5).caracter = 219 ' violeta A(6).x = -6 : A(6).y = 0 : A(6).rojo = 255 : A(6).verde = 255 : A(6).azul = 255 : A(6).caracter = 219 ' blanco A(7).x = -7 : A(7).y = 0 : A(7).rojo = 255 : A(7).verde = 155 : A(7).azul = 155 : A(7).caracter = 219 ' naranja A(8).x = -8 : A(8).y = 0 : A(8).rojo = 155 : A(8).verde = 155 : A(8).azul = 155 : A(8).caracter = 219 ' gris claro

Page 24: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 24 -

A(9).x = -9 : A(9).y = 0 : A(9).rojo = 80 : A(9).verde = 80 : A(9).azul = 80 : A(9).caracter = 219 ' gris oscuro A(10).x = -10 : A(10).y = 0 : A(10).rojo = 100 : A(10).verde = 0 : A(10).azul = 0 : A(10).caracter = 219 ' rojo 1 A(11).x = -11 : A(11).y = 0 : A(11).rojo = 150 : A(11).verde = 0 : A(11).azul = 0 : A(11).caracter = 219 ' rojo 2 A(12).x = -12 : A(12).y = 0 : A(12).rojo = 200 : A(12).verde = 0 : A(12).azul = 0 : A(12).caracter = 219 ' rojo3 A(13).x = -13 : A(13).y = 0 : A(13).rojo = 250 : A(13).verde = 0 : A(13).azul = 0 : A(13).caracter = 219 ' rojo 4 A(14).x = -14 : A(14).y = 0 : A(14).rojo = 155 : A(14).verde = 100 : A(14).azul = 12 : A(14).caracter = 219 ' rojo 5 End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load brocha = New SolidBrush(Color.FromArgb(0, 255, 0)) PictureBox1.Width = AnchoFigura PictureBox1.Height = AltoFigura Grafico = PictureBox1.CreateGraphics iniciar() End Sub Private Sub BtnIniciar_Click(sender As Object, e As EventArgs) Handles BtnIniciar.Click Call ImprimirSer(n, ancho, alto) End Sub

Private Sub TextBox1_KeyDown_1(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown cx = CInt(A(0).x) cy = CInt(A(0).y) Dim valor As Integer = 0 Select Case e.KeyCode Case Keys.Left If cx > -AnchoFigura / (ancho * 2) + 3 Then cx = cx - 1 valor = 1 End If Case Keys.Right If cx < AnchoFigura / (ancho * 2) - 1 Then cx = cx + 1 valor = 1 End If Case Keys.Up If cy > -AltoFigura / (alto * 2) + 1 Then cy = cy - 1 valor = 1 End If Case Keys.Down If cy < AltoFigura / (alto * 2) - 1 Then cy = cy + 1 valor = 1 End If End Select

Page 25: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 25 -

' modificamos el ser solo si es posible If valor = 1 Then For fila = n - 1 To 1 Step -1 A(fila).x = A(fila - 1).x A(fila).y = A(fila - 1).y Next A(0).x = cx A(0).y = cy End If Grafico.Clear(Color.Black) Call ImprimirSer(n, ancho, alto) End SubEnd Class

Modifique el programa que en vez de cuadro sea una figura

REALIZAR EJERCICIOS ADICIONALES

Page 26: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 26 -

Elaborar una aplicación con las siguientes opciones

1. Generar.- se tiene una matriz de n filas por m columnas que representa las alturas en un espacio 3d al principio todos las altura son cero , se generar un nro aleatorio de filas y columnas entonces la celda A(rx,ry) crece en 1 y asi sucesivamente hasta llegar a altura máxima de 15 , si la altura es mayor que 15 el elemento A(ry,yx) se vuelve a cero

2. Mostrar. Mostrar la matriz generada usando colores de acuerdo a las alturas3. Recuperar. Recuperar un archivo grabado de Excel 4. Grabar .- graba el contenido de la matriz en un archivo de texto con

delimitadores para Excel5. Simular. Simula las variaciones de las celdas de la matriz 3D un numero

determinado de veces6. Función. Genera los valores de una función y lo carga a la matriz7. Sumar.- un ser camina aleatoriamente por la matriz y va haciendo crecer de 1

en 1 las alturas 8. Restar. un ser camina aleatoriamente por la matriz y va haciendo decrecer de

1 en 1 las alturas9. Salir

Page 27: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 27 -

Código del modulo 2

Imports System.IOModule Module2 Public A(21, 21) As Integer Public nf As Integer = 10, nc As Integer = 10 Public nveces As Integer Public NombreArchivo As String = "E:\DATOS\DATOS3D.txt" Public tipo As Integer = 1 ' 1 suma 2 resta Public maximo As Integer = 15 Public minimo As Integer = 0 Public px As Integer = 5 ' posicion inicial del ser Public py As Integer = 5 ' fila inicial del ser Function menu() As Integer Dim opcion As Integer Console.ForegroundColor = 15 Console.Clear() Console.SetCursorPosition(1, 1) Console.Write("1. Generar 2: Mostrar 3. Recuperar 4. Grabar 5. Simular 6 funcion ") Console.SetCursorPosition(1, 2)

Page 28: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 28 -

Console.Write("7. Sumar 8 Restar 8 Salir == > Ingrese opcion ") opcion = Console.ReadLine Return opcion End Function

Function f(x As Single, y As Single) As Single Dim funcion As Single = Math.Pow(x, 2) + Math.Pow(y, 2) Dim valor As Single If (funcion <> 0) Then valor = Math.Sin(funcion) / funcion Else valor = 1 End If Return valor End Function

Sub GenerarMatriz(A(,) As Integer, nf As Integer, nc As Integer, nveces As Integer) Dim rx, ry As Integer Dim i As Integer For i = 1 To nveces rx = Int(Rnd() * nc) ry = Int(Rnd() * nf) If rx < nc And ry < nf Then If (A(ry, rx) < 15) Then A(ry, rx) = A(ry, rx) + 1 Else A(ry, rx) = 0 End If End If Next End Sub Sub GenerarFuncion(A(,) As Integer, nf As Integer, nc As Integer) Dim lix As Single = -4 Dim lsx As Single = 4 Dim liy As Single = -4 Dim lsy As Single = 4 Dim dx As Single = (lsx - lix) / nc Dim dy As Single = (lsy - liy) / nf Dim x, y, z As Single y = liy For fila = 0 To nf - 1 x = lix For col = 0 To nc - 1 z = f(x, y) A(fila, col) = Int(z * 12) + 3 x = x + dx Next y = y + dy Next End Sub

Page 29: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 29 -

Sub generarSumaResta(A(,) As Integer, ByRef px As Integer, ByRef py As Integer, nf As Integer, nc As Integer, tipo As Integer) Dim r As Integer Dim valor As Integer = 0 r = Int(Rnd() * 4) Select Case r Case 0 If px < nc - 1 Then px = px + 1 valor = 1 End If Case 1 If px > 0 Then px = px - 1 valor = 1 End If Case 2 If py < nf - 1 Then py = py + 1 valor = 1 End If Case 3 If py > 0 Then py = py - 1 valor = 1 End If End Select If valor = 1 Then Select Case tipo Case 1 ' si es suma If A(py, px) < maximo Then A(py, px) = A(py, px) + 1 Else A(py, px) = minimo End If Case 2 ' si es resta If A(py, px) > minimo Then A(py, px) = A(py, px) - 1 Else A(py, px) = maximo End If End Select End If End Sub Sub Iniciar(A(,) As Integer, nf As Integer, nc As Integer, valor As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 A(fila, col) = valor Next Next End Sub

Page 30: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 30 -

Sub MostrarMatriz(cx As Integer, cy As Integer, A(,) As Integer, nf As Integer, nc As Integer, ex As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 Console.SetCursorPosition(cx + col * ex, cy + fila) Console.ForegroundColor = A(fila, col) Console.Write("{0}", Hex(A(fila, col))) Next Next End Sub

Sub GrabarMatriz(Nombre As String, A(,) As Integer, nf As Integer, nc As Integer) Dim swEscritor As StreamWriter swEscritor = New StreamWriter(NombreArchivo) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 swEscritor.Write("{0}{1}", A(fila, col), vbTab) Next swEscritor.WriteLine() Next swEscritor.Close() End Sub

Sub RecuperarMatriz(ByVal nombrearchivo As String, ByVal A(,) As Integer, ByVal nf As Integer, ByVal nc As Integer) Dim srLector As StreamReader srLector = New StreamReader(nombrearchivo) Dim fila As Integer, col As Integer Dim cadena As String = "" Dim subcadena As String Dim pos As Integer = 0 Dim inicio As Integer = 1 For fila = 0 To nf - 1 cadena = srLector.ReadLine() cadena = cadena & Chr(9) inicio = 1 For col = 0 To nc - 1 pos = InStr(inicio, cadena, Chr(9)) subcadena = Mid(cadena, inicio, pos - inicio) A(fila, col) = Val(subcadena) inicio = pos + 1 Next Next srLector.Close() End SubEnd Module

CODIGO DEL MODULO 1

Module Module1

Page 31: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 31 -

Dim opcion As Integer Sub Main() Randomize() Iniciar(A, nf, nc, minimo) Do opcion = menu() Select Case opcion Case 1 ' generar la matriz Console.SetCursorPosition(1, 3) Console.Write("Nro de veces que quiere generar ") nveces = Console.ReadLine GenerarMatriz(A, nf, nc, nveces) Case 2 ' mostrar matriz MostrarMatriz(5, 5, A, nf, nc, 2) Console.ForegroundColor = 15 Console.SetCursorPosition(1, 4) Console.WriteLine("presione tecla para continuar") Console.ReadLine() Case 3 ' recuperar RecuperarMatriz(NombreArchivo, A, nf, nc) Console.ForegroundColor = 15 Console.SetCursorPosition(1, 3) Console.WriteLine(" archivo {0} recuperado satisfactoriamente", NombreArchivo) Console.SetCursorPosition(1, 4) Console.WriteLine(" presione tecla para continuar") Console.ReadLine() Case 4 ' Grabar GrabarMatriz(NombreArchivo, A, nf, nc) Console.ForegroundColor = 15 Console.SetCursorPosition(1, 3) Console.WriteLine(" archivo {0} grabado satisfactoriamente", NombreArchivo) Console.SetCursorPosition(1, 4) Console.WriteLine(" presione tecla para continuar") Console.ReadLine() Case 5 ' Simular MostrarMatriz(30, 5, A, nf, nc, 2) Console.SetCursorPosition(1, 3) Console.ForegroundColor = 15 Console.Write("Nro de veces que quiere simular ") nveces = Console.ReadLine Dim rx, ry As Integer Dim i As Integer For i = 1 To nveces ry = Int(Rnd() * nf) rx = Int(Rnd() * nc) If ry < nf And rx < nc Then If (A(ry, rx) < 15) Then A(ry, rx) = A(ry, rx) + 1 Else A(ry, rx) = 0

Page 32: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 32 -

End If End If Console.SetCursorPosition(30 + rx, 5 + ry) Console.ForegroundColor = A(ry, rx) Console.Write("{0}", Hex(A(ry, rx))) REM VerMatriz(30, 5, A, nf, nc) System.Threading.Thread.Sleep(1) Console.SetCursorPosition(1, 3) Console.Write("{0}", i) Next Case 6 ' generar funcion GenerarFuncion(A, nf, nc) Console.ForegroundColor = 15 Console.SetCursorPosition(1, 4) Console.WriteLine("presione tecla para continuar") Console.ReadLine() Case 7 ' sumar Iniciar(A, nf, nc, minimo) 'generarSumaResta(A, px, py, nf, nc, 1) MostrarMatriz(5, 5, A, nf, nc, 2) ' Console.ReadLine() Console.ForegroundColor = 15 Console.SetCursorPosition(1, 3) Console.Write("Nro de veces que quiere generar ") nveces = Console.ReadLine For i = 1 To nveces generarSumaResta(A, px, py, nf, nc, 1) MostrarMatriz(5, 5, A, nf, nc, 2) System.Threading.Thread.Sleep(1) Next Console.ReadLine() Case 8 ' Restar Iniciar(A, nf, nc, maximo) MostrarMatriz(5, 5, A, nf, nc, 2) Console.SetCursorPosition(1, 3) Console.Write("Nro de veces que quiere generar ") nveces = Console.ReadLine For i = 1 To nveces generarSumaResta(A, px, py, nf, nc, 2) MostrarMatriz(5, 5, A, nf, nc, 2) System.Threading.Thread.Sleep(1) Next Console.ReadLine() End Select Loop Until opcion = 9 End SubEnd Module

EN MODO FORMULARIO

Page 33: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 33 -

CODIGO DEL MODULO 1

Imports System.IOModule Module1 Public A(51, 51) As Integer Public nf As Integer = 50, nc As Integer = 50 Public nveces As Integer Public NombreArchivo As String = "E:\DATOS\DATOS3D.txt" Public maximo As Integer = 254 Public minimo As Integer = 0 Public cx As Integer = 10 Public cy As Integer = 10 Public px As Integer = 5 Public py As Integer = 5 Public ancho As Integer = 8 Public alto As Integer = 8

Sub generarSumaResta(A(,) As Integer, ByRef px As Integer, ByRef py As Integer, nf As Integer, nc As Integer, tipo As Integer) Dim r As Integer Dim valor As Integer = 0 r = Int(Rnd() * 4) Select Case r Case 0 If px < nc - 1 Then px = px + 1 valor = 1 End If Case 1 If px > 0 Then px = px - 1 valor = 1 End If Case 2 If py < nf - 1 Then py = py + 1

Page 34: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 34 -

valor = 1 End If Case 3 If py > 0 Then py = py - 1 valor = 1 End If End Select If valor = 1 Then Select Case tipo Case 1 ' si es suma If A(py, px) < maximo Then A(py, px) = A(py, px) + 1 Else A(py, px) = minimo End If Case 2 ' si es resta If A(py, px) > minimo Then A(py, px) = A(py, px) - 1 Else A(py, px) = maximo End If End Select End If End Sub

Function f(x As Single, y As Single) As Single Dim funcion As Single = Math.Pow(x, 2) + Math.Pow(y, 2) Dim valor As Single If (funcion <> 0) Then valor = Math.Sin(funcion) / funcion Else valor = 1 End If Return valor End Function Sub GenerarMatriz(A(,) As Integer, nf As Integer, nc As Integer, nveces As Integer) Dim rx, ry As Integer Dim i As Integer For i = 1 To nveces rx = Int(Rnd() * nc) ry = Int(Rnd() * nf) If rx < nc And ry < nf Then If (A(ry, rx) < 255) Then A(ry, rx) = A(ry, rx) + 1 Else A(ry, rx) = 0 End If End If Next End Sub

Page 35: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 35 -

Sub GrabarFuncion(NombreArchivo As String, A(,) As Integer, nf As Integer, nc As Integer) Dim Escritor As New StreamWriter(NombreArchivo) Dim valor As Integer Dim lix As Single = -4 Dim lsx As Single = 4 Dim liy As Single = -4 Dim lsy As Single = 4 Dim dx As Single = (lsx - lix) / nc Dim dy As Single = (lsy - liy) / nf Dim x, y, z As Single y = liy For fila = 0 To nf - 1 x = lix For col = 0 To nc - 1 z = f(x, y) valor = Int(z * 250) + 60 If valor < 0 Then valor = 0 If valor > 255 Then valor = 255 Escritor.Write("{0}{1}", valor, vbTab) x = x + dx Next Escritor.WriteLine() y = y + dy Next Escritor.Close() End Sub

Sub Iniciar(A(,) As Integer, nf As Integer, nc As Integer, valor As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 A(fila, col) = valor Next Next End Sub Sub GrabarMatriz(Nombre As String, A(,) As Integer, nf As Integer, nc As Integer) Dim swEscritor As StreamWriter swEscritor = New StreamWriter(NombreArchivo) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 swEscritor.Write("{0}{1}", A(fila, col), vbTab) Next swEscritor.WriteLine() Next swEscritor.Close() End Sub

Sub RecuperarMatriz(ByVal nombrearchivo As String, ByVal A(,) As Integer, ByVal nf As Integer, ByVal nc As Integer) Dim srLector As StreamReader

Page 36: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 36 -

srLector = New StreamReader(nombrearchivo) Dim fila As Integer, col As Integer Dim cadena As String = "" Dim subcadena As String Dim pos As Integer = 0 Dim inicio As Integer = 1 For fila = 0 To nf - 1 cadena = srLector.ReadLine() cadena = cadena & Chr(9) inicio = 1 For col = 0 To nc - 1 pos = InStr(inicio, cadena, Chr(9)) subcadena = Mid(cadena, inicio, pos - inicio) A(fila, col) = Val(subcadena) inicio = pos + 1 Next Next srLector.Close() End SubEnd Module

CODIGO DEL FORMULARIO

Public Class Form1 Private Sub BtnGenerar_Click(sender As Object, e As EventArgs) Handles BtnGenerar.Click nveces = Val(TextBox1.Text) GenerarMatriz(A, nf, nc, nveces) End Sub Sub MostrarMatriz(A(,) As Integer, nf As Integer, nc As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 DataGridView1.Rows(fila).Cells(col).Value = A(fila, col) Next Next End Sub Private Sub btnMostrar_Click(sender As Object, e As EventArgs) Handles BtnMostrar.Click MostrarMatriz(A, nf, nc) End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load DataGridView1.RowCount = nf DataGridView1.ColumnCount = nc For i = 0 To nc - 1 DataGridView1.Columns(i).Width = 30 Next End Sub Private Sub btnRecuperar_Click(sender As Object, e As EventArgs) Handles BtnRecuperar.Click OpenFileDialog1.ShowDialog() NombreArchivo = OpenFileDialog1.FileName

Page 37: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 37 -

RecuperarMatriz(NombreArchivo, A, nf, nc) MostrarMatriz(A, nf, nc) End Sub Private Sub BtnGrabar_Click(sender As Object, e As EventArgs) Handles BtnGrabar.Click SaveFileDialog1.ShowDialog() NombreArchivo = SaveFileDialog1.FileName GrabarMatriz(NombreArchivo, A, nf, nc) End Sub Private Sub btnSimular_Click(sender As Object, e As EventArgs) Handles BtnSimular.Click MostrarMatriz(A, nf, nc) nveces = Val(TextBox1.Text) Application.DoEvents() Dim rx, ry As Integer Dim i As Integer For i = 1 To nveces ry = Int(Rnd() * nf) rx = Int(Rnd() * nc) If ry < nf And rx < nc Then If (A(ry, rx) < 250) Then A(ry, rx) = A(ry, rx) + 1 Else A(ry, rx) = 0 End If End If Next End Sub Private Sub BtnGenerarFuncion_Click(sender As Object, e As EventArgs) Handles BtnGenerarFuncion.Click SaveFileDialog1.ShowDialog() NombreArchivo = SaveFileDialog1.FileName GrabarFuncion(NombreArchivo, A, nf, nc) End Sub Private Sub btnSimularGrafico_Click(sender As Object, e As EventArgs) Handles btnSimularGrafico.Click Dim grafico As Graphics grafico = PictureBox1.CreateGraphics Dim brocha As SolidBrush For fila = 0 To nf - 1 For col = 0 To nc - 1 brocha = New SolidBrush(Color.FromArgb(A(fila, col), 0, 0)) grafico.FillRectangle(brocha, cx + col * ancho, cy + fila * alto, ancho, alto) Next Next End Sub

Private Sub btnSuma_Click(sender As Object, e As EventArgs) Handles btnSuma.Click For i = 1 To nveces generarSumaResta(A, px, py, nf, nc, 1) MostrarMatriz(A, nf, nc)

Page 38: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 38 -

' System.Threading.Thread.Sleep(1) Next btnSimularGrafico_Click(sender, e) End Sub

Private Sub btnrestar_Click(sender As Object, e As EventArgs) Handles btnrestar.Click For i = 1 To nveces generarSumaResta(A, px, py, nf, nc, 2) MostrarMatriz(A, nf, nc) ' System.Threading.Thread.Sleep(1) Next btnSimularGrafico_Click(sender, e) End Sub

Private Sub btnIniciar_Click(sender As Object, e As EventArgs) Handles btnIniciarMaximo.Click Iniciar(A, nf, nc, maximo) MostrarMatriz(A, nf, nc) nveces = TextBox1.Text End Sub Private Sub btnIniciarMinimo_Click(sender As Object, e As EventArgs) Handles btnIniciarMinimo.Click Iniciar(A, nf, nc, minimo) MostrarMatriz(A, nf, nc) nveces = TextBox1.Text End SubEnd Class

JUEGO DE LA VIDA EN VISUAL BASIC

Archivo usado0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 00 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 00 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 00 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 1 0 00 1 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 0 0 0 0 1 00 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Page 39: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 39 -

CODIGO DEL MODULO 1

Option Explicit OnREM Option Strict OnImports System.IOModule Module1 Public cont As Integer = 0 Public Const maxfilas As Integer = 40 Public Const maxcol As Integer = 40 Public A(maxfilas, maxcol) As Integer Public nfilas As Integer = 7 Public velocidad As Integer = 10 Public ex As Integer = 16 Public ey As Integer = 16 Public srLector As StreamReader Public nf As Integer = 12 '20 ' 40 '94 ' 40 Public nc As Integer = 38 ' 20 ' 50 '80 ' 80 Public ng As Integer = 200 Public grafico As Graphics Public brocha As SolidBrush Public borrador As SolidBrush Public ancho, alto As Integer

Sub IniciarPantalla(ByVal A(,) As Integer, ByVal nf As Integer, ByVal nc As Integer, Semilla As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 A(fila, col) = Semilla Next Next End Sub Sub TranferirMatriz(ByVal A(,) As Integer, ByVal B(,) As Integer, ByVal nf As Integer, ByVal nc As Integer) Dim fila, col As Integer For fila = 1 To nf - 1 For col = 1 To nc - 1

Page 40: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 40 -

B(fila, col) = A(fila, col) Next Next End Sub Sub JuegoVida(ByVal A(,) As Integer, ByVal nf As Integer, ByVal nc As Integer) Dim B(maxfilas, maxcol) As Integer Dim fila, col, vecinos, x1, y1, x2, y2, fila1, col1 As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 vecinos = 0 If fila > 1 Then y1 = fila - 1 Else y1 = fila End If If fila < nf - 1 Then y2 = fila + 1 Else y2 = fila End If If col > 1 Then x1 = col - 1 Else x1 = col End If If col < nc Then x2 = col + 1 Else x2 = col End If For fila1 = y1 To y2 For col1 = x1 To x2 If (fila1 = fila And col1 = col) Then Continue For If A(fila1, col1) = 1 Then vecinos = vecinos + 1 Next Next Select Case vecinos Case 0 B(fila, col) = 0 Case 1 B(fila, col) = 0 Case 2 B(fila, col) = A(fila, col) Case 3 B(fila, col) = 1 Case Else B(fila, col) = 0 End Select Next Next IniciarPantalla(A, nf, nc, 0) TranferirMatriz(B, A, nf, nc)

Page 41: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 41 -

End Sub Sub RecuperarMatriz(ByVal nombrearchivo As String, ByVal A(,) As Integer, ByVal nf As Integer, ByVal nc As Integer) srLector = New StreamReader(nombrearchivo) Dim fila As Integer, col As Integer Dim cadena As String = "" Dim subcadena As String Dim pos As Integer = 0 Dim inicio As Integer = 1 For fila = 0 To nf - 1 cadena = srLector.ReadLine() cadena = cadena & Chr(9) inicio = 1 For col = 0 To nc - 1 pos = InStr(inicio, cadena, Chr(9)) subcadena = Mid(cadena, inicio, pos - inicio) A(fila, col) = CInt(Val(subcadena)) inicio = pos + 1 Next Next Console.Write("Archivo leido satisfactoriamente") srLector.Close() End SubEnd Module

CODIGO DEL FORMULARIO

Imports System.IOPublic Class Form1 Sub VerPantalla(ByVal A(,) As Integer, ByVal nf As Integer, ByVal nc As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 Select Case A(fila, col) Case 0 grafico.FillRectangle(borrador, col * ex, fila * ey, ex, ey) Case 1 grafico.FillRectangle(brocha, col * ex, fila * ey, ex, ey) Case 2 grafico.FillRectangle(Brushes.Red, col * ex, fila * ey, ex, ey) Case 3 grafico.FillRectangle(Brushes.Green, col * ex, fila * ey, ex, ey) End Select Next Next End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load grafico = PictureBox1.CreateGraphics() brocha = New SolidBrush(Color.Yellow) borrador = New SolidBrush(Color.Blue) Timer1.Enabled = False End Sub

Page 42: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 42 -

Private Sub MnuIniciar_Click(sender As Object, e As EventArgs) Handles BtnIniciar.Click cont = 0 DataGridView1.RowCount = nfilas DataGridView1.ColumnCount = 2 DataGridView1.Columns(0).Width = 100 DataGridView1.Columns(1).Width = 50 DataGridView1.Columns(0).HeaderText = "Propiedad" DataGridView1.Columns(1).HeaderText = "Valor" For i = 0 To nfilas - 1 DataGridView1.Rows(i).HeaderCell.Value = i.ToString Next DataGridView1.Rows(0).Cells(0).Value = "Ncol" DataGridView1.Rows(0).Cells(1).Value = nc DataGridView1.Rows(1).Cells(0).Value = "Nfilas" DataGridView1.Rows(1).Cells(1).Value = nf DataGridView1.Rows(2).Cells(0).Value = "Ex" DataGridView1.Rows(2).Cells(1).Value = ex DataGridView1.Rows(3).Cells(0).Value = "Ey" DataGridView1.Rows(3).Cells(1).Value = ey DataGridView1.Rows(4).Cells(0).Value = "Velocidad" DataGridView1.Rows(4).Cells(1).Value = velocidad DataGridView1.Rows(5).Cells(0).Value = "generacion" DataGridView1.Rows(5).Cells(1).Value = ng DataGridView1.Rows(6).Cells(0).Value = "cont" DataGridView1.Rows(6).Cells(1).Value = cont End Sub

Private Sub btnObtenerValores_Click(sender As Object, e As EventArgs) Handles btnObtenerValores.Click nc = DataGridView1.Rows(0).Cells(1).Value nf = DataGridView1.Rows(1).Cells(1).Value ex = DataGridView1.Rows(2).Cells(1).Value ey = DataGridView1.Rows(3).Cells(1).Value velocidad = DataGridView1.Rows(4).Cells(1).Value ng = DataGridView1.Rows(5).Cells(1).Value cont = DataGridView1.Rows(6).Cells(1).Value

End Sub Private Sub btnAbrir_Click(sender As Object, e As EventArgs) Handles BtnAbrir.Click Dim nombre1 As String OpenFileDialog1.ShowDialog() REM OpenFileDialog1.Filter = "*.TXT" nombre1 = OpenFileDialog1.FileName IniciarPantalla(A, maxfilas, maxcol, 0) RecuperarMatriz(nombre1, A, nf, nc) VerPantalla(A, maxfilas, maxcol) End Sub

Private Sub btnBorrar_Click(sender As Object, e As EventArgs) Handles btnBorrar.Click grafico.Clear(Color.Black)

Page 43: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 43 -

End Sub Private Sub btnJugar_Click(sender As Object, e As EventArgs) Handles btnJugar.Click REM grafico.Clear(Color.Black) If cont < ng Then JuegoVida(A, maxfilas, maxcol) VerPantalla(A, maxfilas, maxcol) DataGridView1.Rows(6).Cells(1).Value = cont cont = cont + 1 Else Dim MiFuente As New Font("Verdana", 24, FontStyle.Bold) Dim Brocha As New SolidBrush(Color.BurlyWood) grafico.DrawString("juego terminado GDI+", MiFuente, Brocha, 100, 300) End If System.Threading.Thread.Sleep(velocidad) ' 1 segundo End Sub Private Sub btnVerPantalla_Click(sender As Object, e As EventArgs) Handles btnVerPantalla.Click VerPantalla(A, nf, nc) End Sub Private Sub btnSimular_Click(sender As Object, e As EventArgs) Handles btnSimular.Click Timer1.Interval = velocidad Timer1.Enabled = True End Sub Private Sub BtnDetener_Click(sender As Object, e As EventArgs) Handles BtnDetener.Click Timer1.Enabled = False End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick btnJugar_Click(sender, e) End SubEnd Class

PRACTICAS DEL MARTES 21 DE ABRIL TURNO 19-21 HORAS

1. HOLA MUNDOModule Module1 Sub Main() Console.WriteLine("HOLA") Console.ReadLine() End SubEnd Module

EJERCICIO 2

Module Module1 Dim N1, N2 As Single Sub Main() Randomize() Console.Write("INGRESE PRIMER NUMERO")

Page 44: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 44 -

N1 = Console.ReadLine() Console.Write("INGRESE SEGUNDO NUMERO") N2 = Console.ReadLine() Console.WriteLine("la suma es {0} ", N1 + N2) Console.WriteLine("la resta es {0} ", N1 - N2) Console.WriteLine("la multiplicacion es {0} ", N1 * N2) Console.WriteLine("la division entera es {0} ", N1 \ N2) Console.WriteLine("la division reales {0} ", N1 / N2) Console.WriteLine("el modulo es {0} ", N1 Mod N2) Console.WriteLine("LA potencia de {0} elevado a {1} es = {2} ", N1, N2, Math.Pow(N1, N2)) Console.WriteLine("LA raiz de {0} es {1} ", N1, Math.Sqrt(N1)) Console.WriteLine("numero aleatorio {0} ", Int(Rnd() * 20)) Console.ReadLine() End SubEnd Module

PRACTICAS DE LABORATORIO 23 DE ABRIL DEL 2015JUEVES TURNO 9-11

Pase de parametros por valor y por referencia

Option Explicit OnOption Strict OnModule Module1 Sub cambiar(ByRef a As Single, ByRef b As Single) Dim temp As Single temp = a a = b b = temp End Sub Sub main() Dim x As Single = 10, y As Single = 50 Console.WriteLine("pase de parametros por valor antes x= {0} y ={1} ", x, y) cambiar(x, y) Console.WriteLine("Despues x= {0} y ={1} ", x, y) Console.ReadLine() End SubEnd Module

Page 45: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 45 -

Ejercicio 2

Module Module1 Dim a, b As Single Dim n As Integer Sub ingresar(ByRef a As Single, ByRef b As Single, ByRef n As Integer) Console.Write("ingrese li ") a = Console.ReadLine() Console.Write("ingrese ls ") b = Console.ReadLine() Console.Write("ingrese ni ") n = Console.ReadLine() End Sub Sub main() Console.WriteLine("Antes a= {0} b ={1} c ={2}", a, b, n) ingresar(a, b, n) Console.WriteLine("Despues a= {0} b ={1} c ={2}", a, b, n) Console.ReadLine() End SubEnd Module

Ejercicio 3 Se desarrollo el ejercicio de manejo de teclas,Ejercicio de nros aleatoriosEjercicio 4. Desarrollo de la aplicacion pag 26

Module Module1 Public A(21, 21) As Integer Public nf As Integer = 8, nc As Integer = 8 Sub Iniciar(A(,) As Integer, nf As Integer, nc As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 A(fila, col) = Int(Rnd() * 15) Next Next End Sub Sub MostrarMatriz(cx As Integer, cy As Integer, A(,) As Integer, nf As Integer, nc As Integer, ex As Integer) Dim fila, col As Integer

Page 46: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 46 -

For fila = 0 To nf - 1 For col = 0 To nc - 1 Console.SetCursorPosition(cx + col * ex, cy + fila) Console.ForegroundColor = A(fila, col) Console.Write("{0}", Hex(A(fila, col))) Next Next End Sub Sub Main() Iniciar(A, nf, nc) MostrarMatriz(10, 5, A, nf, nc, 3) Console.ReadLine() End SubEnd Module

PRACTICA JUEVES 23 ABRIL DEL 2015 TURNO DE 11 A 13 HORASPase de parametros por referencia

Option Explicit OnOption Strict OffModule Module1 Sub Ingresar(ByRef a As Single, ByRef b As Single, ByRef n As Integer) Console.Write("ingrese li") a = CInt(Console.ReadLine()) Console.Write("ingrese ls") b = Console.ReadLine() Console.Write("ingrese nd") n = Console.ReadLine() End Sub Sub main() Dim a, b As Single Dim n As Integer Console.WriteLine(" antes a = {0} b {1} n{2}", a, b, n) Ingresar(a, b, n) Console.WriteLine(" despues a = {0} b {0,8:f2} n {2}", a, b, n) Console.ReadLine() End SubEnd Module

Page 47: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 47 -

Dibujar un cuadroModule Module1 Sub Main() Dim Ancho As Integer = 14 Dim alto As Integer = 5 Dim px As Integer = 20 Dim py As Integer = 2 Dim color1 As Integer = 9 Dim color2 As Integer = 12 Cuadro(px, py, Ancho, alto, color1, color2) Cuadro(40, 2, 20, 22, 14, 6) Cuadro(50, 5, 8, 12, 9, 9) Console.ReadLine() End Sub

End ModuleUsando la linea recursiva dibuje un cuadrilátero

REM Dibujar una línea con recursividadModule Module1 Sub Main() Console.ForegroundColor = 14 Dim x1 As Integer = 5 Dim y1 As Integer = 5 Dim x2 As Integer = 40 Dim y2 As Integer = 5 Dim x3 As Integer = 50 Dim y3 As Integer = 20 Dim x4 As Integer = 5 Dim y4 As Integer = 20 Dim cx, cy As Integer linea(x1, y1, x2, y2)

Page 48: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 48 -

linea(x2, y2, x3, y3) linea(x3, y3, x4, y4) linea(x4, y4, x1, y1) cx = (x1 + x2 + x3 + x4) / 4 cy = (y1 + y2 + y3 + y4) / 4 Console.ForegroundColor = 10 Console.SetCursorPosition(cx, cy) Console.Write("C") Console.ReadLine() End SubEnd Module

Ejercicio mostrar una matriz generada aleatoriamente

Module Module1 Public A(21, 21) As Integer Public nf As Integer = 15, nc As Integer = 15 Public nveces As Integer Sub Iniciar(A(,) As Integer, nf As Integer, nc As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 A(fila, col) = Int(Rnd() * 15) Next Next End Sub Sub MostrarMatriz(cx As Integer, cy As Integer, A(,) As Integer, nf As Integer, nc As Integer, ex As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 Console.SetCursorPosition(cx + col * ex, cy + fila) Console.ForegroundColor = A(fila, col) Console.Write("{0}", Hex(A(fila, col))) Next Next End Sub Sub Main() Randomize() Iniciar(A, nf, nc)

Page 49: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 49 -

MostrarMatriz(1, 5, A, nf, nc, 3) Console.ReadLine() End SubEnd Module

RECUPERACION DE LABORATORIO LUNES 27 ABRIL DE 11 13 HORAS

P1 HOLA MUNDO

Module Module1 Sub Main() Console.WriteLine(" HOLA MUNDO") Console.ReadLine() End SubEnd Module

Ejercicio 2 suma de dos numerous enteros

Option Explicit OnModule Module1 Dim n1, n2, suma As Integer Sub Main() Console.Write(" Ingrese nro 1") n1 = Console.ReadLine() Console.Write(" Ingrese nro 2") n2 = Console.ReadLine() suma = n1 + n2 Console.WriteLine("la suma es {0}", suma) Console.ReadLine() End SubEnd Module

Ejercicio 3 , Operaciones matemáticas

Option Explicit OnModule Module1 Dim n1, n2, suma As Single Sub Main() Randomize() 'comentario Console.Write(" Ingrese nro 1") n1 = Console.ReadLine() Console.Write(" Ingrese nro 2") n2 = Console.ReadLine() Console.WriteLine("la suma es {0}", n1 + n2) Console.WriteLine("la resta es {0}", n1 - n2) Console.WriteLine("la multiplicacion es {0}", n1 * n2) Console.WriteLine("la division entera|es {0}", n1 \ n2) Console.WriteLine("la division reals {0,8:f3}", n1 / n2) Console.WriteLine("el modulo es {0}", n1 Mod n2) Console.WriteLine("la{0} elevado a {1} es ={2}", n1, n2, Math.Pow(n1, n2)) Console.WriteLine("numero alatario de 50 a 100={0}", 50 + Int(Rnd() * 50))

Page 50: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 50 -

Console.ReadLine() End SubEnd Module

Ejercicio 4. Ejemplo del for

Option Explicit On'Option Strict OnModule Module1 Sub Main() Dim i As Single = 1 Dim suma As Single = 0 Dim n As Integer = 5 For i = 1 To n Step 0.5 Console.Write("{0} ", i) suma += i Next Console.WriteLine() Console.WriteLine(" la suma es {0} ", suma) Console.ReadLine() End SubEnd Module

Page 51: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 51 -

Option Explicit OnModule Module1 Sub Main() Dim i As Single = 1 Dim suma As Single = 0 Dim n As Integer = 5 For i = 1 To n Step 0.5 Console.WriteLine("{0,8:f4}", i) suma += i Next Console.WriteLine() Console.WriteLine(" la suma es {0} ", suma) Console.ReadLine() End SubEnd Module

Ejercicio 5. Pase de parámetros por valor y por referencia

Option Explicit OnOption Strict OnModule Module1 Sub cambiar(ByRef a As Single, ByRef b As Single) Dim temp As Single temp = a a = b b = temp End Sub Sub main() Dim x As Single = 10, y As Single = 50 Console.WriteLine("Antes antes x= {0} y ={1} ", x, y) cambiar(x, y) Console.WriteLine("Despues x= {0} y ={1} ", x, y) Console.ReadLine() End SubEnd Module

Ejemplo de parametros por referencia

Option Explicit OnOption Strict OnModule Module1 Sub ingresar(ByRef a As Single, ByRef b As Single, ByRef ndiv As Integer) Console.Write(" ingrese a ") a = CSng(Console.ReadLine()) Console.Write(" ingrese b ") b = CSng(Console.ReadLine()) Console.Write(" ingrese n ") ndiv = CInt(Console.ReadLine()) End Sub

Sub main() Dim li, ls As Single

Page 52: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 52 -

Dim ndiv As Integer Console.WriteLine("Antes antes a= {0} b ={1} n{2}", li, ls, ndiv) ingresar(li, ls, ndiv) Console.WriteLine("Despues a= {0} b ={1} n{2}", li, ls, ndiv) Console.ReadLine() End SubEnd Module

PRACTICAS MARTES 28 ABRIL DEL 2015 TURNO 17-19 HORASFUNCIONESEjercicio 1 combinatoria

Option Explicit OnREM Option Strict OnModule Module1 Function fact(ByVal N As Integer) As Integer Dim f = 1, i As Integer For i = 1 To N f = f * i Next Return f End Function Sub Main() Dim n, r, combi As Integer Console.Write("Ingrese un n ") n = CInt(Console.ReadLine()) Console.Write("Ingrese r ") r = CInt(Console.ReadLine()) Console.WriteLine("El factorial de {0} es {1} ", n, fact(n)) combi = CInt(fact(n)) / (CInt(fact(r)) * CInt(fact(n - r))) Console.WriteLine("la combinatoria de {0} en {1} es ={2} ", n, r, combi) Console.ReadLine() End SubEnd Module

Ejercicio 2 cuadros de coloresModule Module1 Sub Main()

Page 53: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 53 -

Dim Ancho As Integer = 14 Dim alto As Integer = 5 Dim px As Integer = 20 Dim py As Integer = 2 Dim color1 As Integer = 9 Dim color2 As Integer = 12 'Cuadro(px, py, Ancho, alto, color1, color1) 'Cuadro(1, 1, 20, 20, 10, 2) 'Cuadro(60, 1, 10, 20, 7, 8) While (1) For I = 1 To 40 Console.BackgroundColor = 0 Console.Clear() Cuadro(I, 1, Ancho, alto, 14, 14) System.Threading.Thread.Sleep(10) ' 1 segundo Next End While Console.ReadLine() End SubEnd Module

Pase de parametros por valor y por referencia

Option Explicit On'Option Strict OnModule Module1

Page 54: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 54 -

Sub INGRESAR(ByRef a As Single, ByRef b As Single, ByRef ndiv As Integer) Console.Write("ingrese li ") a = Console.ReadLine() Console.Write("ingrese ls ") b = Console.ReadLine() Console.WriteLine("ingrese n") ndiv = Console.ReadLine() End Sub Sub main() Dim li As Single = 0, ls As Single = 50 Dim n As Integer Console.WriteLine("Antes li = {0} ls = {1} ndiv ={2} ", li, ls, n) INGRESAR(li, ls, n) Console.WriteLine("despues li = {0} ls = {1} ndiv ={2} ", li, ls, n) Console.ReadLine() End SubEnd Module

Dibujar una figura como la mostrada usando la function linea

REM Dibujar una línea con recursividadModule Module1 Sub Main() Dim x1 As Integer = 10 Dim y1 As Integer = 20 Dim x2 As Integer = 40 Dim y2 As Integer = 2 Dim x3 As Integer = 60 Dim y3 As Integer = 20 Dim cx As Integer = (x1 + x2 + x3) / 3 Dim cy As Integer = (y1 + y2 + y3) / 3 linea(x1, y1, x2, y2) linea(x2, y2, x3, y3) linea(x3, y3, x1, y1) Console.SetCursorPosition(cx, cy)

Page 55: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 55 -

Console.Write("X") Console.ForegroundColor = 10 linea(x1, y1, cx, cy) linea(x2, y2, cx, cy) linea(x3, y3, cx, cy) Console.ReadLine() End SubEnd Module

PRACTICA SI 20015 TURNO 19-21 HORAS MARTES 28 DE ABRIL DEL 20015

Parametros por referencia

Module Module1 Dim x, y As Single Dim n1 As Integer = 10 Sub ingresar(ByRef a As Single, ByRef b As Single, ByRef n As Integer) Console.Write("ingrese li ") a = Console.ReadLine() Console.Write("ingrese ls ") b = Console.ReadLine() Console.Write("ingrese ni ") n = Console.ReadLine() End Sub Sub main() Console.WriteLine("Antes x= {0} y ={1} n1 ={2}", x, y, n1) ingresar(x, y, n1) Console.WriteLine("Despues x= {0} y ={1} n1 ={2}", x, y, n1) Console.ReadLine() End SubEnd Module

Trazo de lineas

Page 56: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 56 -

Module Module1 Function distancia(ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single) As Single distancia = CSng(Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2))) End FunctionSub linea(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer) Dim cx, cy As Integer Console.SetCursorPosition(x1, y1) Console.Write("*") Console.SetCursorPosition(x2, y2) Console.Write("*") If distancia(x1, y1, x2, y2) >= 2 Then cx = (x1 + x2) / 2 cy = (y1 + y2) / 2 linea(x1, y1, cx, cy) linea(cx, cy, x2, y2) End If End Sub

Sub Main() Dim x1 As Integer = 20 Dim y1 As Integer = 20 Dim x2 As Integer = 40 Dim y2 As Integer = 2 Dim x3 As Integer = 64 Dim y3 As Integer = 20 Dim cx As Integer = (x1 + x2 + x3) / 3 Dim cy As Integer = (y1 + y2 + y3) / 3 linea(x1, y1, x2, y2) linea(x2, y2, x3, y3) linea(x3, y3, x1, y1) Console.ForegroundColor = ConsoleColor.Green linea(x1, y1, cx, cy) linea(x2, y2, cx, cy) linea(x3, y3, cx, cy) Console.SetCursorPosition(cx, cy) Console.Write("C") Console.ReadLine()

Page 57: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 57 -

End SubEnd Module

Graficar puntos exportados de Excel tanto en modo consola como en modo formulario

Module Module1 Function distancia(ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single) As Single distancia = CSng(Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2))) End Function

Sub linea(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer) Dim cx, cy As Integer Console.SetCursorPosition(x1, y1) Console.Write("*") Console.SetCursorPosition(x2, y2) Console.Write("*") If distancia(x1, y1, x2, y2) >= 2 Then cx = (x1 + x2) / 2 cy = (y1 + y2) / 2 linea(x1, y1, cx, cy) linea(cx, cy, x2, y2) End If End Sub

Sub Main() Dim X() As Single = {3, 3, 1, 4, 7, 5, 5, 3, 3} Dim Y() As Single = {1, 3, 3, 6, 3, 3, 1, 1, 1} Dim ndatos As Integer = 9 Dim x1, y1, x2, y2 As Integer Dim ex = 4, ey = 2 For i = 0 To ndatos - 2 x1 = X(i) * ex y1 = Y(i) * ey x2 = X(i + 1) * ex y2 = Y(i + 1) * ey linea(x1, y1, x2, y2) Next

Page 58: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 58 -

Console.ReadLine() End SubEnd Module

PRACTICA MIERCOLES 29 DE ABRIL DEL 2016 TURNO 17 A 19 HORAS

Ej hola mundoModule Module1 Sub Main() Console.WriteLine("hola mundo") Console.ReadLine() End SubEnd Module

Ejercicio 2 operaciones matematicas

Module Module1 Dim n1, N2 As Single Sub Main() Randomize() Console.Write("INGRESE PRIMER NUMERO") : n1 = Console.ReadLine() Console.Write("INGRESE SEGUNDO NUMERO") n2 = Console.ReadLine() Console.WriteLine("la suma es {0} ", n1 + n2) Console.WriteLine("la resta es {0} ", n1 - n2) Console.WriteLine("la multiplicacion es {0} ", n1 * n2) Console.WriteLine("la division entera es {0} ", n1 \ n2) Console.WriteLine("la division reales {0} ", n1 / n2) Console.WriteLine("el modulo es {0} ", n1 Mod n2) Console.WriteLine("LA potencia de {0} elevado a {1} es = {2} ", _ n1, N2, Math.Pow(n1, N2)) Console.WriteLine("LA raiz de {0} es {1} ", n1, Math.Sqrt(n1)) Console.WriteLine("numero aleatorio {0} ", Int(Rnd() * 20)) Console.ReadLine() End Sub

Page 59: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 59 -

End Module

Ejercicio nro 3 colores

Module Module1 Sub Main() Console.ForegroundColor = ConsoleColor.Green Console.BackgroundColor = 12 Console.SetCursorPosition(40, 12) Console.Write("peru") Console.SetCursorPosition(1, 1) Console.BackgroundColor = 0 Console.ForegroundColor = 9 Console.Write("AZUL") Console.ForegroundColor = 10 Console.SetCursorPosition(1, 2) Console.Write("VERDE") Console.ForegroundColor = 12 Console.SetCursorPosition(1, 3) Console.Write("ROJO") Dim i As Integer For i = 0 To 15 Console.ForegroundColor = i Console.SetCursorPosition(40, i) Console.Write("color {0} ", i) Next Console.ReadLine() End SubEnd Module

Dibujo de una figura

Page 60: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 60 -

Module Module1 Sub Main() Dim x1 As Integer = 10 Dim y1 As Integer = 20 Dim x2 As Integer = 40 Dim y2 As Integer = 5 Dim x3 As Integer = 60 Dim y3 As Integer = 22 Dim cx As Integer = (x1 + x2 + x3) / 3 Dim cy As Integer = (y1 + y2 + y3) / 3 Console.ForegroundColor = 9 linea(x1, y1, x2, y2) linea(x2, y2, x3, y3) linea(x3, y3, x1, y1) Console.ForegroundColor = 10 linea(x1, y1, cx, cy) linea(x2, y2, cx, cy) linea(x3, y3, cx, cy) Console.SetCursorPosition(cx, cy) Console.ForegroundColor = 14 Console.Write("X") Console.ReadLine() End SubEnd Module

Dibujo de una figura almacenada en Arreglos

Page 61: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 61 -

Module Module2 Function distancia(ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single) As Single distancia = CSng(Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2))) End Function

Sub linea(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer) Dim cx, cy As Integer Console.SetCursorPosition(x1, y1) Console.Write("*") Console.SetCursorPosition(x2, y2) Console.Write("*") If distancia(x1, y1, x2, y2) >= 2 Then cx = (x1 + x2) / 2 cy = (y1 + y2) / 2 linea(x1, y1, cx, cy) linea(cx, cy, x2, y2) End If End SubEnd Module

Module Module1 Dim i As Integer Dim ex As Integer = 6 Dim ey As Integer = -3 Dim np As Integer = 8 Dim x() = {1, 5, 4, 5, 3, 1, 2, 1, 1} Dim y() = {0, 0, 3, 3, 5, 3, 3, 0, 0}

Sub figura(cx As Integer, cy As Integer, ex As Integer, ey As Integer) Dim x1, y1, x2, y2 As Integer For i = 0 To np - 2 x1 = cx + x(i) * ex y1 = cy + y(i) * ey x2 = cx + x(i + 1) * ex

Page 62: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 62 -

y2 = cy + y(i + 1) * ey linea(x1, y1, x2, y2) Next End Sub Sub Main() figura(40, 12, 4, -2) Console.ForegroundColor = 10 figura(10, 15, 6, -3) Console.ReadLine() End SubEnd Module

PRACTICAS SI LAB MIER 29 DE ABRIL DE 17-21 HORAS

Module Module1 Sub Main() Console.WriteLine("HOLA MUNDO") Console.ReadLine() End SubEnd Module

COLORES

Option Explicit On'Option Strict OnModule Module1 Sub Main() Console.ForegroundColor = ConsoleColor.Yellow Console.BackgroundColor = 9 Console.SetCursorPosition(40, 12) Console.Write("PERU")

Page 63: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 63 -

Console.BackgroundColor = 0 Console.ForegroundColor = 9 Console.SetCursorPosition(1, 1) Console.Write("AZUL") Console.ForegroundColor = 10 Console.SetCursorPosition(10, 1) Console.Write("VERDE") Console.ForegroundColor = 12 Console.SetCursorPosition(20, 1) Console.Write("ROJO") Console.BackgroundColor = 15 Console.ForegroundColor = 0 Console.SetCursorPosition(70, 1) Console.Write("UNSA") Console.BackgroundColor = 2 Console.ForegroundColor = 10 Console.SetCursorPosition(70, 2) Console.Write("2015") Dim I As Integer Console.BackgroundColor = 0 For I = 0 To 15 Console.ForegroundColor = I Console.SetCursorPosition(70, I + 3) Console.Write(" COLOR {0} ", I) Next Console.ReadLine() End SubEnd Module

PRACTICA DE LABORATORIO JUEVES 30 DE ABRIL DEL 2015TURNO DE 9 11 HORAS

REM generar los valores de un funcionImports System.IOModule Module1 Function f(x As Single) As Single Return Math.Pow(x, 2) End Function Sub Main() Dim x, y As Single Dim escritor As StreamWriter '' imprime los valores de la funcion en la pantalla 'For x = -3 To 3 Step 0.5 ' y = f(x) ' Console.WriteLine(" {0} {1} ", x, y) 'Next '' imprime los valores de la funcion en el archivo 'escritor = New StreamWriter("E: \datos\funcion.txt") 'For x = -3 To 3 Step 0.5 ' y = f(x) ' escritor.WriteLine(" {0} {1} {2} ", x, vbTab, y)

Page 64: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 64 -

'Next 'escritor.Close() ' lee los valores Console.WriteLine("archivo leido") Dim lector As StreamReader lector = New StreamReader("E: \datos\animales.txt") Dim cadena As String Dim vector(10) As String For i = 1 To 5 cadena = lector.ReadLine() vector(i) = cadena Console.WriteLine("{0}", cadena) Next Console.WriteLine("cadsf") For i = 1 To 5 cadena = lector.ReadLine() InStr ( Console.WriteLine("{0}", vector(i)\) Next lector.Close() Console.ReadLine() End SubEnd Module

PRACTICA DE LABORATORIO JUEVES 30 DE ABRIL DEL 2015TURNO DE 11 13 HORAS

Mostrar una matriz de números aleatorios

Module Module1 Public A(21, 41) As Integer Public nf As Integer = 14, nc As Integer = 30 Sub Iniciar(A(,) As Integer, nf As Integer, nc As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 A(fila, col) = Int(Rnd() * 15) Next

Page 65: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 65 -

Next End Sub Sub MostrarMatriz(cx As Integer, cy As Integer, A(,) As Integer, nf As Integer, nc As Integer, ex As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 Console.SetCursorPosition(cx + col * ex, cy + fila) Console.ForegroundColor = A(fila, col) Console.Write("{0}", Hex(A(fila, col))) Next Next End Sub Sub Main() Iniciar(A, nf, nc) MostrarMatriz(10, 2, A, nf, nc, 2) Console.ReadLine() End SubEnd Module

Sub Main() Iniciar(A, nf, nc) Console.Write(" cuantas veces quiere generar ") nveces = Console.ReadLine For i = 1 To nveces GenerarMatriz(A, nf, nc, 1) MostrarMatriz(10, 2, A, nf, nc, 2) Console.ReadLine() Next End Sub

Graficos animados con barra de dezplazamientoLa barra de desplazamiento vincular con la celda a1

Elabore la Aplicación de la pagina 26 en macros para Excel

Elabore la siguiente aplicacionEn el grafico en opciones de eje

Page 66: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 66 -

CODIGO DEL MODULO1

Private Sub BtnGenerarFuncion_Click()Call GenerarFuncion(A, nf, nc)End SubPrivate Sub btnLeer_Click()Dim nvar As IntegerOpen NombreArchivo For Input As #1While Not EOF(1)Line Input #1, cadenacadena = cadena & Chr(9)inicio = 1For col = 0 To nc - 1 pos = InStr(inicio, cadena, Chr(9)) subcadena = Mid(cadena, inicio, pos - inicio) A(fila, col) = CInt(CSng(Val(subcadena))) inicio = pos + 1NextWendClose #1Call MostrarMatriz(cx, cy, A, nf, nc)End Sub

Private Sub BtnVer_Click()Call MostrarMatriz(cx, cy, A, nf, nc)End Sub

Private Sub cmdGrabar_Click() Dim cadena As StringDim fila, col As Integer'Open "e:\datos\alturas.txt" For Append As #1

Page 67: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 67 -

Open NombreArchivo For Output As #1 For fila = 0 To nf - 1 cadena = "" For col = 0 To nc - 1 cadena = cadena & A(fila, col) & vbTab Next Print #1, cadena NextClose #1End Sub

Sub MostrarMatriz(cx As Integer, cy As Integer, A() As Integer, nf As Integer, nc As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 Hoja2.Cells(cy + fila, cx + col) = A(fila, col) Next Next End Sub

Private Sub BtnAnimar_Click()RandomizeDim rx, ry, i As IntegerFor i = 1 To nsim rx = Int(Rnd() * nc) ry = Int(Rnd() * nf) If rx < nc And ry < nf Then If (A(ry, rx) < 15) Then A(ry, rx) = A(ry, rx) + 1 Else A(ry, rx) = 0 End If End IftxtEtapa.Text = i Hoja2.Cells(cy + ry, cx + rx) = A(ry, rx)'Application.Wait (Now + TimeValue("0:00:001"))DoEventsNextCall MostrarMatriz(cx, cy, A, nf, nc)End Sub

Sub IniciarValores()cx = 7cy = 2nf = Hoja2.TextBox21.Textnc = Hoja2.TextBox22.Textnsim = Hoja2.txtSimulacion.Textpx = 5py = 5maximo = 15minimo = 0

Page 68: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 68 -

End Sub

Private Sub CmdIniciar_Click()Call IniciarValoresCall Iniciar(A, nf, nc, minimo)End Sub

Private Sub cmdProcesar_Click()RandomizeDim rx, ry As Integer rx = Int(Rnd() * nc) ry = Int(Rnd() * nf) If rx < nc And ry < nf Then If (A(ry, rx) < 15) Then A(ry, rx) = A(ry, rx) + 1 Else A(ry, rx) = 0 End If End If Call MostrarMatriz(cx, cy, A, nf, nc)End Sub Private Sub cmdRestar_Click()Call IniciarValoresCall Iniciar(A, nf, nc, maximo)For i = 1 To nsimCall generarSumaResta(A, px, py, nf, nc, 2)Call MostrarMatriz(cx, cy, A, nf, nc)DoEventsNextEnd Sub

Private Sub cmdSumar_Click()Dim i As IntegerCall IniciarValoresCall Iniciar(A, nf, nc, minimo)For i = 1 To nsimCall generarSumaResta(A, px, py, nf, nc, 1)Call MostrarMatriz(cx, cy, A, nf, nc)DoEventsNextEnd Sub Private Sub Worksheet_Activate()Call IniciarTodoEnd Sub

CODIGO DE LA HOJA 2

Private Sub BtnGenerarFuncion_Click()Call GenerarFuncion(A, nf, nc)End Sub

Page 69: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 69 -

Private Sub btnLeer_Click()Dim nvar As IntegerOpen NombreArchivo For Input As #1While Not EOF(1)Line Input #1, cadenacadena = cadena & Chr(9)inicio = 1For col = 0 To nc - 1 pos = InStr(inicio, cadena, Chr(9)) subcadena = Mid(cadena, inicio, pos - inicio) A(fila, col) = CInt(CSng(Val(subcadena))) inicio = pos + 1NextWendClose #1Call MostrarMatriz(cx, cy, A, nf, nc)End Sub

Private Sub BtnVer_Click()Call MostrarMatriz(cx, cy, A, nf, nc)End Sub

Private Sub cmdGrabar_Click() Dim cadena As StringDim fila, col As Integer'Open "e:\datos\alturas.txt" For Append As #1Open NombreArchivo For Output As #1 For fila = 0 To nf - 1 cadena = "" For col = 0 To nc - 1 cadena = cadena & A(fila, col) & vbTab Next Print #1, cadena NextClose #1End Sub

Sub MostrarMatriz(cx As Integer, cy As Integer, A() As Integer, nf As Integer, nc As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 Hoja2.Cells(cy + fila, cx + col) = A(fila, col) Next Next End Sub

Private Sub BtnAnimar_Click()RandomizeDim rx, ry, i As IntegerFor i = 1 To nsim rx = Int(Rnd() * nc)

Page 70: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 70 -

ry = Int(Rnd() * nf) If rx < nc And ry < nf Then If (A(ry, rx) < 15) Then A(ry, rx) = A(ry, rx) + 1 Else A(ry, rx) = 0 End If End IftxtEtapa.Text = i Hoja2.Cells(cy + ry, cx + rx) = A(ry, rx)'Application.Wait (Now + TimeValue("0:00:001"))DoEventsNextCall MostrarMatriz(cx, cy, A, nf, nc)End Sub

Sub IniciarValores()cx = 7cy = 2nf = Hoja2.TextBox21.Textnc = Hoja2.TextBox22.Textnsim = Hoja2.txtSimulacion.Textpx = 5py = 5maximo = 15minimo = 0End SubPrivate Sub CmdIniciar_Click()Call IniciarValoresCall Iniciar(A, nf, nc, minimo)End Sub

Private Sub cmdProcesar_Click()RandomizeDim rx, ry As Integer rx = Int(Rnd() * nc) ry = Int(Rnd() * nf) If rx < nc And ry < nf Then If (A(ry, rx) < 15) Then A(ry, rx) = A(ry, rx) + 1 Else A(ry, rx) = 0 End If End If Call MostrarMatriz(cx, cy, A, nf, nc)End Sub

Private Sub cmdRestar_Click()Call IniciarValoresCall Iniciar(A, nf, nc, maximo)For i = 1 To nsimCall generarSumaResta(A, px, py, nf, nc, 2)

Page 71: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 71 -

Call MostrarMatriz(cx, cy, A, nf, nc)DoEventsNextEnd Sub

Private Sub cmdSumar_Click()Dim i As IntegerCall IniciarValoresCall Iniciar(A, nf, nc, minimo)For i = 1 To nsimCall generarSumaResta(A, px, py, nf, nc, 1)Call MostrarMatriz(cx, cy, A, nf, nc)DoEventsNextEnd Sub Private Sub Worksheet_Activate()Call IniciarTodoEnd Sub

JUEGO DE LA VIDA EN EXCEL

Public cont As Integer Public Const maxfilas As Integer = 40 Public Const maxcol As Integer = 40 Public A(maxfilas, maxcol) As Integer Public nfilas As Integer Public velocidad As Integer Public ex As Integer Public ey As Integer Public nf As Integer Public nc As Integer

Page 72: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 72 -

Public ng As Integer ' Public ancho, alto As Integer

Sub IniciarVariables() cont = 0 nfilas = 8 velocidad = 10 ex = 16 ey = 16 nf = 12 '20 ' 40 '94 ' 40 nc = 38 ' 20 ' 50 '80 ' 80 ng = 200End Sub

Sub IniciarPantalla(A() As Integer, ByVal nf As Integer, ByVal nc As Integer, Semilla As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 A(fila, col) = Semilla Next Next End Sub Sub TranferirMatriz(A() As Integer, B() As Integer, ByVal nf As Integer, ByVal nc As Integer) Dim fila, col As Integer For fila = 1 To nf - 1 For col = 1 To nc - 1 B(fila, col) = A(fila, col) Next Next End Sub Sub JuegoVida(A() As Integer, ByVal nf As Integer, ByVal nc As Integer) Dim B(maxfilas, maxcol) As Integer Dim fila, col, vecinos, x1, y1, x2, y2, fila1, col1 As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 vecinos = 0 If fila > 1 Then y1 = fila - 1 Else y1 = fila End If If fila < nf - 1 Then y2 = fila + 1 Else y2 = fila End If If col > 1 Then x1 = col - 1 Else

Page 73: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 73 -

x1 = col End If If col < nc Then x2 = col + 1 Else x2 = col End If For fila1 = y1 To y2 For col1 = x1 To x2 If (fila1 = fila And col1 = col) Then 'Continue For Else If A(fila1, col1) = 1 Then vecinos = vecinos + 1 End If Next Next Select Case vecinos Case 0 B(fila, col) = 0 Case 1 B(fila, col) = 0 Case 2 B(fila, col) = A(fila, col) Case 3 B(fila, col) = 1 Case Else B(fila, col) = 0 End Select Next Next Call IniciarPantalla(A, nf, nc, 0) Call TranferirMatriz(B, A, nf, nc) End Sub Sub RecuperarMatriz(A() As Integer, ByVal nf As Integer, ByVal nc As Integer) Dim fila As Integer, col As Integer For fila = 0 To nf - 1 cadena = cadena & Chr(9) inicio = 1 For col = 0 To nc - 1 A(fila, col) = Val(Cells(fila + 1, col + 1)) Next Next End Sub

CODIGO DE LA HOJA 1

Private Sub cmdIniciar_Click()Call RecuperarMatriz(A, nf, nc)End Sub

Sub MostrarMatriz(px, py, A, nf, nc)

Page 74: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 74 -

Dim fila, col As IntegerFor fila = 0 To nf - 1For col = 0 To nc - 1Cells(py + fila, px + col) = A(fila, col)NextNextEnd Sub

Private Sub cmdMostrarMatriz_Click()Call MostrarMatriz(1, 15, A, nf, nc)End Sub

Private Sub cmdObtenerValores_Click()nc = Range("AO2")nf = Range("AO3")ex = Range("AO4")ey = Range("AO5")velocidad = Range("AO6")ng = Range("AO7")cont = Range("AO8")End Sub

FUNCIONES PERSONALIZADAS

Ejercicio 5 Elaborar funciones personalizadas

En un a clase hay n alumnos y cada alumno tiene 3 notas, si pide encontrar el promedio de esas notas sabiendo que el promedio de las notas se obtiene sumando las dos más altas notas y luego dividiendo entre dos

Pasos1. Ir al módulo de Visual Basic (ALT+F11) y luego Insertar módulo y escribir la siguiente función

Option Explicit

Function proalumnos(n1 As Single, n2 As Single, n3 As Single)Dim suma As Single, menor As Singlesuma = n1 + n2 + n3menor = n1If n2 < menor Thenmenor = n2End IfIf n3 < menor Thenmenor = n3End Ifproalumnos = (suma - menor) / 2End Function

2.- Estando en Excel llame como cualquier función está en el grupo de funciones definidas por el usuario

Page 75: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 75 -

1. Puede copiar y pegar como cualquier función

Elaborar el ejercicio de la pagina 66 usando funciones de texto de Excel

Nro

APELLIDOS Y NOMBRES /

APELLIDO PATERNO ,

APELLIDO MATERNO

NOMBRES COMPUESTO

1PEREZ/QUISPE, JUAN PEDRO 6 PEREZ

13 QUISPE

JUAN PEDRO

JUAN PEDRO,PEREZ QUISPE

2

CRUZ/SANTANDER,ELIO CELEDONIO 5 CRUZ

15

SANTANDER

ELIO CELEDONIO

ELIO CELEDONIO,CRUZ SANTANDER

3VELIZ/VILCA, ISMAEL 6 VELIZ

12 VILCA ISMAEL

ISMAEL,VELIZ VILCA

En modo formula

Nro

APELLIDOS Y NOMBRES /

APELLIDO PATERNO ,

APELLIDO MATERNO NOMBRES

COMPUESTO

1

PEREZ/QUISPE, JUAN PEDRO

=ENCONTRAR("/",B2,1)

=EXTRAE(B2,1,C2-1)

=ENCONTRAR(",",B2,C2)

=EXTRAE(B2,C2+1,E2-C2-1)

=EXTRAE(B2,E2+1,LARGO(B2)-E2)

=CONCATENAR(H2,",",D2," ",F2)

2

CRUZ/SANTANDER,ELIO CELEDONIO

=ENCONTRAR("/",B3,1)

=EXTRAE(B3,1,C3-1)

=ENCONTRAR(",",B3,C3)

=EXTRAE(B3,C3+1,E3-C3-1)

=EXTRAE(B3,E3+1,LARGO(B3)-E3)

=CONCATENAR(H3,",",D3," ",F3)

3 VELIZ/ =ENCO =EXTR =ENCO =EXTRA =EXTRAE(B =CONCATE

Page 76: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 76 -

VILCA, ISMAEL

NTRAR("/",B4,1)

AE(B4,1,C4-1)

NTRAR(",",B4,C4)

E(B4,C4+1,E4-C4-1)

4,E4+1,LARGO(B4)-E4)

NAR(H4,",",D4," ",F4)

Plan de practicas martes 5 de mayol del 2015

Grabar un archivo de textoImports System.IOModule Module1 Sub Main() Dim swEscritor As StreamWriter Dim NombreArchivo = "E:\DATOS\NOTAS.txt" swEscritor = New StreamWriter(NombreArchivo) ' escribir líneas swEscritor.WriteLine("EJEMPLO DE UN ARCHIVO DE TEXTO") swEscritor.Flush() Console.WriteLine(" archivo {0} grabado satisfactoriamente", NombreArchivo) Console.ReadLine() End SubEnd Module

Leer un archive de textoImports System.IOModule Module1 Sub main() Dim srLector As StreamReader = New StreamReader("e:\DATOS\NOTAS.txt") Dim Linea As String Dim ContadorLin As Integer = 1 Console.WriteLine("lectura de archivos") Linea = srLector.ReadLine() Do While Not (Linea Is Nothing) Console.WriteLine("Línea: {0} - Contenido: {1}", ContadorLin, Linea) ContadorLin += 1 Linea = srLector.ReadLine() Loop srLector.Close() Console.ReadLine() End SubEnd Module

En realidad se puede leer cualquier archive de texto

Page 77: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 77 -

Imports System.IOModule Module1 Sub main() Dim srLector As StreamReader = New StreamReader("e:\DATOS\NOTAS.txt") Dim Linea As String Console.WriteLine("lectura de archivos") Linea = srLector.ReadLine() Do While Not (Linea Is Nothing) Console.WriteLine(" {0}", Linea) Linea = srLector.ReadLine() Loop srLector.Close() Console.ReadLine() End SubEnd Module

El ejercicio de la pagina 26

CODIGO DEL MODULO 2

Imports System.IOModule Module2 Public A(10, 10) As Integer Public nf As Integer = 10, nc As Integer = 10 Public NombreArchivo As String = "E:\DATOS\DATOS3D.txt" Public maximo As Integer = 15 Public minimo As Integer = 0 Public opcion As Integer

Function menu() As Integer Dim op As Integer Console.ForegroundColor = 15 Console.Clear() Console.SetCursorPosition(1, 1) Console.Write("1. Generar 2: Mostrar 3. Recuperar 4. Grabar 5 salir ==opcion==> ") op = Console.ReadLine() Return op End Function

Sub GenerarMatriz(A(,) As Integer, nf As Integer, nc As Integer) Dim fila, col As Integer

Page 78: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 78 -

For fila = 0 To nf - 1 For col = 0 To nc - 1 A(fila, col) = Int(Rnd() * maximo) Next Next End Sub

Sub MostrarMatriz(cx As Integer, cy As Integer, A(,) As Integer, nf As Integer, nc As Integer, ex As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 Console.SetCursorPosition(cx + col * ex, cy + fila) Console.ForegroundColor = A(fila, col) Console.Write("{0}", Hex(A(fila, col))) Next Next End Sub

Sub GrabarMatriz(Nombre As String, A(,) As Integer, nf As Integer, nc As Integer) Dim swEscritor As StreamWriter swEscritor = New StreamWriter(NombreArchivo) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 swEscritor.Write("{0}{1}", A(fila, col), vbTab) Next swEscritor.WriteLine() Next swEscritor.Close() End Sub

Sub RecuperarMatriz(ByVal nombrearchivo As String, ByVal A(,) As Integer, ByVal nf As Integer, ByVal nc As Integer) Dim srLector As StreamReader srLector = New StreamReader(nombrearchivo) Dim fila As Integer, col As Integer Dim cadena As String = "" Dim subcadena As String Dim pos As Integer = 0 Dim inicio As Integer = 1 For fila = 0 To nf - 1 cadena = srLector.ReadLine() cadena = cadena & Chr(9) inicio = 1 For col = 0 To nc - 1 pos = InStr(inicio, cadena, Chr(9)) subcadena = Mid(cadena, inicio, pos - inicio) A(fila, col) = Val(subcadena) inicio = pos + 1 Next Next

Page 79: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 79 -

srLector.Close() End SubEnd Module

CODIGO DEL MODULO 1

Module Module1 Sub Main() Randomize() Do opcion = menu() Select Case opcion Case 1 ' generar la matriz Console.SetCursorPosition(1, 3) GenerarMatriz(A, nf, nc)

Console.WriteLine("matriz generada presiones enter para continuar") Console.ReadLine() Case 2 ' mostrar matriz MostrarMatriz(5, 5, A, nf, nc, 2) Console.ForegroundColor = 15 Console.SetCursorPosition(1, 4) Console.WriteLine("presione tecla para continuar") Console.ReadLine() Case 3 ' recuperar RecuperarMatriz(NombreArchivo, A, nf, nc) Console.ForegroundColor = 15 Console.SetCursorPosition(1, 3) Console.WriteLine(" archivo {0} recuperado satisfactoriamente", NombreArchivo) Console.SetCursorPosition(1, 4) Console.WriteLine(" presione tecla para continuar") Console.ReadLine() Case 4 ' Grabar GrabarMatriz(NombreArchivo, A, nf, nc) Console.ForegroundColor = 15 Console.SetCursorPosition(1, 3) Console.WriteLine(" archivo {0} grabado satisfactoriamente", NombreArchivo) Console.SetCursorPosition(1, 4) \ Console.WriteLine(" presione tecla para continuar") Console.ReadLine() End Select Loop Until opcion = 5 End SubEnd Module

Recuperar un diseño por ejemplo la casita

0 0 0 0 0 0 0 0 0 012 0 0 0 0 0 0 0 0 0 0

Page 80: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 80 -

0 0 0 012

12 0 0 0

12

12

12 0 0 0 0 0 0 0 0 0

0 0 0 012

12 0 0

12

12

12

12

12 0 0 0 0 0 0 0 0

0 0 0 012

12 0

12

12

12

14

12

12

12 0 0 0 0 0 0 0

0 0 0 012

12

12

12

12

14

14

14

12

12

12 0 0 0 0 0 0

0 0 0 012

12

12

12

14

14

14

14

14

12

12

12 0 0 0 0 0

0 0 0 012

12

12

14

14

14

14

14

14

14

12

12

12 0 0 0 0

0 0 012

12

12

14

14

14

14

14

14

14

14

14

12

12

12 0 0 0

0 012

12

12

14

14

14

14

14

14

14

14

14

14

14

12

12

12 0 0

012

12

12

14

14

14

14

14

14

14

14

14

14

14

14

14

12

12

12 0

12

12

12

14

14

14

14 9 9 9 9 9 9 9

14

14

14

14

12

12

12

012 0

14

14

14

14 9

11

11

11

11

11 9

14

14

14

14 0

12 0

0 0 014

14

14

14 9

11

11

11

11

11 9

14

14

14

14 0 0 0

0 0 0

14

14

14

14 9

11

11

11

11

11 9

14

14

14

14 0 0 0

0 0 014

14

14

14 9

11

11

11

11

11 9

14

14

14

14 0 0 0

0 0 014

14

14

14 9

13

11

11

11

11 9

14

14

14

14 0 0 0

0 0 014

14

14

14 9

13

11

11

11

11 9

14

14

14

14 0 0 0

0 0 014

14

14

14 9

11

11

11

11

11 9

14

14

14

14 0 0 0

0 0 014

14

14

14 9

11

11

11

11

11 9

14

14

14

14 0 0 0

0 0 014

14

14

14 9

11

11

11

11

11 9

14

14

14

14 0 0 0

0 0 014

14

14

14 9

11

11

11

11

11 9

14

14

14

14 0 0 0

0 0 014

14

14

14 9 9 9 9 9 9 9

14

14

14

14

10

10

10

0 0 014

14

14 7 7 7 7 7 7 7 7 7

14

14

14

10

10

10

Page 81: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 81 -

ModifiqueImports System.IOModule Module2 Public A(25, 25) As Integer Public nf As Integer = 23, nc As Integer = 21 'Public NombreArchivo As String = "E:\DATOS\DATOS3D.txt" Public NombreArchivo As String = "E:\DATOS\casita21x23.txt"

Diseñe otros modelos, grafique líneas de datos recuperadosConvertir una archivo en minúsculas a mayúsculaTraductor Buscador de palabras

PRACTICA DE LABORATORIO MARTES 05 DE MAYO DEL 2015TURNO DE 17 A 21 HORAS

Imports System.IOModule Module1 Sub main() Dim srLector As StreamReader = New StreamReader("e:\DATOS\NOTAS.txt") Dim Linea As String Console.WriteLine("lectura de archivos") Linea = Console.ReadLine() Do While Not (Linea Is Nothing) Console.WriteLine(" {0}", Linea) Linea = Console.ReadLine() Loop srLector.Close() Console.ReadLine() End SubEnd Module

PRACTICAS DEL 05 DE MAYO DEL 2015 TURNO DE 7 A 9 P,MModule Module1 Sub Main() Console.WriteLine("HOLA MUNDO") Console.ReadLine() End Sub

Page 82: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 82 -

End Module

Grabando en el archivoImports System.IOModule Module1 Dim nombreArchivo As String = "E:\datos\hola.txt" Sub Main() Dim AEscritor As StreamWriter AEscritor = New StreamWriter(nombreArchivo) AEscritor.WriteLine("HOLA MUNDO") AEscritor.Close() Console.WriteLine(" El archivo {0}ha sido grabado", nombreArchivo) Console.ReadLine() End SubEnd Module

Ejercicio 2 el mismo ejercicio pero con archivos

Imports System.IOModule Module1 Dim nombreArchivo As String = "E:\datos\hola.txt" Dim nombre = "juan perez" Sub Main() Dim AEscritor As StreamWriter Dim X As Single AEscritor = New StreamWriter(nombreArchivo) Console.WriteLine("ESCRIBIENDO EN LA PANTALLA") For X = -3 To 3 Step 0.5 Console.WriteLine("{0}{1} {0,8:F4}", X, vbTab, Math.Sin(X)) Next AEscritor.WriteLine("ESCRIBIENDO EN LA PANTALLA") For X = -3 To 3 Step 0.5 AEscritor.WriteLine("{0}{1} {0,8:F4}", X, Chr(9), Math.Sin(X)) Next Console.WriteLine(" El archivo {0}ha sido grabado", nombreArchivo) AEscritor.WriteLine(" El archivo {0}ha sido grabado", nombreArchivo) AEscritor.Close() Console.ReadLine() End SubEnd Module

Page 83: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 83 -

PRACTICA DE ARCHIVOS ( PLAN PARA LAS CLASES DEL MIE 06 DE MAYO DEL 2015 DE 7 A 9 HORAS)

Imports System.IOModule Module1 Dim NombreArchivo As String = "E:\DATOS\texto1.txt" Dim opcion As Integer Dim cadena As String

Sub Main() Dim cki As ConsoleKeyInfo Do Console.WriteLine("1 Ingresar 2. Mostrar 3. Grabar 4 Recuperar 5.Salir ") Console.Write("Ingrese opcion ==> ") opcion = Console.ReadLine Select Case opcion Case 1 Console.WriteLine(" ingrese texto escape z para terminar") cadena = "" Do cki = Console.ReadKey() cadena = cadena & Chr(cki.Key) If cki.Key = 13 Then cadena = cadena + Chr(10) Console.WriteLine() End If Loop While cki.Key <> ConsoleKey.Escape Console.WriteLine("fin")

Page 84: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 84 -

Case 2 Console.WriteLine(" el texto ingresado es ") Console.WriteLine("{0}", cadena) Case 3 Dim escritor As StreamWriter escritor = New StreamWriter(NombreArchivo) escritor.WriteLine("{0}", cadena) Console.WriteLine(" archivo {0} grabado ", NombreArchivo) escritor.Close() Case 4 Dim lector As StreamReader Dim linea As String lector = New StreamReader(NombreArchivo) cadena = "" linea = lector.ReadLine Do While Not (linea Is Nothing) cadena = cadena & linea & Chr(10) & Chr(13) linea = lector.ReadLine() Loop lector.Close() Console.WriteLine(" archivo {0} recuperado ", NombreArchivo) End Select Loop While opcion <> 5 End SubEnd Module

Recupera cualquier archive de textoOTRA MANERA

Imports System.IOModule Module1 Const maximo As Integer = 100

Page 85: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 85 -

Dim NombreArchivo As String = "E:\DATOS\texto1.txt" Dim opcion As Integer Dim cadena(maximo) As String Dim cont, nf As Integer Dim linea As String Sub Main() Dim cki As ConsoleKeyInfo Dim i As Integer Do Console.WriteLine("1 Ingresar 2. Mostrar 3. Grabar 4 Recuperar 5.Salir ") Console.Write("Ingrese opcion ==> ") opcion = Console.ReadLine Select Case opcion Case 1 Console.WriteLine(" ingrese texto fin para terminar") cont = 0 Do linea = Console.ReadLine() cadena(cont) = linea If linea <> "fin" Then cont = cont + 1 Else Exit Do End If Loop While linea <> "fin" nf = cont Case 2 Console.WriteLine(" el texto ingresado es ") For i = 0 To nf - 1 Console.WriteLine("{0}", cadena(i)) Next Case 3 Dim escritor As StreamWriter escritor = New StreamWriter(NombreArchivo) For i = 0 To nf - 1 escritor.WriteLine("{0}", cadena(i)) Next Console.WriteLine(" archivo {0} grabado ", NombreArchivo) escritor.Close() Case 4 Dim lector As StreamReader Dim linea As String lector = New StreamReader(NombreArchivo) cont = 0 linea = lector.ReadLine Do While Not (linea Is Nothing) cadena(cont) = linea linea = lector.ReadLine() cont = cont + 1 Loop nf = cont lector.Close()

Page 86: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 86 -

Console.WriteLine(" archivo {0} recuperado ", NombreArchivo) End Select Loop While opcion <> 5 End SubEnd Module

EJERCICIO DE L PAGINA 89 EN PROGRAMACION VISUALPonga propiedad textbox multiline =trueScrollbar = booth

CódigoImports System.IOPublic Class Form1 Dim NombreArchivo As String = "E:\DATOS\texto1.txt" Private Sub btnGrabar_Click(sender As Object, e As EventArgs) Handles btnGrabar.Click Dim escritor As StreamWriter SaveFileDialog1.ShowDialog() NombreArchivo = SaveFileDialog1.FileName escritor = New StreamWriter(NombreArchivo) escritor.WriteLine("{0}", TextBox1.Text) escritor.Close()

Page 87: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 87 -

ListBox1.Items.Add(" archivo " & NombreArchivo & " GRABADO") End Sub

Private Sub BtnRecuperar_Click(sender As Object, e As EventArgs) Handles BtnAbrir.Click OpenFileDialog1.ShowDialog() NombreArchivo = OpenFileDialog1.FileName TextBox1.Clear() Dim cadena As String = "" Dim lector As StreamReader Dim linea As String lector = New StreamReader(NombreArchivo) linea = lector.ReadLine Do While Not (linea Is Nothing) cadena = cadena + linea + vbCrLf linea = lector.ReadLine() Loop TextBox1.Text = cadena lector.Close() ListBox1.Items.Add(" archivo " & NombreArchivo & " RECUPERADO") End Sub Private Sub btnFuente_Click(sender As Object, e As EventArgs) Handles btnFuente.Click FontDialog1.ShowDialog() TextBox1.Font = FontDialog1.Font End Sub

Private Sub btnColorFondo_Click(sender As Object, e As EventArgs) Handles btnColorFondo.Click ColorDialog1.ShowDialog() TextBox1.BackColor = ColorDialog1.Color End Sub

Private Sub btnColorFuente_Click(sender As Object, e As EventArgs) Handles btnColorFuente.Click ColorDialog1.ShowDialog() TextBox1.ForeColor = ColorDialog1.Color End Sub

Private Sub btnFolder_Click(sender As Object, e As EventArgs) Handles btnFolder.Click FolderBrowserDialog1.ShowDialog() TextBox1.Text = FolderBrowserDialog1.SelectedPath End SubEnd Class

Traductor

Page 88: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 88 -

‘ probar con diccionario bajadoSi se tiene el archive- Novelties, reports, consumerism, games, tricks - Novedades, reportajes, consumo, juegos, trucosY los términos son

Novedades noveltiesreportajes reports

consumoconsumerism

juegos gamestrucos tricks

Imports System.IOPublic Class Form1 Public A(200) As String Public B(200) As String Dim nterminos As Integer = 2 Dim Texto As String = "" Private Sub EspaIngles(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EspañolIngles.Click Dim fila1 As Integer Dim nombre1 As String Dim nombre2 As String texto = txtFuente.Text For fila1 = 0 To nterminos - 1 nombre1 = A(fila1) ' ESPAÑOL nombre2 = B(fila1) ' INGLES texto = Replace(texto, nombre1, nombre2) Next txtDestino.Text = texto End Sub

Page 89: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 89 -

Private Sub btnTerminos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTerminos.Click OpenFileDialog1.ShowDialog() srLector = New StreamReader(OpenFileDialog1.FileName) Dim cadena As String = "" Dim subcadena1 As String Dim subcadena2 As String Dim cont As Integer = 0 cadena = srLector.ReadLine() Dim pos As Integer = 0 Do While Not (cadena Is Nothing) pos = InStr(1, cadena, Chr(9)) subcadena1 = Mid(cadena, 1, pos - 1) A(cont) = subcadena1 ' español subcadena2 = Mid(cadena, pos + 1, Len(cadena) - pos) B(cont) = subcadena2 cadena = srLector.ReadLine() ' ingles cont += 1 Loop 'Me.Text = "terminos " & cont srLector.Close() ListBox1.Items.Clear() ListBox2.Items.Clear() nterminos = cont For i = 0 To nterminos - 1 ListBox1.Items.Add(A(i)) ListBox2.Items.Add(B(i)) Next End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles BtnAbrir.Click OpenFileDialog1.ShowDialog() Dim Linea As String srLector = New StreamReader(OpenFileDialog1.FileName) Linea = srLector.ReadLine() Texto = "" Do While Not (Linea Is Nothing) Texto = Texto & Linea & vbCrLf Linea = srLector.ReadLine() Loop txtFuente.Text = Texto srLector.Close() End Sub

Private Sub btnInglesEspañol_Click(sender As Object, e As EventArgs) Handles btnInglesEspañol.Click Dim fila1 As Integer Dim nombre1 As String Dim nombre2 As String texto = txtFuente.Text For fila1 = 0 To nterminos - 1 nombre1 = A(fila1) ' ESPAÑOL

Page 90: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 90 -

nombre2 = B(fila1) ' INGLES texto = Replace(texto, nombre2, nombre1) Next txtDestino.Text = texto End Sub

Private Sub btnGrabar_Click(sender As Object, e As EventArgs) Handles btnGrabar.Click SaveFileDialog1.ShowDialog() Dim escritor As StreamWriter = New StreamWriter(SaveFileDialog1.FileName) escritor.WriteLine("{0}", txtDestino.Text) escritor.Close() End SubEnd Class

Menú y barra de herramientas

11.1 EJEMPLO DE APLICACIÓN COMPLETA CON MENUS Y BARRA DE HERRAMIENTAS pag 657 vea aplicación MDI

e

PRACTICAS SI 2015 MIE 06 DE MAYO DEL 2015 TURNO DE 17 19 HORASHOLA MUNDO EN ARCHIVOS

Imports System.IOModule Module1 Sub Main() Dim archivo As StreamWriter archivo = New StreamWriter("E:\datos\texto1.txt") Console.WriteLine("HOLA MUNDO") archivo.WriteLine("HOLA MUNDO") Console.WriteLine("el archivo ha sido grabado") archivo.WriteLine("el archivo ha sido grabado") archivo.Close() ' para mostrar en un archivo Console.ReadLine() End SubEnd Module

Page 91: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 91 -

Valores de una function

Imports System.IOModule Module1 Function f(x As Single) As Single ' f = Math.Sin(x) Return Math.Sin(x) End Function Sub Main() Dim NombreArchivo As String = "E:\datos\funcion.txt" ' mostrando en la consola Console.WriteLine("valores de una funcion f(x)=sin(x)") Dim x As Single For x = -3 To 3 Step 0.5 Console.WriteLine(" {0}{1}{2,8:F4} ", x, vbTab, f(x)) Next ' para mostrar en un archivo Dim archivo As StreamWriter = New StreamWriter(NombreArchivo) archivo.WriteLine("valores de una funcion f(x)=sin(x)") For x = -3 To 3 Step 0.5 archivo.WriteLine(" {0}{1}{2,8:F4} ", x, vbTab, f(x)) Next archivo.Close() Console.ReadLine() End SubEnd Module

Page 92: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 92 -

Imports System.IOModule Module1 Sub Main() Dim nombrearchivo As String = "E:\datos\prueba1.txt" Dim cadena As String ' lectura del teclado 'Console.WriteLine(" leido del teclado ") 'cadena = Console.ReadLine() 'Console.WriteLine("la cadena leida es {0}", cadena) 'Console.ReadLine() ' lectura del archivo Dim lector As StreamReader = New StreamReader(nombrearchivo) Console.WriteLine(" leido del archivo ") cadena = lector.ReadLine()

Page 93: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 93 -

Do While Not (cadena Is Nothing) Console.WriteLine(" {0}", cadena) cadena = lector.ReadLine() Loop lector.Close() Console.ReadLine() End SubEnd Module

Archivovo modificadoImports System.IOModule Module1 Const maximo As Integer = 100 Dim NombreArchivo As String = "E:\DATOS\texto1.txt" Dim opcion As Integer Dim cadena(maximo) As String Dim cont, nf As Integer Dim linea As String Sub Main() Dim i As Integer Do Console.WriteLine("1 Ingresar 2. Mostrar 3. Grabar 4 Recuperar 5.Salir ") Console.Write("Ingrese opcion ==> ") opcion = Console.ReadLine Select Case opcion Case 1 Dim linea As String cont = 0 linea = Console.ReadLine Do While linea <> "fin" cadena(cont) = linea linea = Console.ReadLine() cont = cont + 1 Loop nf = cont Case 2 Console.WriteLine(" el texto ingresado es ") For i = 0 To nf - 1 Console.WriteLine("{0}", cadena(i)) Next Case 3 Dim escritor As StreamWriter escritor = New StreamWriter(NombreArchivo) For i = 0 To nf - 1 escritor.WriteLine("{0}", cadena(i)) Next Console.WriteLine(" archivo {0} grabado ", NombreArchivo) escritor.Close() Case 4 Dim lector As StreamReader Dim linea As String

Page 94: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 94 -

lector = New StreamReader(NombreArchivo) cont = 0 linea = lector.ReadLine Do While Not (linea Is Nothing) cadena(cont) = linea linea = lector.ReadLine() cont = cont + 1 Loop nf = cont lector.Close() Console.WriteLine(" archivo {0} recuperado ", NombreArchivo) End Select Loop While opcion <> 5 End SubEnd Module

PRACTICA DE SI 06 DE MAYO DEL 2015

Sub Main() Console.WriteLine("HOLA MUNDO") ' EN el archivo Dim archivo As StreamWriter archivo = New StreamWriter("E:\datos\tarea1.txt") archivo.WriteLine("HOLA MUNDO") archivo.WriteLine("el archivo a sido grabado") Console.WriteLine("el archivo a sido grabado") archivo.Close() Console.ReadLine() End SubEnd Module

Imports System.IOModule Module1 Sub Main() Dim NombreArchivo As String = "E:\datos\funcion.txt" ' mostrando en la consola Console.WriteLine("Arequipa 06 de mayo del 2015") Console.WriteLine("Ingenieria Industrial") Console.WriteLine(" del 2015") Console.ReadLine() ' mostrando en el archivo Dim arch As StreamWriter = New StreamWriter(NombreArchivo)

Page 95: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 95 -

arch.WriteLine("Arequipa 06 de mayo del 2015") arch.WriteLine("Ingenieria Industrial") arch.WriteLine(" del 2015") arch.Close() Console.ReadLine() End SubEnd Module

Ejercicio de lectura de archivos

Imports System.IOModule Module1 Sub Main() Dim NombreArchivo As String = "E:\datos\funcion.txt" ' mostrando en la consola Dim cadena As String ' lectura del archivo Dim arch As StreamReader = New StreamReader(NombreArchivo) cadena = arch.ReadLine() Do While Not (cadena Is Nothing) Console.WriteLine(" {0}", cadena) cadena = arch.ReadLine() Loop arch.Close() Console.ReadLine() End SubEnd Module

Recuperar la casita

Imports System.IOModule Module2 Public A(24, 24) As Integer Public nf As Integer = 23, nc As Integer = 21 Public NombreArchivo As String = "E:\DATOS\casita21x23.txt" Public maximo As Integer = 15 Public minimo As Integer = 0 Public opcion As Integer

Function menu() As Integer Dim op As Integer Console.ForegroundColor = 15 Console.Clear() Console.SetCursorPosition(1, 1) Console.Write("1. Generar 2: Mostrar 3. Recuperar 4. Grabar 5 salir ==opcion==> ") op = Console.ReadLine() Return op End Function

Sub GenerarMatriz(A(,) As Integer, nf As Integer, nc As Integer) Dim fila, col As Integer For fila = 0 To nf - 1

Page 96: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 96 -

For col = 0 To nc - 1 A(fila, col) = Int(Rnd() * maximo) Next Next End Sub Sub MostrarMatriz(cx As Integer, cy As Integer, A(,) As Integer, nf As Integer, nc As Integer, ex As Integer) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 Console.SetCursorPosition(cx + col * ex, cy + fila) Console.ForegroundColor = A(fila, col) Console.Write("{0}", Hex(A(fila, col))) Next Next End Sub Sub GrabarMatriz(Nombre As String, A(,) As Integer, nf As Integer, nc As Integer) Dim swEscritor As StreamWriter swEscritor = New StreamWriter(NombreArchivo) Dim fila, col As Integer For fila = 0 To nf - 1 For col = 0 To nc - 1 swEscritor.Write("{0}{1}", A(fila, col), vbTab) Next swEscritor.WriteLine() Next swEscritor.Close() End Sub

Sub RecuperarMatriz(ByVal nombrearchivo As String, ByVal A(,) As Integer, ByVal nf As Integer, ByVal nc As Integer) Dim srLector As StreamReader srLector = New StreamReader(nombrearchivo) Dim fila As Integer, col As Integer Dim cadena As String = "" Dim subcadena As String Dim pos As Integer = 0 Dim inicio As Integer = 1 For fila = 0 To nf - 1 cadena = srLector.ReadLine() cadena = cadena & Chr(9) inicio = 1 For col = 0 To nc - 1 pos = InStr(inicio, cadena, Chr(9)) subcadena = Mid(cadena, inicio, pos - inicio) A(fila, col) = Val(subcadena) inicio = pos + 1 Next Next srLector.Close() End SubEnd Module

Page 97: Visual Basic 2012 Modo Consola

Sistemas de Informacion 2015A \1. Modo Consola\ I.Véliz Vilca - 97 -

FolderBrowserDialog1

Public Class Form1 Dim grafico As Graphics Dim ancho As Integer = 255 Dim alto As Integer = 255 Dim brocha As SolidBrush Dim pincel As Pen

Private Sub btnColor_Click(sender As Object, e As EventArgs) Handles btnColorDialogo.Click ColorDialog1.ShowDialog() Panel1.BackColor = ColorDialog1.Color

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load grafico = PictureBox1.CreateGraphics

brocha = New SolidBrush(Color.Red) pincel = New Pen(Brushes.AliceBlue) PictureBox1.Width = ancho * 3 PictureBox1.Height = alto * 2 End Sub Private Sub BtnColor_Click_1(sender As Object, e As EventArgs) Handles BtnColor.Click Dim rx As Integer = 0 Dim ry As Integer = 0 Dim vx As Integer = 128 Dim vy As Integer = 128 Dim ax As Integer = 255 Dim ay As Integer = 128 pincel.Color = Color.FromArgb(255, 0, 0) grafico.DrawEllipse(pincel, rx, ry, ancho, alto) pincel.Color = Color.FromArgb(0, 255, 0) grafico.DrawEllipse(pincel, vx, vy, ancho, alto) pincel.Color = Color.FromArgb(0, 0, 255) grafico.DrawEllipse(pincel, ax, ay, ancho, alto) End Sub

End Class