Advertisement
SuperJedi224

Math++

Sep 20th, 2015
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.regex.*;
  4. import javax.swing.JOptionPane;
  5. import static java.lang.Math.*;
  6.  
  7. //Math++ 1.04 Reference Implementation
  8.  
  9. public class MathPlusPlus{
  10.     static Map<Character,Double>vars=new HashMap<>();
  11.     static Scanner in=new Scanner(System.in);
  12.     static final double PHI=(1+sqrt(5))/2;
  13.     static PrintStream log;
  14.     static Map<Integer,Double>tape=new HashMap<>();
  15.     static double expr(String e,int line){
  16.         try{
  17.         e=e.replaceAll("\\s","");
  18.         log.println(e);
  19.         if(e.equalsIgnoreCase("$pi"))return PI;
  20.         if(e.equalsIgnoreCase("$phi"))return PHI;
  21.         if(e.equalsIgnoreCase("$e"))return E;
  22.         if(e.equalsIgnoreCase("$rand"))return Math.random();
  23.         if(e.matches("^\\{(.+)\\}$")){
  24.             e=e.replaceAll("^\\{(.+)\\}$","$1");
  25.             return tape.getOrDefault((int)expr(e,line),0.0);
  26.         }
  27.         while(e.indexOf("(")!=-1){
  28.             Matcher k=Pattern.compile("\\(([^()]+?)\\)").matcher(e);
  29.             k.find();
  30.             String g=k.group(1);
  31.             log.println(g);
  32.             double d=expr(g,line);
  33.             e=e.replaceFirst("\\([^()]+?\\)",d<0?"(-"+d+")":""+d);
  34.         }
  35.         try{return Double.parseDouble(e);}catch(Exception x){}
  36.         if(e.matches("[a-z]"))return vars.getOrDefault(e.charAt(0),0.0);
  37.         if(e.equals("?"))return in.nextDouble();
  38.         if(e.indexOf("|")!=-1){
  39.             for(String a:e.split("\\|")){
  40.                 double i=expr(a,line);
  41.                 if(i!=0)return i;
  42.             }
  43.             return 0;
  44.         }
  45.         if(e.indexOf("&")!=-1){
  46.             for(String a:e.split("&")){
  47.                
  48.                 if(expr(a,line)==0)return 0;
  49.             }
  50.             return 1;
  51.         }
  52.         if(e.indexOf("+")!=-1){
  53.             double i=0;
  54.             for(String a:e.split("\\+")){
  55.                 i+=expr(a,line);
  56.             }
  57.             return i;
  58.         }
  59.         if(e.indexOf("-")>0){
  60.             double i=0;
  61.             String[]a=e.split("-");
  62.             i=expr(a[0],line);
  63.             for(int j=1;j<a.length;j++){
  64.                 i-=expr(a[j],line);
  65.             }
  66.             return i;
  67.         }
  68.         if(e.indexOf("*")>0){
  69.             double i=1;
  70.             for(String a:e.split("\\*")){
  71.                 i*=expr(a,line);
  72.             }
  73.             return i;
  74.         }
  75.         if(e.indexOf("/")>0){
  76.             String[]a=e.split("/");
  77.             double i=expr(a[0],line);
  78.             for(int j=1;j<a.length;j++){
  79.                 i/=expr(a[j],line);
  80.             }
  81.             return i;
  82.         }
  83.         if(e.indexOf("%")>0){
  84.             String[]a=e.split("%");
  85.             double i=expr(a[0],line);
  86.             for(int j=1;j<a.length;j++){
  87.                 double b=expr(a[j],line);
  88.                 i-=b*Math.floor(i/b);
  89.             }
  90.             return i;
  91.         }
  92.         if(e.startsWith("-"))return -expr(e.substring(1),line);
  93.         if(e.startsWith("ln"))return log(expr(e.substring(2),line));
  94.         if(e.startsWith("log"))return log10(expr(e.substring(3),line));
  95.         if(e.startsWith("sin"))return sin(expr(e.substring(3),line));
  96.         if(e.startsWith("cos"))return cos(expr(e.substring(3),line));
  97.         if(e.startsWith("tan"))return tan(expr(e.substring(3),line));
  98.         if(e.startsWith("sec"))return 1/cos(expr(e.substring(3),line));
  99.         if(e.startsWith("csc"))return 1/sin(expr(e.substring(3),line));
  100.         if(e.startsWith("cot"))return 1/tan(expr(e.substring(3),line));
  101.         if(e.startsWith("abs"))return abs(expr(e.substring(3),line));
  102.         if(e.startsWith("sqrt"))return sqrt(expr(e.substring(4),line));
  103.         if(e.startsWith("cbrt"))return cbrt(expr(e.substring(4),line));
  104.         if(e.startsWith("_"))return floor(expr(e.substring(1),line));
  105.         if(e.startsWith("!"))return expr(e.substring(1),line)==0?1:0;
  106.         throwError("Could not evaluate expression "+e+" on line "+line);
  107.         }catch(Exception ex){
  108.             throwError("Unexpected error on line "+line);
  109.         }
  110.         return 0;
  111.     }
  112.     static void throwError(String message){
  113.         JOptionPane.showMessageDialog(null,message,"",JOptionPane.ERROR_MESSAGE);
  114.         System.exit(0);
  115.     }
  116.     public static void main(String[]a){
  117.         if(a.length==0)a=new String[]{"file"};
  118.         List<String>line=new ArrayList<>();
  119.         try{Scanner read=new Scanner(new File(a[0]+".mpp"));
  120.         while(read.hasNext()){
  121.             line.add(read.nextLine());
  122.         }
  123.         read.close();}catch(Exception ex){
  124.             throwError("Could not read file.");
  125.         }
  126.         try{log=new PrintStream("log.txt");
  127.         for(int i=0;i<line.size();i++){
  128.             String l=line.get(i);
  129.             String flag="out";
  130.             if(l.indexOf(">")!=-1){
  131.                 String[]k=l.split(">");
  132.                 flag=k[1];
  133.                 l=k[0];
  134.             }
  135.             double q=expr(l,i+1);
  136.             if(flag.equals("out")){
  137.                 System.out.println(q);
  138.             }
  139.             if(flag.equals("$")){
  140.                 if((int)q==0)System.exit(0);
  141.                 if((int)q>line.size()||(int)q<0)throwError("Attempt to go to nonexistent line "+(int)q+" from line "+(i+1));
  142.                 i=(int)(q-2);
  143.             }
  144.             if(flag.matches("[a-z]"))vars.put(flag.charAt(0),q);
  145.             if(flag.matches("^\\{(.+)\\}$")){
  146.                 flag=flag.replaceAll("^\\{(.+)\\}$","$1");
  147.                 tape.put((int)expr(flag,i+1),q);
  148.             }
  149.         }}catch(Exception e){
  150.             throwError("Unidentified error.");
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement