Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Teste {
- ArrayList<Var> lista = new ArrayList<Var>();
- public String executa(String s){
- StringTokenizer st = new StringTokenizer(s);
- String b = "";
- while(st.hasMoreTokens()){
- String a = st.nextToken();
- b = st.nextToken();
- String c = st.nextToken();
- a.trim();
- b.trim();
- c.trim();
- executaInstrucao(a,b,c);
- }
- return b +" = "+ String.valueOf(getVariavel(b));
- }
- public void executaInstrucao(String a, String b, String c){
- int j = 0;
- int i = 0;
- if(a.equalsIgnoreCase("MOVE")){
- i = 1;
- 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') ;
- if(g){
- j = Integer.parseInt(c);
- i = 6;
- }
- }else if(a.equalsIgnoreCase("ADD")){
- i = 2;
- }else if(a.equalsIgnoreCase("SUB")){
- i = 3;
- }else if(a.equalsIgnoreCase("MPY")){
- i = 4;
- }else if(a.equalsIgnoreCase("DIV")){
- i = 5;
- }
- switch(i){
- case 1: move(b,c); break;
- case 2: add(b,c); break;
- case 3: sub(b,c); break;
- case 4: mpy(b,c); break;
- case 5: div(b,c); break;
- case 6: move(b,j); break;
- }
- }
- public int getVariavel(String s){
- Var temp = null;
- boolean a = false;
- int j = 0;
- for (int i= 0;i<lista.size();i++ ){
- temp = (Var) lista.get(i);
- String t = temp.getNome();
- if(t.equalsIgnoreCase(s)){
- a = true;
- j = i;
- }
- }
- if(a){
- return lista.get(j).getConteudo();
- }else{
- return 0;
- }
- }
- public void setVariavel(String v, int c){
- Var temp = null;
- boolean a = false;
- int j = 0;
- for (int i= 0;i<lista.size();i++ ){
- temp = (Var) lista.get(i);
- String t = temp.getNome();
- if (t.equalsIgnoreCase(v)){
- a = true;
- j = i;
- }
- }
- if(a){
- temp = (Var) lista.get(j);
- temp.setConteudo(c);
- lista.set(j, temp);
- }else{
- Var newVar = new Var();
- newVar.setNome(v);
- newVar.setConteudo(c);
- lista.add(newVar);
- }
- }
- public void move(String o1, String o2){
- setVariavel(o1, getVariavel(o2));
- }
- public void move(String o1, int o2){
- setVariavel(o1, o2);
- }
- public void add(String o1, String o2){
- int i = getVariavel(o1) + getVariavel(o2);
- setVariavel(o1, i);
- }
- public void sub(String o1, String o2){
- int i = getVariavel(o1) - getVariavel(o2);
- setVariavel(o1, i);
- }
- public void mpy(String o1, String o2){
- int i = getVariavel(o1) * getVariavel(o2);
- setVariavel(o1, i);
- }
- public void div(String o1, String o2){
- int i = getVariavel(o1) / getVariavel(o2);
- setVariavel(o1, i);
- }
- }
Add Comment
Please, Sign In to add comment