001: package eg;
002:
003: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
004: // Released under the terms of the GNU General Public License version 2 or later.
005:
006: import fit.*;
007:
008: public class Calculator extends ColumnFixture {
009:
010: public float volts;
011: public String key;
012:
013: public static HP35 hp = new HP35();
014:
015: public boolean points() {
016: return false;
017: }
018:
019: public boolean flash() {
020: return false;
021: }
022:
023: public float watts() {
024: return 0.5f;
025: }
026:
027: public void reset() {
028: key = null;
029: }
030:
031: public void execute() throws Exception {
032: if (key != null) {
033: hp.key(key);
034: }
035: }
036:
037: public ScientificDouble x() {
038: return new ScientificDouble(hp.r[0]);
039: }
040:
041: public ScientificDouble y() {
042: return new ScientificDouble(hp.r[1]);
043: }
044:
045: public ScientificDouble z() {
046: return new ScientificDouble(hp.r[2]);
047: }
048:
049: public ScientificDouble t() {
050: return new ScientificDouble(hp.r[3]);
051: }
052:
053: static class HP35 {
054:
055: double r[] = { 0, 0, 0, 0 };
056: double s = 0;
057:
058: public void key(String key) throws Exception {
059: if (numeric(key)) {
060: push(Double.parseDouble(key));
061: } else if (key.equals("enter")) {
062: push();
063: } else if (key.equals("+")) {
064: push(pop() + pop());
065: } else if (key.equals("-")) {
066: double t = pop();
067: push(pop() - t);
068: } else if (key.equals("*")) {
069: push(pop() * pop());
070: } else if (key.equals("/")) {
071: double t = pop();
072: push(pop() / t);
073: } else if (key.equals("x^y")) {
074: push(Math.exp(Math.log(pop()) * pop()));
075: } else if (key.equals("clx")) {
076: r[0] = 0;
077: } else if (key.equals("clr")) {
078: r[0] = r[1] = r[2] = r[3] = 0;
079: } else if (key.equals("chs")) {
080: r[0] = -r[0];
081: } else if (key.equals("x<>y")) {
082: double t = r[0];
083: r[0] = r[1];
084: r[1] = t;
085: } else if (key.equals("r!")) {
086: r[3] = pop();
087: } else if (key.equals("sto")) {
088: s = r[0];
089: } else if (key.equals("rcl")) {
090: push(s);
091: } else if (key.equals("sqrt")) {
092: push(Math.sqrt(pop()));
093: } else if (key.equals("ln")) {
094: push(Math.log(pop()));
095: } else if (key.equals("sin")) {
096: push(Math.sin(Math.toRadians(pop())));
097: } else if (key.equals("cos")) {
098: push(Math.cos(Math.toRadians(pop())));
099: } else if (key.equals("tan")) {
100: push(Math.tan(Math.toRadians(pop())));
101: } else {
102: throw new Exception("can't do key: " + key);
103: }
104: }
105:
106: boolean numeric(String key) {
107: return key.length() >= 1
108: && (Character.isDigit(key.charAt(0)) || (key
109: .length() >= 2 && (key.charAt(0) == '-' && Character
110: .isDigit(key.charAt(1)))));
111: }
112:
113: void push() {
114: for (int i = 3; i > 0; i--) {
115: r[i] = r[i - 1];
116: }
117: }
118:
119: void push(double value) {
120: push();
121: r[0] = value;
122: }
123:
124: double pop() {
125: double result = r[0];
126: for (int i = 0; i < 3; i++) {
127: r[i] = r[i + 1];
128: }
129: return result;
130: }
131: }
132: }
|