How to code a priority matrix? [closed]

2

I have to make a calculator, but I need to know how to encode the priority matrix, so that for example, first multiply and then add.

    
asked by LINA YOHANNA 23.08.2017 в 21:07
source

3 answers

3
public static int matriz(String s){
    int r = 0;
    switch(s){
        case "+":
        case "-":
            r = 1;
            break;
        case "*":
        case "/":
            r = 2;
            break;
    }
    return r;

}

Also in this way you could do it since you only want to know if it is important or not

    
answered by 01.09.2017 в 04:35
2

You have two ways of making a priority matrix, one is using a Hashmap within another Hashmap

Map<String, Map<String, Object>> doubleKeyMap = new HashMap<String, Map<String,Object>>();

    // Agregar valores

    doubleKeyMap.put("key1A", new HashMap<String, Object>(){{put("key2A", "Test");}});
    doubleKeyMap.put("key1B", new HashMap<String, Object>(){{put("key2B", 123);}});
    doubleKeyMap.put("key1C", new HashMap<String, Object>(){{put("key2C", false);}});

    // Leer valores

    doubleKeyMap.get("key1A").get("key2A"); // Test (String)
    doubleKeyMap.get("key1B").get("key2B"); // 123 (int)
    doubleKeyMap.get("key1C").get("key1C"); // false (boolean)
    doubleKeyMap.get("key1A").get("key2B"); // null (No existe la combinación de keys)

or you can create a Hashmap where you can enter the sign you enter first and the content a method where the other signs are related

public class MatrizPrioridad {

    public static int matriz() {
        /*
         * ArrayList<Contenido> matriz = new ArrayList<Contenido>(); Contenido cont;
         */

        // HashMap<Entry<Character, Character>, Integer> matriz = new
        // HashMap<Entry<Character,Character>, Integer>();

        /*
         * Pair<Character, Character> suma_resta = new Pair<Character, Character>('+',
         * '-'); Pair
         * 
         * matriz.put(suma, 1); matriz.put(suma_resta, 0);
         */
        // matriz.add(cont);

        // Map<new Map<Character, Character>, Integer> matriz = new Map<new
        // Map<Character, Character>, Integer>;
        HashMap<HashMap<Character, Character>, Integer> matriz = new HashMap<HashMap<Character, Character>, Integer>();
        ;

        matriz.put(new HashMap<Character, Character>() {
            {
                put('+', '+');
            }
        }, 1);
        // prueba

        return matriz.get('+');
    }

    public static Integer matrizSuma(Character obten) {
        HashMap<Character, Integer> suma = new HashMap<Character, Integer>();

        suma.put('+', 1);
        suma.put('-', 1);
        suma.put('*', 0);
        suma.put('/', 0);
        suma.put('^', 0);

        Integer resultado = null;

        resultado = suma.get(obten);

        return resultado;
    }

    public static Integer matrizResta(Character obten) {
        HashMap<Character, Integer> resta = new HashMap<Character, Integer>();

        resta.put('+', 1);
        resta.put('-', 1);
        resta.put('*', 0);
        resta.put('/', 0);
        resta.put('^', 0);

        Integer resultado = null;

        resultado = resta.get(obten);

        return resultado;
    }

    public static Integer matrizMultiplica(Character obten) {
        HashMap<Character, Integer> multiplica = new HashMap<Character, Integer>();

        multiplica.put('+', 1);
        multiplica.put('-', 1);
        multiplica.put('*', 1);
        multiplica.put('/', 1);
        multiplica.put('^', 0);

        Integer resultado = null;

        resultado = multiplica.get(obten);

        return resultado;
    }

    public static Integer matrizDivide(Character obten) {
        HashMap<Character, Integer> deivide = new HashMap<Character, Integer>();

        deivide.put('+', 1);
        deivide.put('-', 1);
        deivide.put('*', 1);
        deivide.put('/', 1);
        deivide.put('^', 0);

        Integer resultado = null;

        resultado = deivide.get(obten);

        return resultado;
    }

    public static Integer matrizPotencia(Character obten) {
        HashMap<Character, Integer> potencia = new HashMap<Character, Integer>();

        potencia.put('+', 1);
        potencia.put('-', 1);
        potencia.put('*', 1);
        potencia.put('/', 1);
        potencia.put('^', 1);

        Integer resultado = potencia.get(obten);

        return resultado;
    }

    /*
     * public static void main(String[] args) { System.out.println(matrizSuma('/'));
     * System.out.println(matrizResta('-'));
     * System.out.println(matrizMultiplica('+'));
     * System.out.println(matrizDivide('+'));
     * 
     * }
     */

    public Integer matrizPrincipal(Character signo, Character obten) {
        // HashMap<Character, Integer> matriz = new HashMap<Character, Integer>();
        Integer respuesta = null;
        if (signo.equals('+')) {
            respuesta = matrizSuma(obten);
            // EncontrarComando.addOp(new Suma());
            // System.out.println(matrizSuma(obten));
        } else {
            if (signo.equals('-')) {
                respuesta = matrizResta(obten);
                // EncontrarComando.addOp(new Resta());
            } else {
                if (signo.equals('*')) {
                    respuesta = matrizMultiplica(obten);
                    // EncontrarComando.addOp(new Multiplica());
                } else {
                    if (signo.equals('/')) {
                        respuesta = matrizDivide(obten);
                        // EncontrarComando.addOp(new Divide());
                    } else {
                        if (signo.equals('^')) {
                            respuesta = matrizPotencia(obten);
                        } else {
                            if (signo.equals('p')) {
                                respuesta = 1;
                            }
                        }
                    }
                }
            }
        }
        /*
         * if(respuesta == 1) { ActivarComando.activar(obten); }else {
         * ActivarComando.activar(signo); }
         */

        return respuesta;
        // return matriz.get(obten);
    }
}
    
answered by 01.09.2017 в 04:33
1

Depending on the problem you are proposing, try to make a matrix for each operator in the following way:

public static Hashtable<String, Hashtable<String, Integer>> matriz;

public static Hashtable<String, Integer> suma = new Hashtable<String, Integer>();
public static Hashtable<String, Integer> resta = new Hashtable<String, Integer>();
public static Hashtable<String, Integer> multiplacion = new Hashtable<String, Integer>();
public static Hashtable<String, Integer> division = new Hashtable<String, Integer>();

public static void llenarMatrizDePrioridad() {
    if (matriz == null)
        matriz = new Hashtable<String, Hashtable<String, Integer>>();

    suma.put("+", 1);
    suma.put("-", 1);
    suma.put("*", 0);
    suma.put("/", 0);

    resta.put("+", 1);
    resta.put("-", 1);
    resta.put("*", 0);
    resta.put("/", 0);

    multiplacion.put("+", 1);       
    multiplacion.put("-", 1);
    multiplacion.put("*", 1);
    multiplacion.put("/", 1);

    division.put("+", 1);
    division.put("-", 1);
    division.put("*", 1);
    division.put("/", 1);

    matriz.put("+", suma);
    matriz.put("-", resta);
    matriz.put("*", multiplacion);
    matriz.put("/", division);
}

I hope this helps you.

    
answered by 01.09.2017 в 04:31