|
@@ -0,0 +1,192 @@
|
|
1
|
+
|
|
2
|
+import java.util.*;
|
|
3
|
+import java.lang.*;
|
|
4
|
+/**
|
|
5
|
+ * Write a description of class Calculator here.
|
|
6
|
+ *
|
|
7
|
+ * @author (your name)
|
|
8
|
+ * @version (a version number or a date)
|
|
9
|
+ */
|
|
10
|
+public class Calculator
|
|
11
|
+{
|
|
12
|
+ //Checks the array for the operator b
|
|
13
|
+ public static boolean OperatorChecker(String[] s, String b){
|
|
14
|
+ for(int i = 0;i < s.length; i++){
|
|
15
|
+ if(s[i].equals(b)){
|
|
16
|
+ return true;
|
|
17
|
+ }
|
|
18
|
+ }
|
|
19
|
+ return false;
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ //Inverts the current element of the num array
|
|
23
|
+ public static void SignInvert(String[] op, Double[] num){
|
|
24
|
+ for(int i = 0; i < op.length; i++){
|
|
25
|
+ if(op[i].equals("sgn")){
|
|
26
|
+ num[i] *= -1;
|
|
27
|
+ Console.DeleteOp(0,op);
|
|
28
|
+ Console.DisplayCurrent(num,op);
|
|
29
|
+ Console.SaveParameter(Console.getStringInput(" "), op);
|
|
30
|
+ }
|
|
31
|
+ }
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ // Calculates the inversion of the double
|
|
35
|
+ public static void Inverter(String[] op, Double[] num){
|
|
36
|
+ for(int i = 0; i < op.length; i++){
|
|
37
|
+ if(op[i].equals("inv")){
|
|
38
|
+ num[i] = 1/num[i];
|
|
39
|
+ Console.DeleteOp(0,op);
|
|
40
|
+ Console.DisplayCurrent(num,op);
|
|
41
|
+ Console.SaveParameter(Console.getStringInput("Enter operator"), op);
|
|
42
|
+ }
|
|
43
|
+ }
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ public static void MemStore(String[] op,Double[] num, Double store){
|
|
47
|
+ for(int i = 0; i < op.length; i++){
|
|
48
|
+ if(op[i].equals("m+")){
|
|
49
|
+ store = num[i];
|
|
50
|
+ Console.DeleteOp(0,op);
|
|
51
|
+ Console.println(store + " is stored in memory");
|
|
52
|
+ Console.SaveParameter(Console.getStringInput("Enter operator"), op);
|
|
53
|
+ }
|
|
54
|
+ else if(op[i].equals("mc")){
|
|
55
|
+ store = 0.0;
|
|
56
|
+ Console.DeleteOp(0,op);
|
|
57
|
+ Console.println("MEMORY CLEARED");
|
|
58
|
+ Console.SaveParameter(Console.getDoubleInput("Enter a number"), num);
|
|
59
|
+ }
|
|
60
|
+ else if(op[i].equals("mrc")){
|
|
61
|
+ num[i] = store;
|
|
62
|
+ Console.DeleteOp(0,op);
|
|
63
|
+ Console.println(store + " ---> " + num[i]);
|
|
64
|
+ Console.DisplayCurrent(num,op);
|
|
65
|
+ Console.SaveParameter(Console.getStringInput("Enter operator"), op);
|
|
66
|
+ }
|
|
67
|
+ }
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ public static Double factorial(Double number){
|
|
71
|
+
|
|
72
|
+ if(number == 1){
|
|
73
|
+ return number;
|
|
74
|
+ }
|
|
75
|
+ number *= factorial(number-1);
|
|
76
|
+ return number;
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ public static void FactorialFinish(String[] op, Double[] num){
|
|
80
|
+ for(int i = 0; i < op.length; i++){
|
|
81
|
+ if(op[i].equals("!")){
|
|
82
|
+ if(num[i] == 1){
|
|
83
|
+ num[i] = num[i];
|
|
84
|
+ }
|
|
85
|
+ num[i] *= factorial(num[i-1]);
|
|
86
|
+ for(int j = i; j < op.length -1; j++){
|
|
87
|
+ op[j] = op[j+1];
|
|
88
|
+ }
|
|
89
|
+ Console.DisplayCurrent(num,op);
|
|
90
|
+ Console.SaveParameter(Console.getStringInput("Enter an Operator"), op);
|
|
91
|
+ }
|
|
92
|
+ }
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ public static void Trig(String[] op, Double[]num){
|
|
96
|
+ for(int i = 0; i < num.length; i++){
|
|
97
|
+ if(op[i].equals("sin")){
|
|
98
|
+ num[i] = Math.sin(Math.toRadians(num[i]));
|
|
99
|
+ Console.DeleteOp(i,op);
|
|
100
|
+ Console.DisplayCurrent(num,op);
|
|
101
|
+ Console.SaveParameter(Console.getStringInput("Enter an Operator"), op);
|
|
102
|
+ }
|
|
103
|
+ else if(op[i].equals("cos")){
|
|
104
|
+ num[i] = Math.cos(Math.toRadians(num[i]));
|
|
105
|
+ Console.DeleteOp(i,op);
|
|
106
|
+ Console.DisplayCurrent(num,op);
|
|
107
|
+ Console.SaveParameter(Console.getStringInput("Enter an Operator"), op);
|
|
108
|
+ }
|
|
109
|
+ else if(op[i].equals("tan")){
|
|
110
|
+ num[i] = Math.tan(Math.toRadians(num[i]));
|
|
111
|
+ Console.DeleteOp(i,op);
|
|
112
|
+ Console.DisplayCurrent(num,op);
|
|
113
|
+ Console.SaveParameter(Console.getStringInput("Enter an Operator"), op);
|
|
114
|
+ }
|
|
115
|
+ else{
|
|
116
|
+ //Just passing through
|
|
117
|
+ }
|
|
118
|
+ }
|
|
119
|
+ }
|
|
120
|
+
|
|
121
|
+ public static void ClearMem(String[] op, Double[] num){
|
|
122
|
+ for(int i = 0; i < op.length; i++){
|
|
123
|
+ if(op[i].equals("clr")){
|
|
124
|
+ Arrays.fill(num, null);
|
|
125
|
+ Arrays.fill(op, "0");
|
|
126
|
+ Console.DisplayCurrent(num,op);
|
|
127
|
+ }
|
|
128
|
+ }
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+ //Does all the Basic computing in PEMDAS form.
|
|
132
|
+ public static void BasicComputing(String[] op, Double[] num){
|
|
133
|
+ if(Calculator.OperatorChecker(op,"=")){
|
|
134
|
+ for(int check = 0; check < op.length; check++){
|
|
135
|
+
|
|
136
|
+ for(int i = 0; i < num.length; i++){
|
|
137
|
+ if(op[i].equals("^")){
|
|
138
|
+ num[i] = Math.pow(num[i], num[i+1]);
|
|
139
|
+ Console.ArrShifter(i,op, num);
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ else if(op[i].equals("rt")){
|
|
143
|
+ num[i] = Math.pow(num[i], (1/num[i+1]));
|
|
144
|
+ Console.ArrShifter(i,op, num);
|
|
145
|
+ }
|
|
146
|
+ else{
|
|
147
|
+ //Just passing through
|
|
148
|
+ }
|
|
149
|
+ }
|
|
150
|
+ for(int i = 0; i < num.length; i++){
|
|
151
|
+ if(op[i].equals("*")){
|
|
152
|
+ num[i] = (num[i] * num[i+1]);
|
|
153
|
+ Console.ArrShifter(i,op, num);
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ else if(op[i].equals("/")){
|
|
157
|
+ num[i] = (num[i] / num[i+1]);
|
|
158
|
+ Console.ArrShifter(i,op, num);
|
|
159
|
+
|
|
160
|
+ }
|
|
161
|
+ else{
|
|
162
|
+ //Just passing through
|
|
163
|
+ }
|
|
164
|
+ }
|
|
165
|
+ for(int i = 0; i < num.length; i++){
|
|
166
|
+ if(op[i].equals("+")){
|
|
167
|
+ num[i] = (num[i] + num[i+1]);
|
|
168
|
+ Console.ArrShifter(i,op, num);
|
|
169
|
+
|
|
170
|
+ }
|
|
171
|
+ else if(op[i].equals("-")){
|
|
172
|
+ num[i] = (num[i] - num[i+1]);
|
|
173
|
+ Console.ArrShifter(i,op, num);
|
|
174
|
+
|
|
175
|
+ }
|
|
176
|
+ else if (op[i].equals("=")) {
|
|
177
|
+ for(int j = i; j < op.length -1; j++){
|
|
178
|
+ op[j] = op[j+1];
|
|
179
|
+ }
|
|
180
|
+
|
|
181
|
+ break;
|
|
182
|
+ }
|
|
183
|
+ else{
|
|
184
|
+ //Just passing through
|
|
185
|
+ }
|
|
186
|
+ Calculator.BasicComputing(op, num);
|
|
187
|
+ }
|
|
188
|
+ }
|
|
189
|
+
|
|
190
|
+ }
|
|
191
|
+ }
|
|
192
|
+}
|