Advertisement
Ladies_Man

StackMachine 2.0

Nov 10th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. public interface StackMachine
  2. {
  3.         // Положить константу в стек
  4.         void pushConst(int value);
  5.  
  6.         // Положить в стек значение переменной x
  7.         void pushVar();
  8.  
  9.         // Изменить знак числа на вершине стека
  10.         void neg();
  11.  
  12.         // Бинарные арифметические операции.
  13.         // Каждая операция снимает два операнда со стека
  14.         // и кладёт на стек результат
  15.         void add();  // сложение
  16.         void sub();  // вычитание
  17.         void mul();  // умножение
  18.         void div();  // деление
  19.  
  20.         // Снимает со стека и возвращает число
  21.         int result();
  22. }
  23.  
  24. //========================================================================================
  25.  
  26. public class Evaluator implements StackMachine{
  27.         private int[] data;
  28.     private int top, a, b, c, var;
  29.     public Evaluator (int x) {
  30.         data = new int[100];
  31.         top = 0;
  32.         var = x;
  33.     }
  34.    
  35.     private int pop () {
  36.         return data[--top];
  37.     }
  38.    
  39.     public void pushConst (int value) {
  40.         data[top++] = value;
  41.     }
  42.    
  43.     public void pushVar () {
  44.         data[top++] = var;
  45.     }
  46.    
  47.     public void neg () {
  48.         this.pushConst(pop() * -1);
  49.     }
  50.    
  51.     public void add () {
  52.         this.pushConst(pop() + pop());
  53.     }
  54.    
  55.     public void sub () {
  56.         b = pop();
  57.         a = pop();
  58.         this.pushConst(a-b);
  59.     }  
  60.    
  61.     public void mul () {
  62.         this.pushConst(pop() * pop());
  63.     }
  64.    
  65.     public void div () {
  66.         b = pop();
  67.         a = pop();
  68.         this.pushConst(a/b);
  69.     }
  70.    
  71.     public int result () {
  72.         return pop();
  73.     }
  74. }
  75.  
  76. //========================================================================================
  77.  
  78. public class Test
  79. {
  80.         public static void main(String[] args)
  81.         {
  82.                 try {
  83.                         Evaluator eval = new Evaluator(10);
  84.  
  85.                         // x * (x+1)
  86.                         eval.pushVar();
  87.                         eval.pushVar();
  88.                         eval.pushConst(1);
  89.                         eval.add();
  90.                         eval.mul();
  91.  
  92.                         System.out.println(eval.result());
  93.                 } catch (java.lang.Exception e) {
  94.                         System.out.println("exception:␣" + e);
  95.                 }
  96.         }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement