Browse Source

Add log functions

master
Micah Halter 5 years ago
parent
commit
f8eb1fee0e
6 changed files with 75 additions and 56 deletions
  1. BIN
      Calculator-v1.1.jar
  2. +2
    -2
      build.xml
  3. +1
    -1
      src/main/java/com/mehalter/calculator/ui/BottomPanel.java
  4. +10
    -9
      src/main/java/com/mehalter/calculator/ui/ConstantsPanel.java
  5. +42
    -25
      src/main/java/com/mehalter/calculator/ui/OpPanel.java
  6. +20
    -19
      src/main/java/com/mehalter/calculator/ui/TrigPanel.java

BIN
Calculator-v1.1.jar View File


+ 2
- 2
build.xml View File

@@ -29,13 +29,13 @@
<classpath refid="Calculator.classpath"/>
</javac>
</target>
<target name="Calculator">
<target depends="build" name="Calculator">
<java classname="com.mehalter.calculator.CalculatorRunner" failonerror="true" fork="yes">
<classpath refid="Calculator.classpath"/>
</java>
</target>
<target depends="build" name="jar">
<jar destfile="Calculator-v1.0.jar" filesetmanifest="mergewithoutmain">
<jar destfile="Calculator-v1.1.jar" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="com.mehalter.calculator.CalculatorRunner"/>
<attribute name="Class-Path" value="."/>


+ 1
- 1
src/main/java/com/mehalter/calculator/ui/BottomPanel.java View File

@@ -14,7 +14,7 @@ public class BottomPanel extends JPanel {

public BottomPanel(TextFields stacks, Utils utils, CalcState state) {
this.setLayout(new GridLayout(2, 1));
this.add(new ConstantsPanel(stacks, utils));
this.add(new ConstantsPanel(stacks, state, utils));
this.add(new TrigPanel(stacks, state, utils));
}



+ 10
- 9
src/main/java/com/mehalter/calculator/ui/ConstantsPanel.java View File

@@ -9,6 +9,7 @@ import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;

import com.mehalter.calculator.model.CalcState;
import com.mehalter.calculator.model.TextFields;
import com.mehalter.calculator.model.Utils;

@@ -19,7 +20,7 @@ public class ConstantsPanel extends JPanel {
private TextFields stacks;
private Utils utils;

public ConstantsPanel(TextFields stacks, Utils utils) {
public ConstantsPanel(TextFields stacks, CalcState state, Utils utils) {
this.stacks = stacks;
this.utils = utils;
setLayout(new GridLayout(1,5));
@@ -28,14 +29,14 @@ public class ConstantsPanel extends JPanel {
JButton[] knownKeys = new JButton[5];
knownKeys[0] = new SimpleButton("\u03C0");
knownKeys[0].addActionListener(new constantListener(knownKeys[0], Double.toString(Math.PI)));
knownKeys[1] = new SimpleButton("\u03C4");
knownKeys[1].addActionListener(new constantListener(knownKeys[1], Double.toString(Math.PI * 2)));
knownKeys[2] = new SimpleButton("Pau");
knownKeys[2].addActionListener(new constantListener(knownKeys[2], Double.toString(Math.PI * 1.5)));
knownKeys[3] = new SimpleButton("\u2107");
knownKeys[3].addActionListener(new constantListener(knownKeys[3], Double.toString(Math.E)));
knownKeys[4] = new SimpleButton("\u03C6");
knownKeys[4].addActionListener(new constantListener(knownKeys[4], Double.toString(1 + Math.sqrt(5)/2)));
knownKeys[1] = new SimpleButton("\u2107");
knownKeys[1].addActionListener(new constantListener(knownKeys[1], Double.toString(Math.E)));
knownKeys[2] = new SimpleButton("sin");
knownKeys[2].addActionListener(new trigListener(knownKeys[2], stacks, utils, state));
knownKeys[3] = new SimpleButton("cos");
knownKeys[3].addActionListener(new trigListener(knownKeys[3], stacks, utils, state));
knownKeys[4] = new SimpleButton("tan");
knownKeys[4].addActionListener(new trigListener(knownKeys[4], stacks, utils, state));
for (int k = 0; k < knownKeys.length; k++)
add(knownKeys[k]);
}


+ 42
- 25
src/main/java/com/mehalter/calculator/ui/OpPanel.java View File

@@ -26,30 +26,36 @@ public class OpPanel extends JPanel {
this.state = state;
this.utils = utils;
this.stacks = stacks;
setLayout(new GridLayout(4,2));
setLayout(new GridLayout(4,3));
setBackground(Color.WHITE);
setPreferredSize(new Dimension(170,0));
opKeys = new JButton[8];
opKeys[0] = new SimpleButton("\u2713" + "x");
setPreferredSize(new Dimension(265,0));
opKeys = new JButton[12];
opKeys[0] = new SimpleButton("<html>log<sub>y</sub></html>");
opKeys[0].addActionListener(new opListener(opKeys[0]));
opKeys[1] = new SimpleButton("+");
opKeys[1] = new SimpleButton("log");
opKeys[1].addActionListener(new opListener(opKeys[1]));
opKeys[2] = new SimpleButton("Pow");
opKeys[2] = new SimpleButton("ln");
opKeys[2].addActionListener(new opListener(opKeys[2]));
opKeys[3] = new SimpleButton("-");
opKeys[3] = new SimpleButton("\u2713" + "x");
opKeys[3].addActionListener(new opListener(opKeys[3]));
opKeys[4] = new SimpleButton("E");
opKeys[4].addActionListener(e -> {
opKeys[4] = new SimpleButton("<html>y<sup>x</sup></html>");
opKeys[4].addActionListener(new opListener(opKeys[4]));
opKeys[5] = new SimpleButton("<html>e<sup>x</sup></html>");
opKeys[5].addActionListener(new opListener(opKeys[5]));
opKeys[6] = new SimpleButton("E");
opKeys[6].addActionListener(e -> {
if (!stacks.get(4).contains("E") && !stacks.get(4).isEmpty()) {
String editText = stacks.get(4);
utils.ifEditifOp();
stacks.setText(4, editText + "E");
}
});
opKeys[5] = new SimpleButton("*");
opKeys[5].addActionListener(new opListener(opKeys[5]));
opKeys[6] = new SimpleButton("Ent");
opKeys[6].addActionListener(new ActionListener() {
opKeys[7] = new SimpleButton("*");
opKeys[7].addActionListener(new opListener(opKeys[7]));
opKeys[8] = new SimpleButton("/");
opKeys[8].addActionListener(new opListener(opKeys[8]));
opKeys[9] = new SimpleButton("Ent");
opKeys[9].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!stacks.get(4).endsWith("E")) {
if (stacks.get(4).isEmpty()) {
@@ -72,8 +78,10 @@ public class OpPanel extends JPanel {
}
}
});
opKeys[7] = new SimpleButton("/");
opKeys[7].addActionListener(new opListener(opKeys[7]));
opKeys[10] = new SimpleButton("+");
opKeys[10].addActionListener(new opListener(opKeys[10]));
opKeys[11] = new SimpleButton("-");
opKeys[11].addActionListener(new opListener(opKeys[11]));
for(int k = 0; k < opKeys.length; k++)
add(opKeys[k]);
}
@@ -100,31 +108,40 @@ public class OpPanel extends JPanel {
double x = state.popStack();
double y = state.popStack();
switch (click.getText()) {
case "+": ans = y + x; break;
case "-": ans = y - x; break;
case "*": ans = y * x; break;
case "/": ans = y / x; break;
case "Pow": ans = Math.pow(y, x); break;
case "\u2713" + "x": ans = Math.sqrt(x); state.pushStack(ans); break;
case "+": ans = y + x; break;
case "-": ans = y - x; break;
case "*": ans = y * x; break;
case "/": ans = y / x; break;
case "<html>y<sup>x</sup></html>": ans = Math.pow(y, x); break;
case "<html>e<sup>x</sup></html>": ans = Math.pow(Math.E, x); state.pushStack(y); break;
case "\u2713" + "x": ans = Math.sqrt(x); state.pushStack(y); break;
case "<html>log<sub>y</sub></html>": ans = Math.log(x) / Math.log(y); break;
case "log": ans = Math.log10(x); state.pushStack(y); break;
case "ln": ans = Math.log(x); state.pushStack(y); break;
}
stacks.setText(4, ans.toString());
utils.refreshStack();
state.setOped(true);
state.setEdit(true);
} else {
if (click.getText() == "\u2713" + "x" && !stacks.get(4).endsWith("E")) {
if (click.getText() == "\u2713" + "x" || click.getText() == "log" || click.getText() == "ln" || click.getText() == "<html>e<sup>x</sup></html>"
&& !stacks.get(4).endsWith("E")) {
if (stacks.get(4).equals(""))
stacks.setText(4, "0");
state.pushStack(Double.parseDouble(stacks.get(4)));
ans = Math.sqrt(state.popStack());
switch (click.getText()) {
case "\u2713" + "x": ans = Math.sqrt(state.popStack()); break;
case "log": ans = Math.log10(state.popStack()); break;
case "ln": ans = Math.log(state.popStack()); break;
case "e^x": ans = Math.pow(Math.E, state.popStack()); break;
}
stacks.setText(4, ans.toString());
utils.refreshStack();
state.setOped(true);
state.setEdit(true);
}
}

}

}

}

+ 20
- 19
src/main/java/com/mehalter/calculator/ui/TrigPanel.java View File

@@ -17,30 +17,23 @@ public class TrigPanel extends JPanel {
private static final long serialVersionUID = 8150202400626370885L;
private TextFields stacks;
private CalcState state;
private Utils utils;

public TrigPanel(TextFields stacks, CalcState state, Utils utils) {
this.stacks = stacks;
this.state = state;
this.utils = utils;
setLayout(new GridLayout(1,7));
setBackground(Color.WHITE);
setPreferredSize(new Dimension(0,50));
JButton[] knownKeys = new JButton[7];
knownKeys[0] = new SimpleButton("sin");
knownKeys[0].addActionListener(new trigListener(knownKeys[0]));
knownKeys[1] = new SimpleButton("cos");
knownKeys[1].addActionListener(new trigListener(knownKeys[1]));
knownKeys[2] = new SimpleButton("tan");
knownKeys[2].addActionListener(new trigListener(knownKeys[2]));
knownKeys[0] = new SimpleButton("sec");
knownKeys[0].addActionListener(new trigListener(knownKeys[0], stacks, utils, state));
knownKeys[1] = new SimpleButton("csc");
knownKeys[1].addActionListener(new trigListener(knownKeys[1], stacks, utils, state));
knownKeys[2] = new SimpleButton("cot");
knownKeys[2].addActionListener(new trigListener(knownKeys[2], stacks, utils, state));
knownKeys[3] = new SimpleButton("asin");
knownKeys[3].addActionListener(new trigListener(knownKeys[3]));
knownKeys[3].addActionListener(new trigListener(knownKeys[3], stacks, utils, state));
knownKeys[4] = new SimpleButton("acos");
knownKeys[4].addActionListener(new trigListener(knownKeys[4]));
knownKeys[4].addActionListener(new trigListener(knownKeys[4], stacks, utils, state));
knownKeys[5] = new SimpleButton("atan");
knownKeys[5].addActionListener(new trigListener(knownKeys[5]));
knownKeys[5].addActionListener(new trigListener(knownKeys[5], stacks, utils, state));
knownKeys[6] = new SimpleButton(state.getRad() ? "rad" : "deg");
knownKeys[6].addActionListener(e -> {
state.setRad(!state.getRad());
@@ -49,13 +42,20 @@ public class TrigPanel extends JPanel {
for (int k = 0; k < knownKeys.length; k++)
add(knownKeys[k]);
}
}

class trigListener implements ActionListener {

JButton key;
TextFields stacks;
Utils utils;
CalcState state;

public trigListener(JButton key) {
public trigListener(JButton key, TextFields stacks, Utils utils, CalcState state) {
this.key = key;
this.stacks = stacks;
this.utils = utils;
this.state = state;
}

public void actionPerformed(ActionEvent e) {
@@ -65,6 +65,9 @@ public class TrigPanel extends JPanel {
case "sin": x = Math.sin(x); break;
case "cos": x = Math.cos(x); break;
case "tan": x = Math.tan(x); break;
case "sec": x = 1.0 / Math.cos(x); break;
case "csc": x = 1.0 / Math.sin(x); break;
case "cot": x = 1.0 / Math.tan(x); break;
case "asin": x = Math.asin(x); break;
case "acos": x = Math.acos(x); break;
case "atan": x = Math.atan(x); break;
@@ -77,5 +80,3 @@ public class TrigPanel extends JPanel {
}

}

}

Loading…
Cancel
Save