Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ProgramaPrincipal;
- import java.util.Scanner;
- import TDAArbolBinario.*;
- import TDAArbolBinario.BoundaryViolationException;
- import TDAArbolBinario.InvalidPositionException;
- import TDAArbolBinario.Position;
- import TDACola.*;
- import TDALista.*;
- import TDAMapeo.*;
- import TDAPila.*;
- public class ProgramaPrincipal {
- protected static TablaHashAbierta<String, Integer> variables = new TablaHashAbierta<String, Integer>();
- protected static ArbolBinario<String> expresion = null;
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int eleccion = -1;
- while (eleccion != 0) {
- System.out.println("Elija una opcion: ");
- System.out.println("1) Definir una nueva variable y asignarle un valor.");
- System.out.println("2) Mostrar la variables definidas junto a su valor.");
- System.out.println("3) Introducir una expresion matematica.");
- System.out.println("4) Visualizar la expresion en notacion prefija.");
- System.out.println("5) Visualizar la expresion en notacion infija.");
- System.out.println("6) Visualizar la expresion en notacion postfijate.");
- System.out.println("7) Evaluar la expresion y mostrar el resultado.");
- System.out.println("8) Reemplazar termino.");
- System.out.println("9) Mostrar el arbol binario.");
- eleccion = sc.nextInt();
- if (eleccion == 2) {
- programa2(variables);
- }
- if (eleccion == 6) {
- try {
- programa6(expresion, expresion.root());
- System.out.println();
- } catch (EmptyTreeException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if (eleccion == 7) {
- try {
- System.out.println(evaluar(expresion,expresion.root(),variables));
- } catch (EmptyTreeException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- public boolean AgregarMapeo(String s,int n){
- return programa1(variables,s,n);
- }
- static boolean programa1(TablaHashAbierta<String, Integer> variables,String s,int n) {
- int valor=n;
- String variable=s;
- boolean correcto=true;
- try {
- Integer aux = variables.put(variable, valor);
- if (aux != null) {
- variables.put(variable, aux);
- correcto=false;
- System.out.println("Ya existe esa variable.");
- }
- } catch (InvalidKeyException e) {
- System.out.println("Variable nula.");
- e.printStackTrace();
- }
- return correcto;
- }
- public String mostrarMapeo(){
- return (programa2(variables));
- }
- static String programa2(Map<String, Integer> variables) {
- String s="";
- Iterable<Entry<String, Integer>> entradas = variables.entries();
- for (Entry<String, Integer> entrada : entradas) {
- s=s+entrada.getKey() +" = "+ entrada.getValue()+", ";
- }
- return s;
- }
- /** Recibe una expresion aritmetica por medio de un string y la transforma a un arbol binario
- * @param cad
- * @return expresion
- *
- * **/
- static ArbolBinario<String> programa3(String cad) {
- char[] arr = cad.toCharArray();
- NodeQueue<String> cola = new NodeQueue<String>();
- PilaEnlazada<Character> pila = new PilaEnlazada<Character>();
- ArbolBinario<String> expresion = new ArbolBinario<String>();
- try {
- char c;
- int i = 0;
- while (i < arr.length) {
- c = arr[i];
- if (c == '*' || c == '/' || c == '+' || c == '-' || c == '^' || c == '(') {
- pila.push(c);
- i++;
- } else {
- if (c == ')') {
- while (!pila.isEmpty() && pila.top() != '(') {
- cola.enqueue("" + pila.pop());
- }
- pila.pop();
- i++;
- } else {
- String var = "";
- while (!(c == '*' || c == '/' || c == '+' || c == '-' || c == '^' || c == '(' || c == ')')) {
- var = var + c;
- i++;
- c = arr[i];
- }
- cola.enqueue(var);
- }
- }
- }
- System.out.println(cola);
- PilaEnlazada<ArbolBinario<String>> elePend = new PilaEnlazada<ArbolBinario<String>>();
- while (!cola.isEmpty()) {
- String s = cola.dequeue();
- if (!(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/") || s.equals("^"))) {
- ArbolBinario<String> arb = new ArbolBinario<String>();
- arb.createRoot(s);
- elePend.push(arb);
- } else {
- ArbolBinario<String> arb = new ArbolBinario<String>();
- arb.createRoot(s);
- ArbolBinario<String> der = elePend.pop();
- ArbolBinario<String> izq = elePend.pop();
- // arb.Attach(arb.root(), izq, der);
- arb.Attach(arb.root(), izq, der);
- elePend.push(arb);
- }
- }
- expresion = elePend.pop();
- } catch (EmptyStackException | EmptyQueueException | InvalidOperationException | InvalidPositionException
- | EmptyTreeException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return expresion;
- }
- /**
- *
- * @param expresion
- * @param r
- * @return expresion en prefijo
- */
- public String programa4(ArbolBinario<String> expresion, Position<String> r) {
- String toReturn= r.element();
- try {
- if (expresion.hasLeft(r)) {
- toReturn=toReturn+programa4(expresion, expresion.left(r));
- }
- if (expresion.hasRight(r)) {
- toReturn=toReturn+programa4(expresion, expresion.right(r));
- }
- } catch (InvalidPositionException | BoundaryViolationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return toReturn;
- }
- public String programa5(ArbolBinario<String> expresion, Position<String> r) {
- String toReturn=r.element();
- try {
- if (expresion.hasLeft(r)) {
- if (!(expresion.left(r).element().equals("+") || expresion.left(r).element().equals("-")
- || expresion.left(r).element().equals("*") || expresion.left(r).element().equals("/")
- || expresion.left(r).element().equals("^"))) {
- //System.out.print("(");
- toReturn = "("+programa5(expresion, expresion.left(r))+toReturn;
- }
- else{
- toReturn=programa5(expresion, expresion.left(r))+toReturn;
- }
- }
- //System.out.print(r.element());
- if (expresion.hasRight(r)) {
- toReturn=toReturn+programa5(expresion, expresion.right(r));
- if (!(expresion.right(r).element().equals("+") || expresion.right(r).element().equals("-")
- || expresion.right(r).element().equals("*") || expresion.right(r).element().equals("/")
- || expresion.right(r).element().equals("^"))) {
- //System.out.print(")");
- toReturn = toReturn+")";
- }
- }
- } catch (InvalidPositionException | BoundaryViolationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return toReturn;
- }
- public static String programa6(ArbolBinario<String> expresion, Position<String> r) {
- String toReturn=r.element();
- try {
- if (expresion.hasRight(r)) {
- toReturn=programa6(expresion, expresion.right(r))+toReturn;
- }
- if (expresion.hasLeft(r)) {
- toReturn=programa6(expresion, expresion.left(r))+toReturn;
- }
- //System.out.print(r.element());
- } catch (InvalidPositionException | BoundaryViolationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return toReturn;
- }
- public static String programa7(ArbolBinario<String> expresion, TablaHashAbierta<String, Integer> variables) {
- String resultado = "";
- try {
- resultado = resultado + evaluar(expresion, expresion.root(), variables);
- } catch (EmptyTreeException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return resultado;
- }
- public String calcular(ArbolBinario<String>e, Position<String> r){
- return(programa7(e,variables));
- }
- public static int evaluar(ArbolBinario<String> expresion, Position<String> r,TablaHashAbierta<String,Integer> variables){
- int toReturn = 0;
- try{
- if(!(r.element().equals("+") || r.element().equals("-") ||r.element().equals("*") ||r.element().equals("/") ||r.element().equals("^"))){
- toReturn = variables.get(r.element());
- }
- else{
- if(r.element().equals("+")){
- toReturn = evaluar(expresion, expresion.left(r), variables)+evaluar(expresion, expresion.right(r), variables);
- }
- if(r.element().equals("-")){
- toReturn = evaluar(expresion, expresion.left(r), variables)-evaluar(expresion, expresion.right(r), variables);
- }
- if(r.element().equals("/")){
- toReturn = evaluar(expresion, expresion.left(r), variables)/evaluar(expresion, expresion.right(r), variables);
- }
- if(r.element().equals("*")){
- toReturn = evaluar(expresion, expresion.left(r), variables)*evaluar(expresion, expresion.right(r), variables);
- }
- if(r.element().equals("^")){
- toReturn = evaluar(expresion, expresion.left(r), variables)^evaluar(expresion, expresion.right(r), variables);
- }
- }
- } catch (InvalidKeyException | InvalidPositionException | BoundaryViolationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return toReturn;
- }
- public boolean buscar(String s){
- boolean aux=false;
- Iterable<Entry<String, Integer>> entradas = variables.entries();
- for(Entry<String,Integer> nombres:entradas){
- if(nombres.getKey()==s)
- aux=true;
- else
- aux=false;
- }
- return aux;
- }
- public void llamarPrograma8(ArbolBinario<String> expresion,Queue<String> l){
- programa8(expresion,variables,l);
- System.out.println("Se reemplazo correctamente");
- }
- public void programa8(ArbolBinario<String> expresion, TablaHashAbierta<String,Integer> variables, Queue<String> l){
- try {
- if(expresion.altura()>1){
- reemplazar(expresion, expresion.root(),variables,l);
- }
- } catch (EmptyTreeException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- protected void reemplazar(ArbolBinario<String> expresion,Position<String> r, TablaHashAbierta<String,Integer> variables,Queue<String>l ){
- try {
- if(expresion.isExternal(expresion.left(r))){
- String var=l.dequeue();
- int val = evaluar(expresion, r, variables);
- expresion.remove(expresion.left(r));
- expresion.remove(expresion.right(r));
- expresion.replace(r, var);
- variables.put(var, val);
- }
- else{
- reemplazar(expresion, expresion.left(r), variables,l);
- reemplazar(expresion, expresion.right(r), variables,l);
- }
- } catch (InvalidPositionException |EmptyQueueException| BoundaryViolationException | InvalidOperationException | InvalidKeyException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public int altura(ArbolBinario<String> t){
- int aux=-1;
- try {
- aux= (t.altura());
- } catch (EmptyTreeException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return aux;
- }
- public int cantNodos(ArbolBinario<String> e ){
- return e.size();
- }
- public int cantHojas(ArbolBinario<String> e){
- return (e.cantHojas());
- }
- public int cantInternos(ArbolBinario<String> e){
- return e.cantInternos();
- }
- public boolean esPropio(ArbolBinario<String> e){
- return e.esPropio();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement