Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.69 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Calc {
  4.    
  5.     public static final int ADD = 0, SUBTRACT = 1, MULTIPLY = 2, DIVIDE = 3,
  6.                             POWER = 4, LOG = 5, SIN = 6, COS = 7, TAN = 8,
  7.                             SQRT = 9, PI = 10, EULER = 11, NUMBER = 12;
  8.     public static final String[] token_map = {"+", "-", "*", "/", "^", "ln", "sin",
  9.                                               "cos", "tan", "sqrt", "pi", "e"};
  10.    
  11.     public static class StringToken {
  12.        
  13.         public int instruction;
  14.         public String subtoken0;
  15.         public String subtoken1;
  16.        
  17.         public StringToken(int inst, String subt0, String subt1) {
  18.             this.instruction = inst;
  19.             this.subtoken0 = subt0;
  20.             this.subtoken1 = subt1;
  21.         }
  22.        
  23.     }
  24.    
  25.     public static class Token {
  26.        
  27.         private List<Token> subtokens = new LinkedList();
  28.         private int instruction;
  29.         private double value;
  30.        
  31.         public Token(int inst) {
  32.             this.value = 0;
  33.             this.instruction = inst;
  34.         }
  35.        
  36.         public void set_instruction(int inst) {
  37.             this.instruction = inst;
  38.         }
  39.        
  40.         public boolean insert(double value) {
  41.             if (this.instruction == 12) {
  42.                 this.value = value;
  43.                 return true;
  44.             } else {
  45.                 return false;
  46.             }
  47.         }
  48.        
  49.         public boolean insert(Token subtoken) {
  50.             if (this.instruction < 12) {
  51.                 this.subtokens.add(subtoken);
  52.                 return true;
  53.             } else {
  54.                 return false;
  55.             }
  56.         }
  57.        
  58.         public double evaluate() {
  59.             switch (this.instruction) {
  60.             case Calc.NUMBER:
  61.                 return this.value;
  62.             case Calc.EULER:
  63.                 return Math.E;
  64.             case Calc.PI:
  65.                 return Math.PI;
  66.             case Calc.SQRT:
  67.                 return Math.sqrt(this.subtokens.get(0).evaluate());
  68.             case Calc.POWER:
  69.                 return Math.pow(this.subtokens.get(0).evaluate(), this.subtokens.get(1).evaluate());
  70.             case Calc.MULTIPLY:
  71.                 return this.subtokens.get(0).evaluate() * this.subtokens.get(1).evaluate();
  72.             case Calc.DIVIDE:
  73.                 return this.subtokens.get(0).evaluate() / this.subtokens.get(1).evaluate();
  74.             case Calc.ADD:
  75.                 return this.subtokens.get(0).evaluate() + this.subtokens.get(1).evaluate();
  76.             case Calc.SUBTRACT:
  77.                 return this.subtokens.get(0).evaluate() - this.subtokens.get(1).evaluate();
  78.             case Calc.SIN:
  79.                 return Math.sin(this.subtokens.get(0).evaluate());
  80.             case Calc.COS:
  81.                 return Math.cos(this.subtokens.get(0).evaluate());
  82.             case Calc.TAN:
  83.                 return Math.tan(this.subtokens.get(0).evaluate());
  84.             case Calc.LOG:
  85.                 return Math.log(this.subtokens.get(0).evaluate()) / Math.log(Math.E);
  86.             default:
  87.                 return Double.NaN;
  88.             }
  89.         }
  90.     }
  91.    
  92.     public static double evaluate(Token master_token) {
  93.         return master_token.evaluate();
  94.     }
  95.    
  96.     public static Token combine(Token master, StringToken subtoken) {
  97.         if (subtoken.instruction == 12) {
  98.             master.set_instruction(12);
  99.             master.insert(Integer.parseInt(subtoken.subtoken0));
  100.         } else if (subtoken.instruction == -1) {
  101.            
  102.         } else {
  103.             master.set_instruction(subtoken.instruction);
  104.             master.insert(parse_string(subtoken.subtoken0));
  105.             master.insert(parse_string(subtoken.subtoken1));
  106.         }
  107.         return master;
  108.     }
  109.    
  110.     public static Token parse_string(String expression) {
  111.         Token master = new Token(-1);
  112.         master = combine(master, lowest_token(expression));
  113.         return master;
  114.     }
  115.    
  116.     public static String join_list(List<String> list) {
  117.        
  118.         String output = "";
  119.         for (int i = 0;i < list.size();i++) {
  120.             output += list.get(i);
  121.         }
  122.         return output;
  123.     }
  124.    
  125.     public static int index_of(List<String> list, String comp) {
  126.         for (int i = 0;i < list.size();i++) {
  127.             if (list.get(i).equals(comp))
  128.                 return i;
  129.         }
  130.         return -1;
  131.     }
  132.    
  133.     public static StringToken lowest_token(String expression) {
  134.        
  135.         if (expression.length() == 0)
  136.             return new StringToken(-1, "", "");
  137.         else if (expression.startsWith("(") && expression.endsWith(")"))
  138.             expression = (String) expression.subSequence(1, expression.length() - 1);
  139.         int lbrackets, rbrackets;
  140.         String[] tokenarray = expression.split(
  141.                 "(?<=(ln)|(sin)|(cos)|(tan)|(fac)|(pi)|(e)|\\(|\\)|\\^|/|\\*|\\+|\\-|\\.|\\d)"
  142.                 );
  143.         List<String> tokenlist = new LinkedList<String>();
  144.         for (int i = 0;i < tokenarray.length;i++) {
  145.             tokenlist.add(tokenarray[i]);
  146.         }
  147.         StringToken master_token = new StringToken(-1, "", "");
  148.        
  149.         for (int i = 0;i < 13;i++) {
  150.             if (i == 12) {
  151.                 master_token.instruction = 12;
  152.                 master_token.subtoken0 = join_list(tokenlist);
  153.                 return master_token;
  154.             } else if (!tokenlist.contains(token_map[i])) {
  155.                 continue;
  156.             }
  157.             for (int c = 0;c < tokenlist.size();c++) {
  158.                 if (tokenlist.indexOf(token_map[i]) == c) {
  159.                     lbrackets = 0;
  160.                     rbrackets = 0;
  161.                     for (int x = 0;x < c;x++) {
  162.                         if (tokenlist.get(x).equals("("))
  163.                             lbrackets++;
  164.                         else if (tokenlist.get(x).equals(")"))
  165.                             rbrackets++;
  166.                     }
  167.                     if (lbrackets == rbrackets) {
  168.                         master_token.instruction = i;
  169.                         if (i < 5) {
  170.                             master_token.subtoken0 = join_list(tokenlist.subList(0, c));
  171.                             master_token.subtoken1 = join_list(tokenlist.subList(c+1, tokenlist.size()));
  172.                         } else {
  173.                             lbrackets = 0;
  174.                             rbrackets = 0;
  175.                             for (int x = c + 1;x < tokenlist.size();x++) {
  176.                                 if (tokenlist.get(x).equals("("))
  177.                                     lbrackets++;
  178.                                 else if (tokenlist.get(x).equals(")"))
  179.                                     rbrackets++;
  180.                                 if (lbrackets == rbrackets) {
  181.                                     master_token.subtoken0 = join_list(tokenlist.subList(c + 2, x));
  182.                                     break;
  183.                                 }
  184.                             }
  185.                         }
  186.                         return master_token;
  187.                     }
  188.                 }
  189.             }
  190.         }
  191.         return new StringToken(-1, "", "");
  192.     }
  193.    
  194.     public static double evaluate(String expression) {
  195.         return evaluate(parse_string(expression));
  196.     }
  197.    
  198.     public static void main(String[] args) {
  199.         // Input: ln(sin(3^5*2)/(10+3))
  200.         String expression = "ln(sin(3^5*2)/(10+3))";
  201.         System.out.println(evaluate(expression));
  202.         // Output: -2.7737145543097883 (correct)
  203.     }
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement