0.2.0.0 - Added expression evaluation

project has new dependency of exp4j
This commit is contained in:
61616
2022-05-13 14:14:48 -04:00
parent a2034715c2
commit 7f83b4db59
6 changed files with 63 additions and 38 deletions

View File

@@ -110,7 +110,7 @@ import java.lang.ref.*;
public static class MethodRelay { public static class MethodRelay {
/** The object to handle the draw event */ /** The object to handle the draw event */
private WeakReference reference = null; private WeakReference reference = null; // Replaced the original strong reference with a weak reference so that relays will get garbage collected if the object they call get collected
//private Object handlerObject = null; //private Object handlerObject = null;
/** The method in drawHandlerObject to execute */ /** The method in drawHandlerObject to execute */
private Method handlerMethod = null; private Method handlerMethod = null;

View File

@@ -210,7 +210,8 @@ class B12Int implements Number {
class B12Float implements Number{ class B12Float implements Number{
private ArrayList<B12Digit> digits; //private ArrayList<B12Digit> digits;
private B12Digit[] digits;
private float value; private float value;
private PVector pos; private PVector pos;
private boolean arrayLoaded; private boolean arrayLoaded;
@@ -232,7 +233,20 @@ class B12Float implements Number{
PVector getPos(){ return pos; } PVector getPos(){ return pos; }
int getPlaces(){ return places; } int getPlaces(){ return places; }
B12Int toInt(){return new B12Int(int(value));} B12Int toInt(){return new B12Int(int(value));}
B12Digit[] getDigits(){ //<>//
loadArray();
B12Digit[] out = (B12Digit[])reverse(digits);
for(int i = out.length-1; i > 0; i--){
if(out[i].getValue() == '.'){
out = (B12Digit[])shorten(out);
break;
}
if(out[i].getValue() == 0){
out = (B12Digit[])shorten(out);
}
}
return out;
}
B12Float setValue(float _value){ value = _value; arrayLoaded = false; return this;} B12Float setValue(float _value){ value = _value; arrayLoaded = false; return this;}
B12Float setPos(PVector _pos){ pos = _pos.copy(); inPosition = false;return this; } B12Float setPos(PVector _pos){ pos = _pos.copy(); inPosition = false;return this; }
@@ -256,8 +270,8 @@ class B12Float implements Number{
if(!inPosition){ positionDigits(); } if(!inPosition){ positionDigits(); }
pushMatrix(); pushMatrix();
translate(pos.x,pos.y); translate(pos.x,pos.y);
for(int i = 0; i < digits.size(); i++){ for(int i = 0; i < digits.length; i++){
digits.get(i).display(); digits[i].display();
} }
popMatrix(); popMatrix();
} }
@@ -268,38 +282,38 @@ class B12Float implements Number{
if(mode == LEFT){ if(mode == LEFT){
for(int i = 0; i < pointPlace; i++){ for(int i = 0; i < pointPlace; i++){
curPos += -12; curPos += -12;
digits.get(i).setRefPos(curPos, 0); digits[i].setRefPos(curPos, 0);
count++; count++;
} }
curPos += -8; curPos += -8;
digits.get(count).setRefPos(curPos, 0); digits[count].setRefPos(curPos, 0);
count++; count++;
curPos += -6; curPos += -6;
digits.get(count).setRefPos(curPos, 0); digits[count].setRefPos(curPos, 0);
count++; count++;
for(int i = count; i < digits.size(); i++){ for(int i = count; i < digits.length; i++){
curPos += -12; curPos += -12;
digits.get(i).setRefPos(curPos, 0); digits[i].setRefPos(curPos, 0);
} }
}else if(mode == DECIMAL){ }else if(mode == DECIMAL){
curPos = -5; curPos = -5;
digits.get(pointPlace).setRefPos(curPos,0); digits[pointPlace].setRefPos(curPos,0);
curPos += -2; curPos += -2;
for(int i = pointPlace - 1; i >= 0; i--){ for(int i = pointPlace - 1; i >= 0; i--){
curPos += 12; curPos += 12;
digits.get(i).setRefPos(curPos,0); digits[i].setRefPos(curPos,0);
} }
curPos = -2; curPos = -2;
for(int i = pointPlace + 1; i < digits.size(); i++){ for(int i = pointPlace + 1; i < digits.length; i++){
curPos += -12; curPos += -12;
digits.get(i).setRefPos(curPos,0); digits[i].setRefPos(curPos,0);
} }
}else if(mode == RIGHT){ }else if(mode == RIGHT){
for(int i = digits.size() - 1; i >= 0; i--){ for(int i = digits.length - 1; i >= 0; i--){
digits.get(count).setRefPos((12 * i) + 3, 0); digits[count].setRefPos((12 * i) + 3, 0);
count++; count++;
} }
} }
@@ -307,7 +321,8 @@ class B12Float implements Number{
} }
private void loadArray(){ private void loadArray(){
digits = new ArrayList<B12Digit>(); //digits = new ArrayList<B12Digit>();
digits = new B12Digit[0];
B12Digit[] temp = new B12Digit[places]; B12Digit[] temp = new B12Digit[places];
float mval = abs(value); float mval = abs(value);
int whole = int(mval); int whole = int(mval);
@@ -320,24 +335,24 @@ class B12Float implements Number{
} }
for(int i = places - 1; i >= 0; i--){ for(int i = places - 1; i >= 0; i--){
digits.add(temp[i]); digits = (B12Digit[])append(digits,temp[i]);
} }
pointPlace = digits.size(); pointPlace = digits.length;
digits.add(new B12Digit('.')); digits = (B12Digit[])append(digits,new B12Digit('.'));
while(whole > 0){ while(whole > 0){
if(whole < 12){ if(whole < 12){
digits.add(new B12Digit(whole)); digits = (B12Digit[])append(digits,new B12Digit(whole));
whole = 0; whole = 0;
}else{ }else{
digits.add(new B12Digit(whole % 12)); digits = (B12Digit[])append(digits,new B12Digit(whole % 12));
whole /= 12; whole /= 12;
} }
} }
if(value < 0){ if(value < 0){
digits.add(new B12Digit('-')); digits = (B12Digit[])append(digits,new B12Digit('-'));
} }
arrayLoaded = true; arrayLoaded = true;

View File

@@ -1,6 +1,8 @@
import net.objecthunter.exp4j.*;
// B12NumbersV3 // // B12NumbersV3 //
String dbout = new String(""); String dbout = new String("");
float scale = 2; float scale = 4;
PVector offset; PVector offset;
public static final int DECIMAL = 65; public static final int DECIMAL = 65;
MouseHandler mh; // Mouse event handler MouseHandler mh; // Mouse event handler
@@ -15,7 +17,7 @@ STime48 time;
void setup(){ void setup(){
size(400,400); size(800,800);
offset = new PVector(width/2, height/2); offset = new PVector(width/2, height/2);
time = new STime48(); time = new STime48();
mh = new MouseHandler(new MouseData(offset, scale)); mh = new MouseHandler(new MouseData(offset, scale));
@@ -23,13 +25,13 @@ void setup(){
calc = new Calculator(mh, ex); calc = new Calculator(mh, ex);
mode = new Button(mh).setPos(new PVector(-20,-width/4), new PVector(40,20)).setRadius(2).setColor(#8B687F).autoHighlight().setText("Mode").setFunction(new MethodRelay(this, "changeMode")); mode = new Button(mh).setPos(new PVector(-20,-100), new PVector(40,20)).setRadius(2).setColor(#8B687F).autoHighlight().setText("Mode").setFunction(new MethodRelay(this, "changeMode"));
} }
void draw(){ void draw(){
textAlign(LEFT,TOP);
background(196); background(196);
textAlign(LEFT,TOP);
textSize(30);
text(dbout,0,0); text(dbout,0,0);
mh.frameUpdate(offset, scale); mh.frameUpdate(offset, scale);
stroke(0); stroke(0);
@@ -68,7 +70,7 @@ void changeMode(){
} }
calc = null; calc = null;
clock = new Clock(time).setPos(new PVector(30,0)); clock = new Clock(time).setPos(new PVector(30,0));
changeTime = new Button(mh).setPos(new PVector(-40,-width/4 + 30), new PVector(80,20)).setRadius(2).setColor(#B096A7).autoHighlight().setText("Change Time").setFunction(new MethodRelay(clock, "setTime", Time48.class)); changeTime = new Button(mh).setPos(new PVector(-40,-60), new PVector(80,20)).setRadius(2).setColor(#B096A7).autoHighlight().setText("Change Time").setFunction(new MethodRelay(clock, "setTime", Time48.class));
changeTime.setData(new Time48(12,0,0)); changeTime.setData(new Time48(12,0,0));
Runtime.getRuntime().gc(); Runtime.getRuntime().gc();
} }

View File

@@ -7,7 +7,7 @@ class B12Expression {
// GETTERS // // GETTERS //
B12Digit getDigit(int index){ return expression[index]; } B12Digit getDigit(int index){ return expression[index]; }
int length(){return expression.length;} int length(){if(expression == null){return 0;} return expression.length;}
// SETTERS // // SETTERS //
B12Expression insertChar(int ind, B12Digit _digit){ B12Expression insertChar(int ind, B12Digit _digit){
@@ -35,10 +35,11 @@ class B12Expression {
} }
void evaluate(){ void evaluate(){
String evalString = parseDigits(); String evalString = parseDigits(); //<>//
println(evalString); Expression exp = new ExpressionBuilder(evalString).build();
dbout = evalString; expression = new B12Float((float)exp.evaluate()).setPlaces(12).getDigits();
//<>// //<>// println(exp.evaluate());
dbout = str((float)exp.evaluate());
} }
private String parseDigits(){ private String parseDigits(){

View File

@@ -1,3 +1,4 @@
class MathPad{ class MathPad{
private B12Expression ex; private B12Expression ex;
private MouseHandler mh; private MouseHandler mh;
@@ -41,7 +42,7 @@ class MathPad{
buttons = (Button[])append(buttons, new B12Button(mh, new B12Digit('*')).setPos(new PVector(pos.x + 22*4,pos.y)).setFunction(new MethodRelay(this, "addChar", B12Digit.class)).setColor(220,150)); buttons = (Button[])append(buttons, new B12Button(mh, new B12Digit('*')).setPos(new PVector(pos.x + 22*4,pos.y)).setFunction(new MethodRelay(this, "addChar", B12Digit.class)).setColor(220,150));
buttons = (Button[])append(buttons, new B12Button(mh, new B12Digit('/')).setPos(new PVector(pos.x + 22*4,pos.y + 22)).setFunction(new MethodRelay(this, "addChar", B12Digit.class)).setColor(220,150)); buttons = (Button[])append(buttons, new B12Button(mh, new B12Digit('/')).setPos(new PVector(pos.x + 22*4,pos.y + 22)).setFunction(new MethodRelay(this, "addChar", B12Digit.class)).setColor(220,150));
buttons = (Button[])append(buttons, new B12Button(mh, new B12Digit('.')).setPos(new PVector(pos.x + 22*4,pos.y + 22*2)).setFunction(new MethodRelay(this, "addChar", B12Digit.class)).setColor(220,150)); buttons = (Button[])append(buttons, new B12Button(mh, new B12Digit('.')).setPos(new PVector(pos.x + 22*4,pos.y + 22*2)).setFunction(new MethodRelay(this, "addChar", B12Digit.class)).setColor(220,150));
buttons = (Button[])append(buttons, new Button(mh).setText("Enter").setPos(new PVector(pos.x + 22*4,pos.y + 22*3)).setDim(new PVector(42,20)).setFunction(new MethodRelay(this.ex, "evaluate")).setColor(220,150)); //<>// buttons = (Button[])append(buttons, new Button(mh).setText("Enter").setPos(new PVector(pos.x + 22*4,pos.y + 22*3)).setDim(new PVector(42,20)).setFunction(new MethodRelay(this.ex, "evaluate")).setColor(220,150));
buttons = (Button[])append(buttons, new Button(mh).setText("Cl").setPos(new PVector(pos.x + 22*5,pos.y + 22)).setDim(new PVector(20,42)).setFunction(new MethodRelay(this.ex, "clear")).setColor(220,150)); buttons = (Button[])append(buttons, new Button(mh).setText("Cl").setPos(new PVector(pos.x + 22*5,pos.y + 22)).setDim(new PVector(20,42)).setFunction(new MethodRelay(this.ex, "clear")).setColor(220,150));
buttons = (Button[])append(buttons, new Button(mh).setText("Del").setPos(new PVector(pos.x + 22*5,pos.y)).setDim(new PVector(20,20)).setFunction(new MethodRelay(this.ex, "delete")).setColor(220,150)); buttons = (Button[])append(buttons, new Button(mh).setText("Del").setPos(new PVector(pos.x + 22*5,pos.y)).setDim(new PVector(20,20)).setFunction(new MethodRelay(this.ex, "delete")).setColor(220,150));
} }

View File

@@ -3,19 +3,25 @@
Beta version of a clock in base 12. Beta version of a clock in base 12.
by Nayan Sawyer by Nayan Sawyer
started Mar 2022 started Mar 2022
version 0.1.5.8 April 30 2022 version 0.2.0.0 April 30 2022
Characters are a variation of Kaktovik Inupiaq numerals Characters are a variation of Kaktovik Inupiaq numerals
reversed and in base 12 instead of 20. I take no credit reversed and in base 12 instead of 20. I take no credit
for the design. for the design.
Includes method relay code be Quark - see https://forum.processing.org/two/discussion/13093/how-to-call-function-by-string-content.html Includes method relay code by Quark - see https://forum.processing.org/two/discussion/13093/how-to-call-function-by-string-content.html
for more details. for more details.
// TODO add actual math evaluation to B12Expression // Once thiss is done we hit version 0.2.0.0 // // TODO switch B12Int from ArrayList to Array
// DONE add actual math evaluation to B12Expression // Once thiss is done we hit version 0.2.0.0 //
// TODO add throwing exceptions to all contructors // TODO add throwing exceptions to all contructors
// MAYBE start clock widget structure // MAYBE start clock widget structure
// MAYBE add additional operations like power, log, and trig functions // MAYBE add additional operations like power, log, and trig functions
changelog 0.2.0.0
- Evaluating expressions has been fully implemented using
exp4j. Various things have been added to the base classes
to support this.
changelog 0.1.5.8 changelog 0.1.5.8
- Tweaks for first beta release for class presentation - Tweaks for first beta release for class presentation