1
+ package ArithmeticCal ;
2
+
3
+ import java .io .FileInputStream ;
4
+ import java .io .FileOutputStream ;
5
+ import java .io .IOException ;
6
+ import java .io .PrintWriter ;
7
+ import java .util .Scanner ;
8
+
9
+ public class ArithmeticCalculator
10
+ {
11
+ private char [] operators = new char [10 ];
12
+ private double [] operands = new double [10 ];
13
+ private int operatorTop = -1 ;
14
+ private int operandTop = -1 ;
15
+
16
+ public String booleanOperatorFinder (String input )
17
+ {
18
+ String [] operators = {">" , "<" , " " , " " , "==" , "!=" };
19
+
20
+ for (String str : operators )
21
+ if (input .contains (str ))
22
+ return str ;
23
+
24
+ return "" ;
25
+ }
26
+
27
+ public boolean compare (String input )
28
+ {
29
+ String operator = booleanOperatorFinder (input );
30
+ String [] numbers = splitString (input , operator );
31
+
32
+ return comparer (calculate (numbers [0 ]), calculate (numbers [1 ]), operator );
33
+ }
34
+
35
+ private static String [] splitString (String input , String delimiter )
36
+ {
37
+ String [] substrings = new String [2 ];
38
+ int delimiterIndex = input .indexOf (delimiter );
39
+
40
+ substrings [0 ] = input .substring (0 , delimiterIndex );
41
+ substrings [1 ] = input .substring (delimiterIndex + 1 );
42
+
43
+ return substrings ;
44
+ }
45
+
46
+ private static boolean comparer (double a , double b , String operator ) {
47
+ switch (operator ) {
48
+ case "≥" :
49
+ return Double .compare (a , b ) >= 0 ;
50
+ case "≤" :
51
+ return Double .compare (a , b ) <= 0 ;
52
+ case ">" :
53
+ return Double .compare (a , b ) > 0 ;
54
+ case "<" :
55
+ return Double .compare (a , b ) < 0 ;
56
+ case "==" :
57
+ return Double .compare (a , b ) == 0 ;
58
+ case "!=" :
59
+ return Double .compare (a , b ) != 0 ;
60
+ }
61
+ return true ;
62
+ }
63
+
64
+
65
+ public double calculate (String expression )
66
+ {
67
+ for (int i = 0 ; i < expression .length (); i ++)
68
+ {
69
+ char c = expression .charAt (i );
70
+
71
+ if (c == ' ' )
72
+ continue ;
73
+
74
+ if (Character .isDigit (c ) || c == '.' || (c == '-' && (i == 0 || expression .charAt (i - 1 ) == '(' )))
75
+ {
76
+ StringBuilder sb = new StringBuilder ();
77
+
78
+ while (i < expression .length () && (Character .isDigit (expression .charAt (i )) || expression .charAt (i ) == '.' || (c == '-' && (i == 0 || expression .charAt (i - 1 ) == '(' ))))
79
+ {
80
+ if (c == '-' && (i == 0 || expression .charAt (i - 1 ) == '(' ) && expression .charAt (i + 1 ) == '(' )
81
+ {
82
+ pushOperand (0 );
83
+ pushOperator ('-' );
84
+ }
85
+ else
86
+ sb .append (expression .charAt (i ));
87
+
88
+ i ++;
89
+ }
90
+
91
+ i --;
92
+
93
+ String number = sb .toString ();
94
+
95
+ if (!(number .equals ("" )))
96
+ pushOperand (Double .parseDouble (number ));
97
+ }
98
+ else if (c == '(' )
99
+ pushOperator (c );
100
+ else if (c == ')' )
101
+ {
102
+ while (operators [operatorTop ] != '(' )
103
+ evaluateTopOperator ();
104
+
105
+ popOperator ();
106
+ }
107
+ else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^' )
108
+ {
109
+ while (operatorTop != -1 && precedence (c , operators [operatorTop ]) && (operatorTop >= 0 && !(c == '^' && operators [operatorTop ] == '^' )))
110
+ evaluateTopOperator ();
111
+ pushOperator (c );
112
+ }
113
+ }
114
+
115
+ while (operatorTop != -1 )
116
+ evaluateTopOperator ();
117
+
118
+ double result = operands [operandTop ];
119
+
120
+ if (Double .isNaN (result ))
121
+ throw new ArithmeticException ("Math error: undefined operation." );
122
+
123
+ return result ;
124
+ }
125
+
126
+ private boolean precedence (char op1 , char op2 )
127
+ {
128
+ if (op2 == '(' || op2 == ')' )
129
+ return false ;
130
+ if ((op1 == '*' || op1 == '/' ) && (op2 == '+' || op2 == '-' ))
131
+ return false ;
132
+ if (op1 == '^' && (op2 == '+' || op2 == '-' || op2 == '*' || op2 == '/' ))
133
+ return false ;
134
+ return true ;
135
+ }
136
+
137
+ private void evaluateTopOperator ()
138
+ {
139
+ char operator = popOperator ();
140
+ double b = popOperand ();
141
+ double a = popOperand ();
142
+
143
+ pushOperand (applyOperator (operator , b , a ));
144
+ }
145
+
146
+ private void pushOperand (double operand )
147
+ {
148
+ if (operandTop == operands .length - 1 )
149
+ expandOperandStack ();
150
+ operands [++operandTop ] = operand ;
151
+ }
152
+
153
+ private double popOperand ()
154
+ {
155
+ if (operandTop == -1 )
156
+ throw new IllegalStateException ("Syntax error: the operand stack is empty." );
157
+ return operands [operandTop --];
158
+ }
159
+
160
+ private void pushOperator (char operator )
161
+ {
162
+ if (operatorTop == operators .length - 1 )
163
+ expandOperatorStack ();
164
+ operators [++operatorTop ] = operator ;
165
+ }
166
+
167
+ private char popOperator ()
168
+ {
169
+ if (operatorTop == -1 )
170
+ throw new IllegalStateException ("Syntax error: the operator stack is empty." );
171
+ return operators [operatorTop --];
172
+ }
173
+
174
+ private double applyOperator (char operator , double b , double a )
175
+ {
176
+ switch (operator )
177
+ {
178
+ case '+' :
179
+ return a + b ;
180
+ case '-' :
181
+ return a - b ;
182
+ case '*' :
183
+ return a * b ;
184
+ case '/' :
185
+ if (b == 0 )
186
+ throw new ArithmeticException ("Math error: division by zero." );
187
+ return a / b ;
188
+ case '^' :
189
+ return Math .pow (a , b );
190
+ }
191
+ return 0 ;
192
+ }
193
+
194
+ private void expandOperandStack ()
195
+ {
196
+ double [] newArray = new double [operands .length * 2 ];
197
+ System .arraycopy (operands , 0 , newArray , 0 , operands .length );
198
+ operands = newArray ;
199
+ }
200
+
201
+ private void expandOperatorStack ()
202
+ {
203
+ char [] newArray = new char [operators .length * 2 ];
204
+ System .arraycopy (operators , 0 , newArray , 0 , operators .length );
205
+ operators = newArray ;
206
+ }
207
+
208
+ public static void main (String [] args )
209
+ {
210
+ Scanner input = null ;
211
+ PrintWriter output = null ;
212
+
213
+ try
214
+ {
215
+ input = new Scanner (new FileInputStream ("C:\\ Users\\ apple\\ Desktop\\ Input.txt" ));
216
+ output = new PrintWriter (new FileOutputStream ("C:\\ Users\\ apple\\ Desktop\\ Output.txt" ));
217
+
218
+ while (input .hasNextLine ())
219
+ {
220
+ String expression = input .nextLine ();
221
+
222
+ try
223
+ {
224
+ ArithmeticCalculator2 calculator = new ArithmeticCalculator2 ();
225
+ if (calculator .booleanOperatorFinder (expression ).equals ("" ))
226
+ {
227
+ double result = calculator .calculate (expression );
228
+ output .print (expression );
229
+ output .println ("\t Result: " + result );
230
+ }
231
+ else
232
+ {
233
+ boolean result = calculator .compare (expression );
234
+ output .print (expression );
235
+ output .println ("\t Result: " + result );
236
+ }
237
+ }
238
+ catch (ArithmeticException aee )
239
+ {
240
+ output .print (expression );
241
+ output .println ("\t " + aee .getMessage ());
242
+ }
243
+ }
244
+ }
245
+ catch (IOException ioe )
246
+ {
247
+ System .out .println ("File not found or cannot be created." );
248
+ }
249
+ finally
250
+ {
251
+ if (input != null )
252
+ input .close ();
253
+ if (output != null )
254
+ output .close ();
255
+ }
256
+ }
257
+ }
0 commit comments