Changed visibility of classes
This commit is contained in:
parent
ff54e19845
commit
d7b66238ed
1 changed files with 121 additions and 120 deletions
|
|
@ -89,10 +89,10 @@ public class MathParser {
|
||||||
System.out.println("char: "+c);
|
System.out.println("char: "+c);
|
||||||
}
|
}
|
||||||
|
|
||||||
current = getOperation(c);
|
current = getOperation(c);
|
||||||
System.out.println("operation: "+current.getClass().getName());
|
System.out.println("operation: "+current.getClass().getName());
|
||||||
current.math1 = container;
|
current.math1 = container;
|
||||||
previus.math2 = current;
|
previus.math2 = current;
|
||||||
|
|
||||||
if(c == ')'){
|
if(c == ')'){
|
||||||
return;
|
return;
|
||||||
|
|
@ -115,15 +115,15 @@ public class MathParser {
|
||||||
|
|
||||||
private static MathOperation getOperation(char c){
|
private static MathOperation getOperation(char c){
|
||||||
switch(c){
|
switch(c){
|
||||||
case '+': return new MathAddition();
|
case '+': return new MathAddition();
|
||||||
case '-': return new MathSubtraction();
|
case '-': return new MathSubtraction();
|
||||||
case '*': return new MathMultiplication();
|
case '*': return new MathMultiplication();
|
||||||
case '/': return new MathDivision();
|
case '/': return new MathDivision();
|
||||||
case '%': return new MathModulus();
|
case '%': return new MathModulus();
|
||||||
case '^': return new MathPow();
|
case '^': return new MathPow();
|
||||||
case ')':
|
case ')':
|
||||||
case (char)0: return new EmptyMath();
|
case (char)0: return new EmptyMath();
|
||||||
default: return null;
|
default: return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,117 +138,118 @@ public class MathParser {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
abstract class Math{
|
static abstract class Math{
|
||||||
public abstract double exec();
|
public abstract double exec();
|
||||||
|
|
||||||
public abstract String toString();
|
public abstract String toString();
|
||||||
}
|
|
||||||
|
|
||||||
class MathNode extends Math{
|
|
||||||
Math math;
|
|
||||||
|
|
||||||
public double exec() {
|
|
||||||
return math.exec();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
static class MathNode extends Math{
|
||||||
return "( "+math.toString()+" )";
|
Math math;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class MathNumber extends Math{
|
public double exec() {
|
||||||
double num;
|
return math.exec();
|
||||||
|
}
|
||||||
public double exec() {
|
|
||||||
return num;
|
public String toString() {
|
||||||
}
|
return "( "+math.toString()+" )";
|
||||||
|
|
||||||
public String toString() {
|
|
||||||
return ""+num;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract class MathOperation extends Math{
|
|
||||||
Math math1;
|
|
||||||
Math math2;
|
|
||||||
|
|
||||||
public abstract double exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
class MathAddition extends MathOperation{
|
|
||||||
public double exec() {
|
|
||||||
return math1.exec() + math2.exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
|
||||||
return math1.toString()+" + "+math2.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class MathSubtraction extends MathOperation{
|
|
||||||
public double exec() {
|
|
||||||
return math1.exec() - math2.exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
|
||||||
return math1.toString()+" - "+math2.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class MathMultiplication extends MathOperation{
|
|
||||||
public double exec() {
|
|
||||||
return math1.exec() * math2.exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
|
||||||
return math1.toString()+" * "+math2.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class MathDivision extends MathOperation{
|
|
||||||
public double exec() {
|
|
||||||
return math1.exec() / math2.exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
|
||||||
return math1.toString()+" / "+math2.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class MathModulus extends MathOperation{
|
|
||||||
public double exec() {
|
|
||||||
return math1.exec() % math2.exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
|
||||||
return math1.toString()+" % "+math2.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class MathPow extends MathOperation{
|
|
||||||
public double exec() {
|
|
||||||
double ret = 1;
|
|
||||||
double tmp1 = math1.exec();
|
|
||||||
double tmp2 = math2.exec();
|
|
||||||
for(int i=0; i<tmp2 ;i++){
|
|
||||||
ret *= tmp1;
|
|
||||||
}
|
}
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
static class MathNumber extends Math{
|
||||||
return math1.toString()+"^"+math2.toString();
|
double num;
|
||||||
}
|
|
||||||
}
|
public double exec() {
|
||||||
|
return num;
|
||||||
class EmptyMath extends MathOperation{
|
}
|
||||||
public double exec() {
|
|
||||||
return math1.exec();
|
public String toString() {
|
||||||
}
|
return ""+num;
|
||||||
|
}
|
||||||
public String toString() {
|
|
||||||
return math1.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static abstract class MathOperation extends Math{
|
||||||
|
Math math1;
|
||||||
|
Math math2;
|
||||||
|
|
||||||
|
public abstract double exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
static class MathAddition extends MathOperation{
|
||||||
|
public double exec() {
|
||||||
|
return math1.exec() + math2.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return math1.toString()+" + "+math2.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class MathSubtraction extends MathOperation{
|
||||||
|
public double exec() {
|
||||||
|
return math1.exec() - math2.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return math1.toString()+" - "+math2.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class MathMultiplication extends MathOperation{
|
||||||
|
public double exec() {
|
||||||
|
return math1.exec() * math2.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return math1.toString()+" * "+math2.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class MathDivision extends MathOperation{
|
||||||
|
public double exec() {
|
||||||
|
return math1.exec() / math2.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return math1.toString()+" / "+math2.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class MathModulus extends MathOperation{
|
||||||
|
public double exec() {
|
||||||
|
return math1.exec() % math2.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return math1.toString()+" % "+math2.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class MathPow extends MathOperation{
|
||||||
|
public double exec() {
|
||||||
|
double ret = 1;
|
||||||
|
double tmp1 = math1.exec();
|
||||||
|
double tmp2 = math2.exec();
|
||||||
|
for(int i=0; i<tmp2 ;i++){
|
||||||
|
ret *= tmp1;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return math1.toString()+"^"+math2.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class EmptyMath extends MathOperation{
|
||||||
|
public double exec() {
|
||||||
|
return math1.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return math1.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue