miércoles, 4 de mayo de 2011

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");
   }
}

No hay comentarios:

Publicar un comentario