Mostrando las entradas con la etiqueta java. Mostrar todas las entradas
Mostrando las entradas con la etiqueta java. Mostrar todas las entradas
on lunes, 11 de agosto de 2014
Bueno ahora en clase de Est. & Org. de Datos, nos pidieron hacer una matriz diferente a lo comun oseasemelese
comun:
int matriz[][] = new int[3][3];
haciendo asi que 3 vectores, contengan 3 campos, pero en este caso se decea que cada vector o renglon contenga mas o menos campos, segun

quedando algo asi
int matriz[][] = new int[3][];
matriz[0] = new int[2];
matriz[1] = new int[4];
matriz[2] = new int[7];

Asi podriamos poner a cada uno exactamente como lo deceemos, pero bueno ya el punto era que deceabamos hacer eso y cada renglon representa un alumno y columna materia, y entonces sacar el promedio por materia, pero en ocaciones un alumno lleva menos materias que otro por lo que tendriamos que checar que tal alumno cumpla con tales sino que no lo lea porque daria un error de fuera de rango si no tuviera tal materia.

Bueno ya al final de todo aqui lo hice aunque me falto comentarlo jeje ;D
Ahi cualquier duda o cosa y/o Sugerencia o algun error que vean me avisan asi tambien mejorar jejeje xP

public class Matriz 
{
 public static void main(String[] args) 
 {
  int alumnos, cal, mcal = 0;
  do{
   alumnos = read.dataInt("Cantidad de Alumnos: ");
  }while(alumnos < 0);
  int matriz [][] = new int[alumnos][];
  for(int x = 0; x < alumnos; x++)
  {
   cal = read.dataInt("Cantidad de Materias Alumno[" + (x+1) + "]: ");
   matriz[x] = new int[cal];
   if(mcal < cal)
    mcal = cal;
  }
  SOP("\n");
  matriz = Rellenar(matriz,mcal);
  SacarPromedioXMateria(matriz,mcal);
  SacarPromedioXAlumno(matriz,mcal);
 }
 
 private static int [][] Rellenar(int matriz[][], int mcal)
 {
  for(int x = 0; x < mcal; x++)
  {
   SOP("Ingreso de Calificaciones Materia [" + (x+1) + "]: ");
   for(int xx = 0; xx < matriz.length; xx++)
   {
    if(matriz[xx].length >= (x+1))
     matriz[xx][x] = read.dataInt("Calificacion Alumno [" + (xx+1) + "]: ");
   }
   SOP("\n");
  }
  return matriz;
 }
 
 private static void SacarPromedioXMateria(int matriz [][], int mcal)
 {
  int prom;
  for(int x = 0; x < mcal; x++)
  {
   prom = 0;
   for(int xx = 0; xx < matriz.length; xx++)
   {
    if(matriz[xx].length >= (x+1))
     prom += matriz[xx][x];
   }
   prom = prom/matriz.length;
   SOP("Promedio Materia [" + (x+1) + "]: " + prom);
  }  
 }
 
 private static void SacarPromedioXAlumno(int matriz [][], int mcal)
 {
  int prom;
  for(int x = 0; x < matriz.length; x++)
  {
   prom = 0;
   for(int xx = 0; xx < matriz[x].length; xx++)
   {
    prom += matriz[x][xx];
   }
   prom = prom/mcal;
   SOP("Promedio Alumno [" + (x+1) + "]: " + prom);
  }
 }
 
 private static void SOP(String msj)
 {
  System.out.println(msj);
 }

}
Clase PilaS:
package Pila;

public class PilaS 
{
 private int Tope = -1;
 private int Max;
 private int Pila[];
 public int DatoPila;
 
 public PilaS(int CantMax)
 {
  Pila = new int[CantMax];
  Max = CantMax;
 }
 
 private boolean PilaVacia()
 {
  boolean resp = false;
  if(Tope == -1)
   resp = true;
  return resp;
 }
 
 private boolean PilaLlena()
 {
  boolean resp = false;
  if((Tope-1) == Max)
   resp = true;
  return resp;
 }
 
 public boolean InsertarPila(int Dato)
 {
  boolean resp = false;
  if(!PilaLlena())
  {
   Tope++;
   Pila[Tope] = Dato;
   resp = true;
  }
  return resp; 
 }
 
 public boolean RemoverPila()
 {
  boolean resp = false;
  if(!PilaVacia())
  {
   DatoPila = Pila[Tope];
   Tope--;
   resp = true;
  }
  return resp;
 }
 
 public boolean VerPila()
 {
  boolean resp = false;
  if(!PilaVacia())
  {
   DatoPila = Pila[Tope];
   resp = true;
  }
  return resp;
 }

}

Clase ProyectosPilas:
Este contiene el main y demas funciones.
package Pila;

public class ProyectosPilas 
{

 public static void main(String[] args) 
 {  
  //Pila Normal Mostrar Elementos inverso a como fueron insertados
  Ej0();
  //Pila Insertamos y mostramos en otra Pila de forma que fueron insertados 
  Ej1();
  //Pila Insertamos y mostramos en la Pila Principal o 0 de forma que fueron insertados
  Ej2();
 }
 
 private static void Ej0()
 {
  int num = read.dataInt("Total de Elementos en la Pila: ");
  PilaS P = new PilaS(num);
  for(int x = 0; x < num; x++)
  {
   P.InsertarPila(read.dataInt("Dato a Insertar[" + x + "]:"));
  }
  
  while(P.RemoverPila())
   SOP(P.DatoPila);
 }
 
 private static void Ej1()
 {
  int num = read.dataInt("Total de Elementos en la Pila: ");
  PilaS P = new PilaS(num);
  PilaS P0 = new PilaS(num);
  for(int x = 0; x < num; x++)
  {
   P.InsertarPila(read.dataInt("Dato a Insertar[" + x + "]:"));
  }
  
  while(P.RemoverPila())
   P0.InsertarPila(P.DatoPila);
  while(P0.RemoverPila())
   SOP(P0.DatoPila);
 }
 
 private static void Ej2()
 {
  int num = read.dataInt("Total de Elementos en la Pila: ");
  PilaS P = new PilaS(num);
  PilaS P0 = new PilaS(num);
  for(int x = 0; x < num; x++)
  {
   P.InsertarPila(read.dataInt("Dato a Insertar[" + x + "]:"));
  }

  while(P.RemoverPila())
   P0.InsertarPila(P.DatoPila);
  //P = P0;
  
  PilaS P1 = new PilaS(num);
  while(P0.RemoverPila())
   P1.InsertarPila(P0.DatoPila);
  while(P1.RemoverPila())
   P.InsertarPila(P1.DatoPila);
  
  while(P.RemoverPila())
   SOP(P.DatoPila);
 }
 
 private static void SOP(int msj)
 {
  System.out.println(msj);
 }

}
Clase read: Esta clase no la cree yo sino un amigo Jackz(cryptt3r) con la que siempre utilizo para leer jeje
package Pila;
import java.io.*;

/**
 * @author cryptter
 */
public class read
{
 public static boolean error;
 public static String data(String message) {
     String sdato = "";
     System.out.print(message);
     try {
       // Definir un flujo de caracteres de entrada: flujoE
       InputStreamReader isr = new InputStreamReader(System.in);
       BufferedReader flujoE = new BufferedReader(isr);
       // Leer. La entrada finaliza al pulsar la tecla Entrar
       sdato = flujoE.readLine();
     }
     catch(IOException e) {
       System.err.println("Error: " + e.getMessage());
     }
     return sdato; // devolver el dato tecleado
   }
 public static int dataInt(String message){
     try {
      error=false;
      int val=Integer.parseInt(data(message));
      if (val<-32768 data-blogger-escaped-val="">32767)
       error=true;
       return val;
     }
     catch(NumberFormatException e) {
       return Integer.MIN_VALUE; // valor más pequeño
     }
 }

 public static short datoShort(String message){
    try {
   return Short.parseShort(data(message));
  }
  catch (NumberFormatException e){
   return Short.MIN_VALUE;
  }
 }



 public static long dataLong(String message){
  try {
   return Long.parseLong(data(message));
  }
  catch (NumberFormatException e){
   return Long.MIN_VALUE;
  }
 }

 public static float dataFloat(String message){
  try {
   Float f = new Float(data(message));
   return f.floatValue();
  }
  catch (NumberFormatException e){
   return Float.NaN;
  }
 }

 public static double dataDouble(String message){
  try {
   Double d = new Double(data(message));
   return d.doubleValue();
  }
  catch (NumberFormatException e){
   return Double.NaN;
  }
 }
 /*public static char dataChar(String message){
  try {
   Char d = new Char(data(message));
   return d.charValue();
  }
  catch (NumberFormatException e){
   return Char.NaN;
  }
 }*/
}