Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. /**
  6.  * @author Jan Buchar
  7.  */
  8. public class CodEx
  9. {
  10.     private static String line;
  11.  
  12.     private static int cursor;
  13.  
  14.     public static void main (String[] args)
  15.     {
  16.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  17.  
  18.         try {
  19.             while ((line = reader.readLine()) != null) {
  20.                 if (isEmpty(line)) {
  21.                     continue;
  22.                 }
  23.  
  24.                 try {
  25.                     System.out.printf("%.5f\n", eval(line));
  26.                 } catch (Exception e) {
  27.                     System.out.println("ERROR");
  28.                 }
  29.             }
  30.         } catch (IOException e) {
  31.             System.out.println("ERROR");
  32.         }
  33.     }
  34.  
  35.     private static boolean isEmpty (String line)
  36.     {
  37.         for (char ch: line.toCharArray()) {
  38.             if (!Character.isWhitespace(ch)) {
  39.                 return false;
  40.             }
  41.         }
  42.  
  43.         return true;
  44.     }
  45.  
  46.     private static void next ()
  47.     {
  48.         do {
  49.             cursor++;
  50.         } while (current() != 0 && Character.isWhitespace(current()));
  51.     }
  52.  
  53.     private static char current ()
  54.     {
  55.         if (cursor >= line.length()) {
  56.             return 0;
  57.         }
  58.  
  59.         return line.charAt(cursor);
  60.     }
  61.  
  62.     private static double eval (String line) throws Exception
  63.     {
  64.         cursor = -1;
  65.         next();
  66.         return sub();
  67.     }
  68.  
  69.     private static double sub () throws Exception
  70.     {
  71.         double result = add();
  72.  
  73.         while (current() == '-') {
  74.             next();
  75.             result -= add();
  76.         }
  77.  
  78.         return result;
  79.     }
  80.  
  81.     private static double add () throws Exception
  82.     {
  83.         double result = div();
  84.  
  85.         while (current() == '+') {
  86.             next();
  87.             result += div();
  88.         }
  89.  
  90.         return result;
  91.     }
  92.  
  93.     private static double div () throws Exception
  94.     {
  95.         double result = mul();
  96.  
  97.         while (current() == '/') {
  98.             next();
  99.             result /= mul();
  100.         }
  101.  
  102.         return result;
  103.     }
  104.  
  105.     private static double mul () throws Exception
  106.     {
  107.         double result = val();
  108.  
  109.         while (current() == '*') {
  110.             next();
  111.             result *= val();
  112.         }
  113.  
  114.         return result;
  115.     }
  116.  
  117.     private static double val () throws Exception
  118.     {
  119.         if (current() == '(') {
  120.             next();
  121.             double result = sub();
  122.  
  123.             if (current() != ')') {
  124.                 throw new Exception();
  125.             }
  126.  
  127.             next();
  128.             return result;
  129.         }
  130.  
  131.         StringBuilder builder = new StringBuilder();
  132.  
  133.         do {
  134.             builder.append(current());
  135.             cursor++;
  136.         } while (current() != '+'
  137.                 && current() != '-'
  138.                 && current() != '*'
  139.                 && current() != '/'
  140.                 && current() != '('
  141.                 && current() != ')'
  142.                 && current() != 0
  143.         );
  144.  
  145.         String number = builder.toString();
  146.  
  147.         if (isEmpty(number)) {
  148.             throw new Exception();
  149.         }
  150.  
  151.         try {
  152.             return Double.parseDouble(number);
  153.         } catch (NumberFormatException e) {
  154.             throw new Exception();
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement