Guest User

Untitled

a guest
Jul 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. public class Teste {
  5.     ArrayList<Var> lista = new ArrayList<Var>();
  6.     public String executa(String s){
  7.         StringTokenizer st = new StringTokenizer(s);
  8.         String b = "";
  9.         while(st.hasMoreTokens()){
  10.             String a = st.nextToken();
  11.             b = st.nextToken();
  12.             String c = st.nextToken();
  13.            
  14.             a.trim();
  15.             b.trim();
  16.             c.trim();
  17.            
  18.                        
  19.             executaInstrucao(a,b,c);
  20.         }
  21.         return b +" = "+ String.valueOf(getVariavel(b));
  22.     }
  23.    
  24.     public void executaInstrucao(String a, String b, String c){
  25.         int j = 0;
  26.         int i = 0;
  27.         if(a.equalsIgnoreCase("MOVE")){
  28.             i = 1;
  29.             boolean g = (c.charAt(0) == '0')||(c.charAt(0) == '1')||(c.charAt(0) == '2')||(c.charAt(0) == '3')||(c.charAt(0) == '4')||(c.charAt(0) == '5')||(c.charAt(0) == '6')||(c.charAt(0) == '7')||(c.charAt(0) == '8')||(c.charAt(0) == '9') ;
  30.             if(g){
  31.                 j = Integer.parseInt(c);
  32.                 i = 6;
  33.             }
  34.            
  35.         }else if(a.equalsIgnoreCase("ADD")){
  36.             i = 2;
  37.         }else if(a.equalsIgnoreCase("SUB")){
  38.             i = 3;
  39.         }else if(a.equalsIgnoreCase("MPY")){
  40.             i = 4;
  41.         }else if(a.equalsIgnoreCase("DIV")){
  42.             i = 5;
  43.         }
  44.         switch(i){
  45.             case 1: move(b,c); break;
  46.             case 2: add(b,c); break;
  47.             case 3: sub(b,c); break;
  48.             case 4: mpy(b,c); break;
  49.             case 5: div(b,c); break;
  50.             case 6: move(b,j); break;
  51.         }
  52.     }
  53.    
  54.     public int getVariavel(String s){
  55.         Var temp = null;
  56.         boolean a = false;
  57.         int j = 0;
  58.         for (int i= 0;i<lista.size();i++ ){
  59.             temp = (Var) lista.get(i);
  60.             String t = temp.getNome();
  61.             if(t.equalsIgnoreCase(s)){
  62.                 a = true;
  63.                 j = i;
  64.             }
  65.         }
  66.         if(a){
  67.             return lista.get(j).getConteudo();
  68.         }else{
  69.             return 0;
  70.         }
  71.     }
  72.    
  73.     public void setVariavel(String v, int c){
  74.         Var temp = null;
  75.         boolean a = false;
  76.         int j = 0;
  77.         for (int i= 0;i<lista.size();i++ ){
  78.             temp = (Var) lista.get(i);
  79.             String t = temp.getNome();
  80.             if (t.equalsIgnoreCase(v)){
  81.                 a = true;
  82.                 j = i;
  83.             }
  84.         }
  85.         if(a){
  86.             temp = (Var) lista.get(j);
  87.             temp.setConteudo(c);
  88.             lista.set(j, temp);
  89.         }else{
  90.             Var newVar = new Var();
  91.             newVar.setNome(v);
  92.             newVar.setConteudo(c);
  93.             lista.add(newVar);
  94.         }
  95.     }
  96.    
  97.     public void move(String o1, String o2){
  98.         setVariavel(o1, getVariavel(o2));
  99.     }
  100.    
  101.     public void move(String o1, int o2){
  102.         setVariavel(o1, o2);
  103.     }
  104.    
  105.     public void add(String o1, String o2){
  106.         int i = getVariavel(o1) + getVariavel(o2);
  107.         setVariavel(o1, i);
  108.     }
  109.    
  110.     public void sub(String o1, String o2){
  111.         int i = getVariavel(o1) - getVariavel(o2);
  112.         setVariavel(o1, i);
  113.     }
  114.    
  115.     public void mpy(String o1, String o2){
  116.         int i = getVariavel(o1) * getVariavel(o2);
  117.         setVariavel(o1, i);
  118.     }
  119.    
  120.     public void div(String o1, String o2){
  121.         int i = getVariavel(o1) / getVariavel(o2);
  122.         setVariavel(o1, i);
  123.     }
  124.    
  125. }
Add Comment
Please, Sign In to add comment