Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Plain Text by lenam ( 13 years ago )
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exam1;

import java.util.Scanner;
import java.util.InputMismatchException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


/**
 *
 * @author vaio
 */
public class Exam1 {
    
    public enum llista {CREA, ESBORRA, SIGNE, TRANSPOSA, SUMA,
    RESTA, MULTIPLICA, INTERCANVIA, IMPRIMEIX, LLEGIR, GUARDAR, SORTIR}
    
    public static double[][] A = new double[0][0];
    public static double[][] B = new double[0][0];

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        
        boolean continua = true;
        llista opcio;
        do{
            System.out.println("1. CREA: Introdueix una nova matriu A.");
            System.out.println("2. ESBORRA: Buida la matriu A.");
            System.out.println("3. SIGNE: Calcula la oposada de la matriu A i desa en B.");
            System.out.println("4. TRANSPOSA: Calcula de transposada de la matriu A i desa en B.");
            System.out.println("5. SUMA.");
            System.out.println("6. RESTA.");
            System.out.println("7. MULTIPLICA.");
            System.out.println("8. INTERCANVIA.");
            System.out.println("9. IMPRIMEIX.");
            System.out.println("10. LLEGIR.");
            System.out.println("11. GUARDAR.");
            System.out.println("12. SORTIR.");
            System.out.println("Quina vols realitzar?");
            
            Scanner entrada = new Scanner (System.in);
            opcio = llista.CREA;
            int vullfer;
            
            do{
                vullfer = entrada.nextInt();
                
                if (vullfer==1) opcio = llista.valueOf("CREA");
                else if (vullfer==2) opcio = llista.valueOf("ESBORRA");
                else if (vullfer==3) opcio = llista.valueOf("SIGNE");
                else if (vullfer==4) opcio = llista.valueOf("TRANSPOSA");
                else if (vullfer==5) opcio = llista.valueOf("SUMA");
                else if (vullfer==6) opcio = llista.valueOf("RESTA");
                else if (vullfer==7) opcio = llista.valueOf("MULTIPLICA");
                else if (vullfer==8) opcio = llista.valueOf("INTERCANVIA");
                else if (vullfer==9) opcio = llista.valueOf("IMPRIMEIX");
                else if (vullfer==10) opcio = llista.valueOf("LLEGIR");
                else if (vullfer==11) opcio = llista.valueOf("GUARDAR");
                else if (vullfer==12) opcio = llista.valueOf("SORTIR");
                
            } while (vullfer<=0 || vullfer>12);
            
            switch(opcio) {
                
                case CREA:
                    int numfiles = numfiles();
                    int numcolum = numcolum();
                    A = intromatriu (numfiles, numcolum);
                    break;
                
                case ESBORRA:
                    if (esborra()==false) System.err.println("No hi ha matriu per operar.");
                    break;
                
                case SIGNE:
                    if (canvisigne()==false) System.err.println("No hi ha matriu per operar.");
                    break;
                    
                case TRANSPOSA:
                    if (transposa()==false) System.err.println("No hi ha matriu per operar.");
                    break;
                    
                case SUMA:
                    System.out.println("La matriu A és:");
                    imprimatriu(A);
                    System.out.println("La matriu B és:");
                    imprimatriu(B);
                    if (suma()==true) System.out.println("La matriu resultat, desada en B és:");
                    imprimatriu(B);
                    if (suma()==false) System.err.println("Les matrius no es poden operar.");
                    break;
                    
                case RESTA:
                    System.out.println("La matriu A és:");
                    imprimatriu(A);
                    System.out.println("La matriu B és:");
                    imprimatriu(B);
                    if (resta()==true) System.out.println("La matriu resultat, desada en B és:");
                    imprimatriu(B);
                    if (resta()==false) System.err.println("Les matrius no es poden operar.");
                    break;
                    
                case MULTIPLICA:
                    System.out.println("La matriu A és:");
                    imprimatriu(A);
                    System.out.println("La matriu B és:");
                    imprimatriu(B);
                    if (producte()==true) System.out.println("La matriu resultat, desada en B és:");
                    imprimatriu(B);
                    if (producte()==false) System.err.println("Les matrius no es poden operar.");
                    break;
                    
                case INTERCANVIA:
                    if (intercanvi()==false) System.err.println("Les matrius no es poden intercanviar.");
                    break;
                    
                case IMPRIMEIX:
                    System.out.println("La matriu A és:");
                    imprimatriu(A);
                    System.out.println("La matriu B és:");
                    imprimatriu(B);
                    break;
                    
                /*case LLEGIR:
                    llegeix();
                    break;
                 */   
                case GUARDAR:
                    guardar(A);
                    break;
                    
                case SORTIR:
                    continua = false;
                    System.out.println("Sortint del programa.");
                    break;
                    
                default:
                    System.err.println("Error.");
            }
             
        } while(continua);
    }
    
    
    
    public static int numfiles () {
        int numfiles;
        do {
            try {
                Scanner entrada = new Scanner (System.in);
                System.out.println("Introdueix el número de fils de la matriu. Enter entre 1 i 15.");
                numfiles = entrada.nextInt();
                if (numfiles<=0 || numfiles>15) System.err.println("El número de files no és vàlid."); 
            } catch (InputMismatchException ime) {
                System.err.println("El número ha de ser un enter entre 1 i 15.");
                numfiles = 0;
            }
    
        } while (numfiles<=0 || numfiles>15);
        return numfiles;
    }
    
    
    
    public static int numcolum() {
        int numcolum;
        do {
            try {
                Scanner entrada = new Scanner (System.in);
                System.out.println("Introdueix el número de columnes de la matriu. Enter entre 1 i 15.");
                numcolum = entrada.nextInt();
                if (numcolum<=0 || numcolum>15) System.err.print("El número de columnes no és vàlid.");
            } catch (InputMismatchException ime) {
                System.err.println("El número ha de ser un enter enter 1 i 15.");
                numcolum = 0;
            }
  
        } while (numcolum<=0 || numcolum>15);
        return numcolum;   
    }
    
    
    
    public static double[][] intromatriu (int numfiles, int numcolum){
        Scanner entrada = new Scanner (System.in);
        A = new double [numfiles][numcolum];
        System.out.println("Introdueix els valors de la matriu A.");
        boolean error;
        for (int i=0; i<numfiles; ++i) {
            for (int j=0; j<numcolum; ++j){
                do {
                    try {
                        error=false;
                        System.out.println("Element de la fila " + (i+1) + " i columna " + (j+1) + ":");
                        A[i][j] = entrada.nextDouble();
                    } catch (InputMismatchException ime) {
                        System.err.println("Valor no correcte. Introdueix un de nou.");
                        error=true;
                    }
                } while (error);  
            } 
        } 
        return A;
    }
    
    
    
    public static boolean esborra () {
        boolean correcte = true;
        if (A.length ==0) correcte = false;
        else A = new double [0][0];
        return correcte;
    }
    
    
    
    public static boolean canvisigne () {
        boolean correcte = true;
        if (A.length ==0) correcte = false;
        else {
            B = new double [A.length][A[0].length];
            for (int i=0; i<A.length; ++i) {
                for (int j=0; j<A[0].length; ++j) {
                    B[i][j] = -A[i][j];
                }
            }
        }
        return correcte;
    }
    
    
    public static boolean transposa () {
        boolean correcte = true;
        if (A.length ==0) correcte = false;
        else {
            B = new double [A[0].length][A.length];
            for (int i=0; i<B.length; ++i) {
                for (int j=0; j<B[0].length; ++j){
                    B[i][j] = A[j][i];
                }
            }
        }
      return correcte;  
    }
    
    public static void imprimatriu(double[][] M) {
        for (int i=0; i<M.length; ++i) {
            for (int j=0; j<M[0].length; ++j) {
                System.out.print(M[i][j] + " ");
            }
            System.out.println();
        }
    }
    
    
    public static boolean suma () {
        boolean correcte = true;
        if (A.length==0 || B.length==0 || A.length!=B.length ||A[0].length!=B[0].length) correcte = false;
        else {
            for (int i=0; i<A.length; ++i) {
                for (int j=0; j<A[0].length; ++j) {
                    B[i][j] += A[i][j];
                }
            }       
        }
    return correcte;
    }
    
    
    public static boolean resta () {
        boolean correcte = true;
        if (A.length==0 || B.length==0 || A.length!=B.length ||A[0].length!=B[0].length) correcte = false;
        else {
            for (int i=0; i<A.length; ++i) {
                for (int j=0; j<A[0].length; ++j) {
                    B[i][j] -= A[i][j];
                }
            }       
        }
    return correcte;
    }
    
    
    public static boolean producte () {
        boolean correcte = true;
        if (A.length==0 || B.length==0 || A[0].length!=B.length) correcte = false;
        else {
            double[][]C = new double [A.length][B[0].length];
            for (int i=0; i<A.length; ++i) {
                for (int j=0; j<B[0].length; ++j) {
                     int c
                    for (int k=0; k<A[0].length; ++k) {
                        cont+= A[i][k]*B[k][j];
                    }
                    C[i][j]=cont;
                }
            }
            B = C;
        }
        return correcte;
    }
    
    
    
    public static boolean intercanvi () {
        boolean correcte = true;
        if (A.length==0 || B.length==0) correcte = false;
        else {
            if (A.length==B.length && A[0].length==B[0].length) {
                double prov;
                for (int i=0; i<A.length; ++i) {
                    for (int j=0; j<A[0].length; ++j) {
                        prov = A[i][j];
                        A[i][j] = B[i][j];
                        B[i][j] = prov;
                    }
                }
            }
            else {
                double[][] C = new double [A.length][A[0].length];
                for (int i=0; i<A.length; ++ i) System.arraycopy(A[i], 0, C[i], 0, A[0].length);
                A = new double [B.length][B[0].length];
                for (int i=0; i<B.length; ++ i) System.arraycopy(B[i], 0, A[i], 0, B[0].length);
                B = new double [C.length][C[0].length];
                for (int i=0; i<C.length; ++ i) System.arraycopy(C[i], 0, B[i], 0, C[0].length);  
            }   
        }
        return correcte;
    }
    
    
    public static void llegeix() {
        
    
    }
    
    public static void guardar (double[][] A) throws IOException {
        Scanner entrada = new Scanner (System.in);
        System.out.println("Introdueix el nom del fitxer amb l'extensió.");
        String nomfitxer = entrada.nextLine();
        
        if (!nomfitxer.contains(".")) {
            System.err.println("Introdueix l'extensió de l'arxiu.");
            nomfitxer += "." + entrada.next();
        }
        
        FileWriter fw = new FileWriter(nomfitxer);
        PrintWriter pw = new PrintWriter(fw);
        for (int i=0; i<A.length; ++i) {
            for (int j=0; j<A[0].length; ++j) {
                pw.print(A[i][j]);
                if (j<(A[0].length)-1) pw.print(";");
            }
        pw.println();
        }
        fw.close();
    }
    
    
    
    
}

 

Revise this Paste

Your Name: Code Language: