1 / 10

IWI-131 PROGRAMACION DE COMPUTADORES UTFSM – Campus Santiago

IWI-131 PROGRAMACION DE COMPUTADORES UTFSM – Campus Santiago. Ejercicios Certamen III. Claudio O’Ryan Ingeniero Civil En informática Coryan@gasatacama.cl. IWI-131 PROGRAMACION DE COMPUTADORES UTFSM – Campus Santiago.

aimee-cobb
Download Presentation

IWI-131 PROGRAMACION DE COMPUTADORES UTFSM – Campus Santiago

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IWI-131 PROGRAMACION DE COMPUTADORES UTFSM – Campus Santiago Ejercicios Certamen III Claudio O’Ryan Ingeniero Civil En informática Coryan@gasatacama.cl

  2. IWI-131 PROGRAMACION DE COMPUTADORES UTFSM – Campus Santiago 1. Archivos de texto ancho fijo.Codificar un programa Java que permita generar un archivo de texto con campos de ancho fijo “sueldos.txt”, con los registros de sueldo de los N trabajadores de una empresa desde pantalla con la siguiente estructura: Nombre 20 sueldo 10 descuentos 10Posteriormente, se debe generar un archivo “pagos.txt” que contenga el nombre y el sueldo líquido, ambos de ancho fijo 20 y 10 respectivamente.

  3. public class sueldos{ static int N=1; static String ESPACIOS=" "; static void main(String[] args) { String reg, nombre, sueldo, descuentos; Double sueldo_num, descuentos_num; //creacion del archivo de sueldos Out salida = new Out("c:\\prueba\\sueldos.txt"); for (int i=1;i<=N;i++) { StdOut.println("Ingrese Nombre de Trabajador:"); nombre=StdIn.readLine(); StdOut.println("Ingrese sueldo base:"); sueldo_num=StdIn.readDouble(); StdOut.println("Ingrese Descuentos:"); descuentos_num=StdIn.readDouble(); StdIn.readLine(); if (nombre.length() >=20) nombre=nombre.substring(0,20); else nombre=nombre+ESPACIOS.substring(0,20-nombre.length()); sueldo=Double.toString(sueldo_num); if (sueldo.length() >=10) sueldo=sueldo.substring(0,10); else sueldo=ESPACIOS.substring(0,10-sueldo.length())+sueldo; descuentos=Double.toString(descuentos_num); if (descuentos.length() > 10) descuentos=descuentos.substring(0,10); else descuentos=ESPACIOS.substring(0,10-descuentos.length())+descuentos; reg=nombre+sueldo+descuentos; salida.println(reg); } salida.close();

  4. //creacion de archivo de pagos In entrada=new In("c:\\prueba\\sueldos.txt"); Out salida_final=new Out("C:\\prueba\\pagos.txt"); Double liquido_num; String liquido; while (!entrada.isEmpty()) { reg=entrada.readLine(); nombre=reg.substring(0,20); sueldo=reg.substring(20,30); descuentos=reg.substring(30,40); liquido_num=Double.valueOf(sueldo.trim())-Double.valueOf(descuentos.trim()); liquido=Double.toString(liquido_num); if (liquido.length() >= 10) liquido=liquido.substring(0,10); else liquido=ESPACIOS.substring(0,10-liquido.length()) + liquido; reg=nombre+liquido; salida_final.println(reg); } salida_final.close(); entrada.close(); } }

  5. IWI-131 PROGRAMACION DE COMPUTADORES UTFSM – Campus Santiago 2. Archivos texto con campos separados por coma.Codificar un programa Java que permita grabar un archivo de texto “inventario.txt” con campos separados por coma, para los items de bodega, generando, los siguientes campos por línea: Nombre char[ ] Precio unitario Double stock DoublePosteriormente, se debe generar un archivo “vinventario.txt” con el nombre y valor del inventario de cada item, también separados por coma.

  6. public class inventario{ static char coma=','; static int n=2; public static void main(String[] args) { Out salida= new Out("c:\\prueba\\inventario.txt"); String nombre, precio, stock; Double precio_num,stock_num; for (int i=1; i<=n;i++) { StdOut.println("Ingrese nombre de producto:"); nombre=StdIn.readLine(); StdOut.println("Ingrese precio:"); precio_num=StdIn.readDouble(); StdOut.println("Ingrese stock:"); stock_num=StdIn.readDouble(); salida.println(nombre+coma+precio_num+coma+stock_num); StdIn.readLine(); } salida.close(); In entrada=new In("c:\\prueba\\inventario.txt"); Out valor=new Out("c:\\prueba\\valor.txt"); String[] campos; Double dinero; String reg; while (!entrada.isEmpty()) { reg=entrada.readLine(); campos=reg.split("\\,"); dinero=Double.valueOf(campos[1]) * Double.valueOf(campos[2]); valor.println(campos[0]+coma+Double.toString(dinero)); } entrada.close(); valor.close(); } }

  7. 3. Archivos texto con un campo por línea.Se tiene un archivo de texto notas.txt con un campo por línea de la siguiente forma: Nombre (string) - Línea 1 Apellido (string) - Línea 2 Carrera (string) - Línea 3 Notas  (int) - Línea 4 El valor de este campo define cuantas líneas con Nota1 notas vienen a continuación. Nota2 .......................................... Se solicita generar el archivo promedios.txt con un campo por línea de la siguiente forma: Nombre(string) - Línea 1 promedio (int) - Línea 2 .........................................  IWI-131 PROGRAMACION DE COMPUTADORES UTFSM – Campus Santiago

  8. public class notas{ public static void main(String[] args) { In ent=new In("c:\\notas.txt"); Out sal=new Out("c:\\promedios.txt"); String nombre, apellido, carrera; int nota, promedio, suma, notas; while (!ent.isEmpty()) { nombre=ent.readString(); System.out.println("nombre:"+nombre); apellido=ent.readString(); carrera=ent.readString(); notas=ent.readInt(); suma=0; for (int i=1;i<=notas;i++) { nota=ent.readInt(); suma+=nota; } promedio=suma/notas; sal.println(nombre); sal.println(promedio); } sal.close(); ent.close(); } }

  9. 4. Archivos texto con campos separados por espacios.Se tiene un archivo de texto quices.txt con los campos separados por espacios y el siguiente formato en cada línea: Nombre Apellido nota1 nota2 nota3 nota 4 nota5 .......................................... Se solicita generar el archivo promquices.txt con el siguiente formato: Nombre apellido promedio .........................................  Todos los valores deben ser tratados como enteros. IWI-131 PROGRAMACION DE COMPUTADORES UTFSM – Campus Santiago

  10. public class quices{ static int n=5; public static void main(String[] args) { In entrada = new In("c:\\prueba\\quices.txt"); Out salida=new Out("c:\\prueba\\promquices.txt"); int promedio, nota,suma; String nombre, apellido; while (!entrada.isEmpty()) { nombre=entrada.readString(); apellido=entrada.readString(); suma=0; for (int i=1;i<=n;i++) { nota=entrada.readInt(); suma+=nota; } promedio=suma/n; salida.println(nombre+" "+apellido+" "+promedio); } entrada.close(); salida.close(); } }

More Related