-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalcEngineString.java
77 lines (68 loc) · 1.56 KB
/
CalcEngineString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* The main part of the calculator doing the calculations.
*
* @author n-c0de-r, jonasblome, and joeysmeets
* @version 12.06.21
*/
public class CalcEngineString {
private String displayString = "";
private Postfix postfix;
public CalcEngineString() {
postfix = new Postfix();
}
/**
* @return The string that should be calculated by postfix.
*/
public String getDisplayString() {
return displayString;
}
/**
* Extend the string to evaluate
*
* @param number The number pressed on the calculator.
*/
public void buttonPressed(String number) {
displayString = displayString + number;
displayString += " ";
}
/**
* Calculate the dec / hex output
*
* @param mode The base mode to parse to.
*/
public void equals(int mode) {
if (displayString != null) {
String pfx = postfix.infixToPostfix(displayString);
if (mode == 10) {
displayString = "" + postfix.evaluate(pfx);
} else {
// Cast Doubles to integer, as hex can't calculate doubles anyways!
displayString = "" + Integer.toHexString((int) (postfix.evaluate(pfx)));
}
}
}
/**
* The 'C' (clear) button was pressed. Reset everything to a starting state.
*/
public void clear() {
displayString = "";
}
/**
* @return The title of this calculation engine.
*/
public String getTitle() {
return "Better Java Calculator";
}
/**
* @return The author of this engine.
*/
public String getAuthor() {
return "D.J. Barnes & M. Kolling";
}
/**
* @return The version number of this engine.
*/
public String getVersion() {
return "Version 2";
}
}