Просмотр исходного кода

completed Sci Calculator Lab - copied from team member Saurav

Christian Sheridan 6 лет назад
Родитель
Сommit
972bd2a236
3 измененных файлов: 288 добавлений и 15 удалений
  1. 192
    0
      Calculator.java
  2. 53
    5
      Console.java
  3. 43
    10
      MainApplication.java

+ 192
- 0
Calculator.java Просмотреть файл

@@ -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
+}

+ 53
- 5
Console.java Просмотреть файл

@@ -1,11 +1,56 @@
1
- 
2
-
3
-import java.util.Scanner;
4
-
1
+import java.util.*;
2
+import java.lang.*; 
5 3
 /**
6 4
  * Created by leon on 2/9/18.
7 5
  */
8 6
 public class Console {
7
+    //Saves the input in the next open slot of the array
8
+    public static void SaveParameter(String s, String[] b){
9
+        for(int i = 0; i < b.length; i++){
10
+            if(b[i].equals("0")){
11
+                b[i] = s;
12
+                break;
13
+            }
14
+        }
15
+    }
16
+
17
+    //Saves the input in the next open slot of the array
18
+    public static void SaveParameter(Double s, Double[] b){
19
+        for(int i = 0; i < b.length; i++){
20
+            if(b[i] == null){
21
+                b[i] = s;
22
+                break;
23
+            }
24
+        } 
25
+    }
26
+
27
+    //Shifts the elements of the array back one position starting from the i position
28
+    public static void ArrShifter(int i , String[] op, Double[] num  ){
29
+        for(int j = (i+1); j < num.length -1; j++){
30
+            num[j] = num[j + 1];
31
+        }
32
+        for(int j = i; j < num.length -1; j++){
33
+            op[j] = op[j + 1];
34
+        }
35
+    }
36
+
37
+    public static void DeleteOp(int i, String[] op){
38
+        for(int j = i; j < op.length -1; j++){
39
+            op[j] = op[j + 1];
40
+        }
41
+    }
42
+    //Displays the elements of the arrays in alternating order.
43
+    public static void DisplayCurrent(Double[]num, String[]op){
44
+        for(int i = 0; i < num.length; i++){
45
+            if(num[i] != null){
46
+                System.out.print(String.format("%s ",num[i]));
47
+            }
48
+            if(!op[i].equals("0")){
49
+                System.out.print(String.format("%s ",op[i]));
50
+            }
51
+        }
52
+        System.out.print("\n");
53
+    }
9 54
 
10 55
     public static void print(String output, Object... args) {
11 56
         System.out.printf(output, args);
@@ -27,6 +72,9 @@ public class Console {
27 72
     }
28 73
 
29 74
     public static Double getDoubleInput(String prompt) {
30
-        return null;
75
+        Scanner scanner = new Scanner(System.in);
76
+        println(prompt);
77
+        Double userInput = scanner.nextDouble();
78
+        return userInput;
31 79
     }
32 80
 }

+ 43
- 10
MainApplication.java Просмотреть файл

@@ -1,17 +1,50 @@
1
- 
2
-
3 1
 /**
4 2
  * Created by leon on 2/9/18.
5 3
  */
4
+import java.util.*;
5
+
6 6
 public class MainApplication {
7 7
     public static void main(String[] args) {
8
-        Console.println("Welcome to my calculator!");
9
-        String s = Console.getStringInput("Enter a string");
10
-        Integer i = Console.getIntegerInput("Enter an integer");
11
-        Double d = Console.getDoubleInput("Enter a double.");
12
-
13
-        Console.println("The user input %s as a string", s);
14
-        Console.println("The user input %s as a integer", i);
15
-        Console.println("The user input %s as a d", d);
8
+        System.out.println("\n ========v1 Calculator========");
9
+        System.out.println("0");
10
+        boolean power = true;
11
+        Double mem = 0.0;
12
+        Double[] operandHolder = new Double[100];
13
+        String[] operatorHolder = new String[100];
14
+        Arrays.fill(operatorHolder,"0");
15
+
16
+        while(power){
17
+            Console.SaveParameter(Console.getDoubleInput("Enter a number"), operandHolder);
18
+            //Console.println("\n" + Arrays.toString(operandHolder));
19
+            //Console.println(Arrays.toString(operatorHolder) + "\n");
20
+
21
+            Console.DisplayCurrent(operandHolder,operatorHolder);
22
+
23
+            Console.SaveParameter(Console.getStringInput("Enter an operator "), operatorHolder);
24
+
25
+            //Console.println("\n" + Arrays.toString(operandHolder));
26
+            //Console.println(Arrays.toString(operatorHolder) + "\n");
27
+
28
+            Calculator.Inverter(operatorHolder, operandHolder);
29
+            //Calculator.SignInvert(operatorHolder, operandHolder);
30
+            Calculator.ClearMem(operatorHolder, operandHolder);
31
+            Calculator.MemStore(operatorHolder, operandHolder, mem);
32
+            
33
+            power = !Calculator.OperatorChecker(operatorHolder,"end");
34
+            
35
+            if(Calculator.OperatorChecker(operatorHolder, "=" )){
36
+                Calculator.BasicComputing(operatorHolder, operandHolder);
37
+                Console.DisplayCurrent(operandHolder,operatorHolder);
38
+                Console.SaveParameter(Console.getStringInput("Enter an operator "), operatorHolder);
39
+            }
40
+            
41
+            power = !Calculator.OperatorChecker(operatorHolder,"end");
42
+            Console.DisplayCurrent(operandHolder,operatorHolder);
43
+        }
44
+
45
+        //Console.println(Arrays.toString(operandHolder));
46
+        //Console.println(Arrays.toString(operatorHolder));
16 47
     }
48
+
17 49
 }
50
+