Guest User

Untitled

a guest
May 27th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. // gramatyka:
  2. // wyrazenie ::= literal | '(' wyrazenie ')' | unarny wyrazenie | wyrazenie binarny wyrazenie
  3. // unarny ::= '+' | '-'
  4. // binarny ::= '+' | '-' | '*' | '/' | '^' | '%'
  5. // literal ::= cyfra { cyfra }*
  6. // cyfra ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
  7.  
  8. import java.util.Map;
  9. import java.util.HashMap;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.io.BufferedReader;
  13.  
  14. class Parser {
  15. static class SyntaxErrorException extends Exception {
  16. SyntaxErrorException(){ }
  17. SyntaxErrorException(String s){ super(s); }
  18. }
  19.  
  20. public enum Expression {
  21. PLUS(0) { public double eval(double a, double b){ return a; } },
  22. MINUS(3) { public double eval(double a, double b){ return -a; } },
  23. ADD(1) { public double eval(double a, double b){ return a+b; } },
  24. SUB(1) { public double eval(double a, double b){ return a-b; } },
  25. MUL(2) { public double eval(double a, double b){ return a*b; } },
  26. DIV(2) { public double eval(double a, double b){ return a/b; } },
  27. POW(5){ public double eval(double a, double b){ return Math.pow(a,b); } },
  28. MOD(2){ public double eval(double a, double b){ return a%b; } };
  29.  
  30. final int priority;
  31. abstract public double eval(double a, double b);
  32.  
  33. Expression(int p){ priority = p; }
  34. }
  35.  
  36. static int _index=0;
  37. static String _operands[];
  38.  
  39. static Map<String, Expression> _unary = new HashMap<String, Expression>();
  40. static Map<String, Expression> _binary = new HashMap<String, Expression>();
  41.  
  42. static {
  43. _unary.put("+", Expression.PLUS);
  44. _unary.put("-", Expression.MINUS);
  45.  
  46. _binary.put("+", Expression.ADD);
  47. _binary.put("-", Expression.SUB);
  48. _binary.put("*", Expression.MUL);
  49. _binary.put("/", Expression.DIV);
  50. _binary.put("^", Expression.POW);
  51. _binary.put("%", Expression.MOD);
  52. }
  53.  
  54. private Parser(){ }
  55.  
  56. static public void setExpression(String expression){
  57. for(String o : _binary.keySet())
  58. expression = expression.replace(o, " " + o + " " );
  59.  
  60. expression = expression.replace("(", " ( ");
  61. expression = expression.replace(")", " ) ");
  62.  
  63. _operands = (expression+" $").trim().split("( )+");
  64. _index = 0;
  65. }
  66.  
  67. static double convert(String token) throws SyntaxErrorException {
  68. try {
  69. peek();
  70. return Double.parseDouble(token);
  71. } catch(NumberFormatException e){
  72. String c = check();
  73. if(c.equals("$"))
  74. c = "konca wyrazenia";
  75. throw new SyntaxErrorException("Nieoczekiwany symbol: '" +(token.equals("$") ? "koniec wyrazenia" : token )+"' w poblizu '"+ c +"'");
  76. }
  77. }
  78.  
  79. // zwraca kolejny element z operandow
  80. static String peek() throws SyntaxErrorException {
  81. if(_index >= _operands.length)
  82. throw new SyntaxErrorException("Zbyt krotkie wyrazenie");
  83. return _operands[_index++];
  84. }
  85.  
  86. static String check(){
  87. if(_index >= _operands.length) return "$";
  88. else return _operands[_index-1];
  89. }
  90.  
  91. static double primary(String token) throws SyntaxErrorException {
  92. if(token.equals("(")){
  93. double result = subexpr(0);
  94. if(!check().equals(")"))
  95. throw new SyntaxErrorException("Brak nawiasu zamykajacego w poblizu '"+ check()+"'");
  96. peek();
  97. return result;
  98. } else return convert(token);
  99. }
  100.  
  101. static double subexpr(int limit) throws SyntaxErrorException {
  102. double result;
  103. String token = peek();
  104.  
  105. Expression op = _unary.get(token);
  106. if(op != null) result=op.eval(subexpr(op.priority), 0);
  107. else result=primary(token);
  108.  
  109. while((op = _binary.get(check())) != null && op.priority>limit)
  110. result=op.eval(result, subexpr(op.priority));
  111. return result;
  112. }
  113.  
  114. static public double parse(String expr) throws SyntaxErrorException {
  115. setExpression(expr);
  116. if(_operands[0].equals("$")) throw new SyntaxErrorException("Brak wyrazenia, aby wyjsc nacisnij Ctrl+D");
  117. double result = subexpr(0);
  118. if(_index < _operands.length)
  119. throw new SyntaxErrorException("Zbyt dlugie wyrazenie");
  120. return result;
  121. }
  122. }
  123.  
  124. public class Kalkulator {
  125. static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  126.  
  127. static String read(){
  128. try {
  129. return in.readLine();
  130. } catch (IOException ioe) {
  131. System.out.println(ioe.getMessage());
  132. System.exit(1);
  133. }
  134. return null;
  135. }
  136.  
  137. public static void main(String args[]){
  138. String expr;
  139. while((expr = read()) != null){
  140. System.out.print("= ");
  141. try {
  142. System.out.println(Parser.parse(expr));
  143. } catch(Parser.SyntaxErrorException see) {
  144. System.out.println(see.getMessage());
  145. }
  146. }
  147. }
  148. }
Add Comment
Please, Sign In to add comment