miércoles, 4 de mayo de 2011

MATRICES

package practicamatrices;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class MATRICES {
    public int n;
    public int [][] x;
   
    public MATRICES(int n){
        this.n=n;
        x = new int[n][n];
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                x[i][j]=0;
                }
             }
   }
    public MATRICES(int [][] x){
        this.x=x;
        n= x.length;
    }
    public String toString(){
        String texto="";
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                texto += "\t"+(int)Math.round(1000*x[i][j])/1000;
                }
                texto += "\n";
            }
        return texto;
    }
    public static int leer(BufferedReader buff){
        int lee=0; boolean error;
        do{
            error = false;
            try{
                lee = Integer.parseInt(buff.readLine());}
            catch (NumberFormatException ex){
                System.out.println("SE ha producido un error");
                error = true;
            }
            catch (Exception ex){ex.printStackTrace(System.err);}
        }while(error);
        return lee;
    }
    public static void cargar(int v[][]){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        for(int i=0; i<v.length; i++){
            for( int j = 0; j < v.length; j ++ ){
            System.out.print("Cargar vector: = ");
            v[i][j]=leer(br);
            }
       }
    }
    public static void mostrar(int v[][]){
       for(int i=0; i<v.length; i++){
            for(int  j=0;j< v.length; j++){
           System.out.print("\t"+v[i][j]);
           }
             System.out.println();
        }
    }
  public static double Diagonal(int [][] x){
     double diagonal=0;
     for(int i=0;i<x.length;i++){
        diagonal = diagonal + x[i][i];
     }
        return diagonal;
    }
  public static int mayor( int V[ ][ ] ){
    int mayor =0;
    for( int i = 0; i < V.length; i ++ ){
        for( int j = 0; j < V.length; j ++ ){
            if(V[i][j] > mayor){
                mayor = V[i][j];
            }
        }
     }
    return mayor;
}
public static int menor( int V[ ][ ]){
    int menor = mayor(V);
    for( int i = 0; i < V.length; i ++ ){
        for( int j = 0; j < V.length; j ++ ){
            if(V[i][j] < menor){
                menor = V[i][j];
            }
        }
     }
    return menor;
}
public static void ordenar(int v[][]){
    for(int  i=0; i < v.length; i++){//ordena la matriz de abajo hacia arriba
           for(int  j=0;j< v.length; j++){
              for(int x = 0; x < v.length; x++){
                  for(int y=0; y < v.length ; y++){
                     if(v[i][j] > v[x][y]){
                       int aux = v[i][j];
                       v[i][j] = v[x][y];
                       v[x][y] = aux;
                     }
                  }
              }
          }
      }
}
public static void invertida(int v[][]){
    for(int  i=0; i < v.length; i++){//ordena la matriz de abajo hacia arriba
           for(int  j=0;j< v.length; j++){
              for(int x = 0; x < v.length; x++){
                  for(int y=0; y < v.length ; y++){
                     if(v[i][j] < v[x][y]){
                       int aux = v[i][j];
                       v[i][j] = v[x][y];
                       v[x][y] = aux;
                     }
                  }
              }
          }
      }
}
    public static int Par(int v[][]){
      int cantidad =0;
      for(int i=0; i<v.length; i++){
           for(int  j=0;j< v.length; j++){
             if(v[i][j] % 2 == 0)
             cantidad++;
           }
        }
    return cantidad;
    }
   public static int Impar(int v[][]){
      int cantidad =0;
      for(int i=0; i<v.length; i++){
           for(int  j=0;j< v.length; j++){
          if(v[i][j] % 2 != 0)
          cantidad++;
      }
   }
    return cantidad;
    }
   public static double buscar(int V[][], int elem){
   int pos =0;
    for( int i = 0; i < V.length; i ++ ){
        for(int  j=0;j< V.length; j++)
        if(V[ i ][j] == elem){
            pos ++;
        }
    }
   if( pos >= 1 ){
       return 0;
   }else
   return -1;
}
   public static int Igualdad(int v[][], int m[][]){
        double pos=0 ;
        for(int i=0; i<v.length; i++){
            for(int  j=0;j< v.length; j++)
            if(v[i][j]==m[i][j]){
                pos++;
        }
    }
       if( pos == v.length*v.length ){
       return 1;
   }else
   return 0;
    }
    public static void main(String [] args){
        System.out.println("Tamaño de nuestra matriz Cuadrada: ");
        Scanner e= new Scanner(System.in);
        int n = e.nextInt();
        int v[][] = new int[n][n];
        int a[][] = new int[n][n];
        int x[][] = new int[n][n];
        System.out.println("Llenar matriz A");
        cargar(v);
        System.out.println();
        System.out.println("Elemento a buscar en la matriz A: ");
        Scanner f= new Scanner(System.in);
        int elem = f.nextInt();
       System.out.println("Llenar matriz B");
       cargar(a);
       System.out.println();
       System.out.println("Cantidad de pares matriz A ="+ Par(v));
       System.out.println("Cantidad de impares matriz A ="+ Impar(v));
       System.out.println("El mayor matriz A es = "+ mayor(v));
       System.out.println("El menor es matriz A = "+ menor(v));
       System.out.println("La suma de la diagonal matriz A es = "+ Diagonal(v));
       if(buscar(v,elem)==-1){
       System.out.println("No se encentra el elemento.");
       }else
           System.out.println("Si se encuentra el elemento. ");
       System.out.println();
       System.out.println("Pares hay ="+ Par(a));
       System.out.println("Impares hay ="+ Impar(a));
       System.out.println("El mayor matriz es = "+ mayor(a));
       System.out.println("El menor es matriz = "+ menor(a));
       System.out.println("La suma de la diagonal es = "+ Diagonal(a));
       System.out.println();
       System.out.println("Matriz A Invertida ");
       ordenar(v);
       mostrar(v);
       System.out.println();
       System.out.println("Matriz A Ordednada");
       invertida(v);
       mostrar(v);
       System.out.println();
       System.out.println("Matriz B Invertida ");
       ordenar(a);
       mostrar(a);
       System.out.println();
       System.out.println("Matriz B Ordenada");
       invertida(a);
       mostrar(a);
       if(Igualdad(v,a)==1){
           System.out.println("si son iguales");
       }else
     System.out.println("no son iguales");
    }
}

VECTORES

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;   
Class VECTOR{
    public int n;//Tam
    public double [] x; //Arreglo
   
    public VECTOR(int n){//SOBRECARGA DEL METODO
        this.n=n;
        x = new double[n];
            for(int i=0;i<n;i++){
                x[i]=0;
        }
    }
    public VECTOR(double [] x){//SOBRECARGA DEL METODO
        this.x=x;
        n= x.length;
    }
    public String toString(){//REDEFINICION DE METODO
        String texto="";
            for(int i=0;i<n;i++){
                texto += "\t"+(double)Math.round(1000*x[i])/1000;
            }
            texto += "\n";
        return texto;
    } 
public static int leer(BufferedReader buff){
        int lee=0; boolean error;
        do{
            error = false;
            try{
                lee = Integer.parseInt(buff.readLine());}
            catch (NumberFormatException ex){
                System.out.println("SE ha producido un error");
                error = true;
            }
            catch (Exception ex){ex.printStackTrace(System.err);}
        }while(error);
        return lee;
    }
    public static void cargar(double v[]){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        for(int i=0; i<v.length; i++){
            System.out.print("Cargar vector: = ");
            v[i]=leer(br);
        }
    }
    public static void mostrar(double v[]){
       for(int i=0; i<v.length; i++){
           System.out.println("["+i+"]= "+v[i]);
           }
    }
    public static int Par(double v[]){
      int cantidad =0;
      for(int i=0; i<v.length; i++){
        if(v[i] % 2 == 0)
          cantidad++;
      }
    return cantidad;
    }
   public static int Impar(double v[]){
      int cantidad =0;
      for(int i=0; i<v.length; i++){
          if(v[i] % 2 != 0)
          cantidad++;
      }
    return cantidad;
    }
public static void Ordenar( double V[ ]){
 for( int i = 0; i < V.length; i ++ ){
    for( int j = 0; j < V.length; j ++ ){
     if(V[ i ] < V[ j ]){
      double aux = V[ i ];
       V[ i ] = V[j];
       V[ j ] = aux;
      }else if(V[ i ] > V[ j ]){
       double aux = V[ i ];
       V[ i ] = aux;
       V[ j ] = V[j];}
    }
   }
 }
     public  static void Invertir(double V[]){
      for( int i = 0; i < V.length; i ++ ){
    for( int j = 0; j < V.length; j ++ ){
     if(V[ i ] > V[ j ]){
      double aux = V[ i ];
       V[ i ] = V[j];
       V[ j ] = aux;
      }else if(V[ i ] < V[ j ]){
       double aux = V[ i ];
       V[ i ] = aux;
       V[ j ] = V[j];}
    }
   }
   }
  public static double Mayor(double v[]){
      double mayor =0;
      for(int i=0; i<v.length; i++){
         mayor = ((v[i] < mayor)? mayor : v[i]);
      }
    return mayor;
    }
    public static double Menor(double v[]){
      double menor =0;
      for(int i=0; i<v.length; i++){
         menor = ((v[i] > menor)? menor : v[i]);
      }
    return menor;
    }
    public static double Igual(double v[], double m[]){
        for(int i=0; i<v.length; i++){
            if(v[i]!=m[i])
                return 0;
        }
        return 1;
    }
   public static void main(String[] args) {
       int n = 5;
       double [] v= new double[n];
       double [] m= new double[n];
       System.out.println("llenando vector A");
       cargar(v);
       System.out.println("llenando vector B");
       cargar(v);
      
       System.out.println("Cantidad de pares ="+ Par(v));
       System.out.println("Cantidad de impares ="+ Impar(v));
       System.out.println("El mayor es = "+ Mayor(v));
       System.out.println("El menor es ="+ Menor(v));
       System.out.println("dkjlajdkj");
       Ordenar(v);
       mostrar(v);
       System.out.println("dñjadlñajlkfa6");
       Invertir(v);
       mostrar(v);
       if(Igual(v,m)==1){
           System.out.println("no");}
 else
     System.out.println("si");
   }
}