-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
314 lines (284 loc) · 11.9 KB
/
App.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* John Hu; this is my comments section:
This is my calculator app with a cool blue green theme. It allows users to do basic operations and it includes square root and percentage.
Calculator capabilities:
:All four operations work: add, subtract, multiply, divide as well as clear button
:Decimal works. By using hasDecimal boolean variable, app only allows one decimal per a number
:Includes two additional operations ====> Unlike before, I added lastOperationIndex and buttonCount variables which allow me to use negative sign, square root, and percetage on the correct number. For example, take 35*45. Before, these buttons would only do the calculation on 35, the first number, like -35*45 even though the current number is 45. Now, it will appear like 35*-45. So, square roots, percetage, and negative sign all work
:Able to do several operations before hitting "="
:Explored another component not discussed in class ==> touchableOpacity and OnPress
:App cannot crash; when hitting random buttons, the answer/display text will output "ERROR" but cannot crash. This error message is easily removed by using the clear button
Calculator limitations:
:Biggest limitation ==> cannot do PEMDAS since my getSolution method evaluates the given expression from left to right, but Mr.Horner said that PEMDAS is not necessary.
:User must input a number then add negative; won't work if you first click negative sign without a number because it essentially multiplies the number by -1
:Small limitation ==> When you try to enter random buttons that dont make sense in the displayEquation text (not the result text), the app will output "NaN" in that text. I am unable to change it to "Error" unlike in the result text
*/
import React, { useState } from 'react';
import { Text, View, StyleSheet, TextInput, Button, Alert, TouchableOpacity, ScrollView } from 'react-native';
import Constants from 'expo-constants';
import AssetExample from './components/AssetExample';
import {Picker} from '@react-native-picker/picker';
import { Card } from 'react-native-paper';
var hasDecimal = false;
let lastOperationIndex = 0, buttonCount = 0; //use these variables to find the last index of a operation. This will allow negative, percent, root to work only on the current number. Use of for loops takes too much time
export default function App() {
const [displayEquation, changeEquationDisplay] = useState(" "); //equation line
const [result, changeResult] = useState("0"); //answer line
const decimalChange = () => {hasDecimal = !hasDecimal;}
const buttonClick = (input) => {
if (input === "C") {
changeEquationDisplay(" ");
changeResult("0");
hasDecimal = false;
buttonCount = 0;
lastOperationIndex = 0;
} else if (input === "root") {
if (result !== " " && result !== "0" && result !== "") {
changeResult(Math.sqrt(parseFloat(result.toString().substring(lastOperationIndex+1))));
} else {
if (displayEquation.toString().length < 3) {
changeEquationDisplay(Math.sqrt(parseFloat(displayEquation)));
} else {
changeEquationDisplay(displayEquation.toString().substring(0,lastOperationIndex+2) + Math.sqrt(parseFloat(displayEquation.toString().substring(lastOperationIndex+2))));
}
}
} else if (input === "neg") {
if (displayEquation.toString().length < 3) {
changeEquationDisplay(parseFloat(displayEquation)*-1);
} else {
console.log(displayEquation.toString().length);
changeEquationDisplay(displayEquation.toString().substring(0,lastOperationIndex+2) + parseFloat(displayEquation.toString().substring(lastOperationIndex+2))*-1);
}
} else if (input === ".") {
if (!hasDecimal) {
changeEquationDisplay(displayEquation + input);
decimalChange();
}
} else if (input === "%") {
if (result !== " " && result !== "0" && result !== "") {
changeResult(result.toString().substring(0,lastOperationIndex+2) + parseFloat(result.toString().substring(lastOperationIndex+2))*0.01);
} else {
if (displayEquation.toString().length < 3) {
changeEquationDisplay(parseFloat(displayEquation)*0.01);
} else {
changeEquationDisplay(displayEquation.toString().substring(0,lastOperationIndex+2) + parseFloat(displayEquation.toString().substring(lastOperationIndex+2))*0.01);
}
}
} else if (input === "+" || input === "×" || input === "−" || input === "÷") {
changeEquationDisplay(displayEquation + input);
operation = input;
lastOperationIndex = buttonCount;
buttonCount++;
decimalChange();
} else if (input === "=") {
getSolution();
} else {
changeEquationDisplay(displayEquation + input);
buttonCount++;
}
}
const getSolution = () => {
let string = displayEquation.toString();
const operations = [], numbers = [], finalNums = [];
let opIndex = 0, addIndex = 0;
let currentNumberDealer = "";
for (let i = 0; i < string.length; i++) {
if ((string.substring(i,i+1) !== "+" && string.substring(i,i+1) !== "×" && string.substring(i,i+1) !== "−" && string.substring(i,i+1) !== "÷")) {
currentNumberDealer += string.substring(i,i+1);
} else { //if index lands on operation
numbers[addIndex] = currentNumberDealer;
currentNumberDealer = "";
addIndex++;
operations[opIndex] = string.substring(i,i+1);
opIndex++;
}
}
numbers[addIndex] = currentNumberDealer;
//convert string numbers to numbers in array
for (let j = 0; j < numbers.length; j++) {
finalNums[j] = parseFloat(numbers[j]);
}
//make calculations
let result = 0;
result += finalNums[0];
for (let i = 1; i < finalNums.length; i++) {
if (operations[i-1] === "+") {
result += finalNums[i];
} else if (operations[i-1] === "−") {
result -= finalNums[i];
} else if (operations[i-1] === "×") {
result *= finalNums[i];
} else if (operations[i-1] === "÷") {
result /= finalNums[i];
}
}
if (isNaN(result)) {
changeResult("Error");
} else {
changeResult(result);
}
}
return (
<View style={styles.buffer, {backgroundColor: 'black'}}>
<View style={styles.buffer}>
</View>
<Text style={styles.equationText}>{displayEquation}</Text>
<Text style = {styles.input}>{result}</Text>
<View style={styles.container}>
<View style = {styles.left, {backgroundColor: 'black'}}>
<TouchableOpacity onPress={()=>{buttonClick("C")}} style={styles.buttonStyleOther}>
<Text style={styles.buttonTextOther}>C</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick(7)}} style={styles.buttonStyleNumbers}>
<Text style={styles.buttonTextNumbers}>7</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick(4)}} style={styles.buttonStyleNumbers}>
<Text style={styles.buttonTextNumbers}>4</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick(1)}} style={styles.buttonStyleNumbers}>
<Text style={styles.buttonTextNumbers}>1</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick(".")}} style={styles.buttonStyleNumbers}>
<Text style={styles.buttonTextNumbers}>.</Text>
</TouchableOpacity>
</View>
<View style = {styles.left, {backgroundColor: 'black'}}>
<TouchableOpacity onPress={()=>{buttonClick("root")}} style={styles.buttonStyleOther}>
<Text style={styles.buttonTextOther}>√x</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick(8)}} style={styles.buttonStyleNumbers}>
<Text style={styles.buttonTextNumbers}>8</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick(5)}} style={styles.buttonStyleNumbers}>
<Text style={styles.buttonTextNumbers}>5</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick(2)}} style={styles.buttonStyleNumbers}>
<Text style={styles.buttonTextNumbers}>2</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick(0)}} style={styles.buttonStyleNumbers}>
<Text style={styles.buttonTextNumbers}>0</Text>
</TouchableOpacity>
</View>
<View style = {styles.left, {backgroundColor: 'black'}}>
<TouchableOpacity onPress={()=>{buttonClick("%")}} style={styles.buttonStyleOther}>
<Text style={styles.buttonTextOther}>%</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick(9)}} style={styles.buttonStyleNumbers}>
<Text style={styles.buttonTextNumbers}>9</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick(6)}} style={styles.buttonStyleNumbers}>
<Text style={styles.buttonTextNumbers}>6</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick(3)}} style={styles.buttonStyleNumbers}>
<Text style={styles.buttonTextNumbers}>3</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick("neg")}} style={styles.buttonStyleOther}>
<Text style={styles.buttonTextOther}>±</Text>
</TouchableOpacity>
</View>
<View style = {styles.left, {backgroundColor: 'black'}}>
<TouchableOpacity onPress={()=>{buttonClick("÷")}} style={styles.buttonStyle}>
<Text style={styles.buttonText}>÷</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick("×")}} style={styles.buttonStyle}>
<Text style={styles.buttonText}>×</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick("−")}} style={styles.buttonStyle}>
<Text style={styles.buttonText}>−</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick("+")}} style={styles.buttonStyle}>
<Text style={styles.buttonText}>+</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{buttonClick("=")}} style={styles.buttonStyle}>
<Text style={styles.buttonText}>=</Text>
</TouchableOpacity>
</View>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
height: '100%',
flexDirection: 'row',
alignItems: 'start',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#000000',
},
buffer: {
height: '0%'
},
left: {
alignItems: 'center',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ffffff',
},
buttonTextNumbers: {
fontSize: 25,
color: '#adadc9',
},
buttonTextOther: {
fontSize: 25,
color: '#c5c6d0',
},
buttonText: {
fontSize: 25,
color: '#59778e',
},
buttonStyleNumbers: {
height: 75,
width: 75,
marginTop: 15,
backgroundColor: '#59515e',
borderRadius: 50,
alignItems: 'center',
justifyContent: 'center',
marginHorizontal: 10,
marginVertical: 8,
},
equationText: {
fontFamily: "Cochin",
fontStyle: 'italic',
height: 50,
width: '100%',
color: '#8abaae',
fontSize: 35,
marginTop: 20,
marginHorizontal: 30,
marginVertical: 10,
textAlign: 'left',
},
buttonStyleOther: {
height: 75,
width: 75,
marginTop: 15,
backgroundColor: '#9897a9',
borderRadius: 100,
alignItems: 'center',
justifyContent: 'center',
marginHorizontal: 10,
marginVertical: 8,
},
buttonStyle: {
height: 75,
width: 75,
marginTop: 15,
backgroundColor: '#8DA399',
borderRadius: 100,
alignItems: 'center',
justifyContent: 'center',
marginHorizontal: 10,
marginVertical: 8,
},
input: {
color: '#36454F',
fontFamily: "Cochin",
height: "15%",
width: "100%",
textAlign: 'right',
borderColor: "skyblue",
fontSize: 95,
paddingRight: 20,
},
});