Advertisement
Vincentyfc

Untitled

Aug 6th, 2014
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. package foo;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * Procrastination: I'll add a javadoc description later.<br>
  7.  * hello @ foo
  8.  *
  9.  * @author Unihedron<vincentyification@gmail.com>
  10.  */
  11. public class hello {
  12.    
  13.     private static final java.util.regex.Pattern WORD = java.util.regex.Pattern.compile("([A-Za-z]\\d)");
  14.    
  15.     private static Object magic(final Object[][] grid, final String toParse) {
  16.         if ( !toParse.matches("=(\\w+)([-+*/]\\w+)*")) return toParse;
  17.         String eval = toParse;
  18.         while (true) {
  19.             final java.util.regex.Matcher matcher = hello.WORD.matcher(eval);
  20.             if (matcher.find()) eval =
  21.                 eval.replaceFirst(hello.WORD.pattern(),
  22.                         grid[Integer.parseInt(Character.toString(matcher.group(1).charAt(1))) - 1][matcher.group(1).charAt(0) - 'A'].toString());
  23.             else break;
  24.         }
  25.         final javax.script.ScriptEngine mgr = new javax.script.ScriptEngineManager().getEngineByName("JavaScript");
  26.         try {
  27.             final String evald = mgr.eval(eval.toString().substring(1)).toString();
  28.             return evald.substring(0, evald.length() - 2);
  29.         } catch (final javax.script.ScriptException e) {
  30.             e.printStackTrace();
  31.         }
  32.         return null;
  33.     }
  34.    
  35.     public static void main(final String args[]) {
  36.         final Scanner input = new Scanner(System.in);
  37.        
  38.         final Object[][] grid = new Object[3][3];
  39.        
  40.         for (int i = 0; i < 3; i++ )
  41.             for (int j = 0; j < 3; j++ ) {
  42.                 final String toParse = input.next();
  43.                 grid[i][j] = toParse.matches("-?\\d+") ? Integer.valueOf(toParse) : hello.magic(grid, toParse);
  44.             }
  45.         for (int i = 0; i < 3; i++ ) {
  46.             for (int j = 0; j < 3; j++ ) {
  47.                 System.out.print(grid[i][j]);
  48.                 System.out.print(' ');
  49.             }
  50.             System.out.println();
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement