Facebook
From das, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 249
  1. package task2;
  2.  
  3. public abstract class Type {
  4.     public abstract String toString(Node[] a);
  5.     public abstract Boolean calculate (Node[] a, Boolean val);
  6.  
  7. }
  8.  
  9.  
  10. package task2;
  11.  
  12. public class Value extends Type {
  13.  
  14.     @Override
  15.     public String toString(Node[] a) {
  16.         return "x";
  17.     }
  18.  
  19.     @Override
  20.     public Boolean calculate(Node[] a, Boolean val) {
  21.         return val;
  22.     }
  23. }
  24.  
  25.  
  26. package task2;
  27.  
  28. public class OR extends Type {
  29.     private static final Object OR = new OR();
  30.  
  31.     public static Node execute(Node v, Node v1) {
  32.         return new Node(v, v1, (Type) OR);
  33.     }
  34.  
  35.     @Override
  36.     public String toString(Node[] a) {
  37.         return "("+a[0]+"|"+a[1]+")";
  38.     }
  39.  
  40.     @Override
  41.     public Boolean calculate(Node[] a, Boolean val) {
  42.         return a[0].calculate(val)|a[1].calculate(val);
  43.     }
  44. }
  45.  
  46.  
  47.  
  48.  
  49. package task2;
  50.  
  51. public class NOT extends Type{
  52.  
  53.     private static final Object NOT = new NOT();
  54.  
  55.     public static Node execute(Node v) {
  56.         return new Node(v, (Type) NOT);
  57.     }
  58.  
  59.     @Override
  60.     public String toString(Node[] a) {
  61.         return "(~"+a[0]+")";
  62.     }
  63.  
  64.     @Override
  65.     public Boolean calculate(Node[] a, Boolean val) {
  66.         return !a[0].calculate(val);
  67.     }
  68. }
  69.  
  70.  
  71. package task2;
  72.  
  73. public class AND extends Type{
  74.  
  75.  
  76.     private static final Object AND = new AND();
  77.  
  78.  
  79.     public static Node execute(Node v, Node v1) {
  80.         return new Node(v, v1, (Type) AND);
  81.     }
  82.  
  83.     @Override
  84.     public String toString(Node[] a) {
  85.         return "("+a[0]+"&"+a[1]+")";
  86.     }
  87.  
  88.     @Override
  89.     public Boolean calculate(Node[] a, Boolean val) {
  90.         return a[0].calculate(val)&a[1].calculate(val);
  91.     }
  92. }
  93.  
  94.  
  95.  
  96. package task2;
  97.  
  98. public interface Expr<T> {
  99.     void flush ();
  100.     T evaluate (T a);
  101. }
  102.  
  103.  
  104. package task2;
  105.  
  106. import java.util.Arrays;
  107.  
  108. public class Node {
  109.     private static final Object Value = new Value();
  110.     Node[] x;
  111.     Type t;
  112.     Node () {
  113.         x=Arrays.copyOf(new Node[0], 0);
  114.         this.t=(Type) Value;
  115.     }
  116.     Node (Node a, Type t) {
  117.         x=Arrays.copyOf(new Node[1], 1);
  118.         x[0]=a;
  119.         this.t=t;
  120.     }
  121.     Node (Node a, Node b, Type t) {
  122.         x=Arrays.copyOf(new Node[2], 2);
  123.         x[0]=a;
  124.         x[1]=b;
  125.         this.t=t;
  126.     }
  127.  
  128.     @Override
  129.     public String toString() {
  130.         return t.toString(x);
  131.     }
  132.  
  133.     public Boolean calculate(Boolean a) {
  134.         return t.calculate(x, a);
  135.     }
  136. }
  137.  
  138.  
  139.  
  140. package task2;
  141.  
  142. public class BoolExpr implements Expr<Boolean> {
  143.  
  144.     protected Node v;
  145.  
  146.     public BoolExpr() {
  147.         this.v=new Node();
  148.     }
  149.  
  150.     public BoolExpr(Node v) {
  151.         this.v = v;
  152.     }
  153.  
  154.     public BoolExpr and (BoolExpr x) {
  155.         return new BoolExpr(AND.execute(v, x.v));
  156.     }
  157.  
  158.     public BoolExpr or (BoolExpr x) {
  159.         return new BoolExpr(OR.execute(v, x.v));
  160.     }
  161.  
  162.     public BoolExpr not () {
  163.         return new BoolExpr(NOT.execute(v));
  164.     }
  165.  
  166.     @Override
  167.     public void flush() {
  168.         System.out.println(v);
  169.     }
  170.  
  171.     @Override
  172.     public Boolean evaluate(Boolean a) {
  173.         return v.calculate(a);
  174.     }
  175. }
  176.  
  177.  
  178.  
  179. import task2.BoolExpr;
  180.  
  181. public class Main {
  182.     static public void main (String[] argv) {
  183.         BoolExpr a,b,c;
  184.  
  185.         a = new BoolExpr();
  186.         b = new BoolExpr();
  187.         c = new BoolExpr();
  188.  
  189.  
  190.  
  191.         c = a.and(b); // a \land b
  192.         c = c.or(b.not()); // c \lor \not b
  193.  
  194.         c.flush();
  195.         Boolean T = true;
  196.         System.out.println(c.evaluate(T));
  197.  
  198.     }
  199. }
  200.