Created
March 16, 2013 18:09
-
-
Save flour4445/5177578 to your computer and use it in GitHub Desktop.
クソコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PolandNotation implements Calculable | |
{ | |
private transient String[] tokens; | |
private transient int index = 0; | |
private CalculableNode root; | |
public PolandNotation(String str) | |
{ | |
tokens = str.split(" "); | |
root = createNode(); | |
} | |
private CalculableNode createNode() | |
{ | |
String token = tokens[index++]; | |
if(token.equals("+")) | |
return new AddNode(createNode(), createNode()); | |
if(token.equals("-")) | |
return new SubtractNode(createNode(), createNode()); | |
if(token.equals("*")) | |
return new MultiplyNode(createNode(), createNode()); | |
if(token.equals("/")) | |
return new DivideNode(createNode(), createNode()); | |
double d = Double.parseDouble(token); | |
return new NumberNode(d); | |
} | |
@Override | |
public double calc() | |
{ | |
return root.calc(); | |
} | |
@Override | |
public String toString() | |
{ | |
return root.toString(); | |
} | |
} | |
interface Calculable | |
{ | |
public double calc(); | |
} | |
abstract class CalculableNode implements Calculable | |
{ | |
@Override | |
public abstract String toString(); | |
} | |
class NumberNode extends CalculableNode | |
{ | |
private final double value; | |
public NumberNode(double value) | |
{ | |
this.value = value; | |
} | |
@Override | |
public double calc() | |
{ | |
return value; | |
} | |
@Override | |
public String toString() | |
{ | |
return String.valueOf(value); | |
} | |
} | |
abstract class OperationNode extends CalculableNode | |
{ | |
protected Calculable left; | |
protected Calculable right; | |
public OperationNode(Calculable left, Calculable right) | |
{ | |
this.left = left; | |
this.right = right; | |
} | |
} | |
class AddNode extends OperationNode | |
{ | |
public AddNode(Calculable left, Calculable right) | |
{ | |
super(left, right); | |
} | |
@Override | |
public double calc() | |
{ | |
return left.calc() + right.calc(); | |
} | |
@Override | |
public String toString() | |
{ | |
return new StringBuilder("+ ").append(left).append(' ').append(right).toString(); | |
} | |
} | |
class SubtractNode extends OperationNode | |
{ | |
public SubtractNode(Calculable left, Calculable right) | |
{ | |
super(left, right); | |
} | |
@Override | |
public double calc() | |
{ | |
return left.calc() - right.calc(); | |
} | |
@Override | |
public String toString() | |
{ | |
return new StringBuilder("- ").append(left).append(' ').append(right).toString(); | |
} | |
} | |
class MultiplyNode extends OperationNode | |
{ | |
public MultiplyNode(Calculable left, Calculable right) | |
{ | |
super(left, right); | |
} | |
@Override | |
public double calc() | |
{ | |
return left.calc() * right.calc(); | |
} | |
@Override | |
public String toString() | |
{ | |
return new StringBuilder("* ").append(left).append(' ').append(right).toString(); | |
} | |
} | |
class DivideNode extends OperationNode | |
{ | |
public DivideNode(Calculable left, Calculable right) | |
{ | |
super(left, right); | |
} | |
@Override | |
public double calc() | |
{ | |
double divisor = right.calc(); | |
if(Double.isNaN(divisor/divisor)) throw new ArithmeticException("/ by zero"); | |
return left.calc() / divisor; | |
} | |
@Override | |
public String toString() | |
{ | |
return new StringBuilder("/ ").append(left).append(' ').append(right).toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment