-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
117 lines (99 loc) · 3.11 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
import React, { useState } from 'react';
import {
StyleSheet,
View,
SafeAreaView,
Text
} from 'react-native';
import Button from './src/components/Button'
import Display from './src/components/Display'
function App() {
const [displayValue, setDisplayValue] = useState('0')
const [clearDisplay, setClearDisplay] = useState(false)
const [operation, setOperation] = useState(null)
const [values, setValues] = useState([0, 0])
const [current, setCurrent] = useState(0)
addDigit = (n) => {
console.log(`n: ${n}`)
const clear = displayValue === '0' || clearDisplay
if (n === '.' && !clearDisplay && displayValue.includes('.')) {
return
}
const currentValue = clear ? '' : displayValue
console.log(`current value: ${currentValue}`)
console.log(`current value + n: ${currentValue + n}`)
const newLocal = currentValue + n;
setDisplayValue(newLocal)
console.log(`displayValue: ${displayValue}`)
setClearDisplay(false)
if (n !== '.') {
const newValue = parseFloat(newLocal)
const v = [...values]
v[current] = newValue
setValues(v)
console.log(`values: ${values}`)
}
}
clearMemory = () => {
setDisplayValue('0')
setClearDisplay(false)
setOperation(null)
setValues([0, 0])
setCurrent(0)
}
setOp = op => {
if (current == 0) {
setOperation(op)
setCurrent(1)
setClearDisplay(true)
} else {
const equals = op === '='
const v = [...values]
try {
v[0] = eval(`${v[0]} ${operation} ${v[1]}`)
} catch (e) {
v[0] = values[0]
}
v[1] = 0
setDisplayValue(`${v[0]}`)
setOperation(equals ? null : op)
setCurrent(equals ? 0 : 1)
setClearDisplay(true)
setValues(v)
}
}
return (
<SafeAreaView style={styles.container}>
<Display value={displayValue} />
<View style={styles.buttons}>
<Button label="AC" triple onClick={clearMemory} />
<Button label="/" operation onClick={() => setOp('/')} />
<Button label="7" onClick={() => addDigit('7')} />
<Button label="8" onClick={() => addDigit('8')} />
<Button label="9" onClick={() => addDigit('9')} />
<Button label="*" operation onClick={() => setOp('*')} />
<Button label="4" onClick={() => addDigit('4')} />
<Button label="5" onClick={() => addDigit('5')} />
<Button label="6" onClick={() => addDigit('6')} />
<Button label="-" operation onClick={() => setOp('-')} />
<Button label="1" onClick={() => addDigit('1')} />
<Button label="2" onClick={() => addDigit('2')} />
<Button label="3" onClick={() => addDigit('3')} />
<Button label="+" operation onClick={() => setOp('+')} />
<Button label="0" double onClick={() => addDigit('0')} />
<Button label="." onClick={() => addDigit('.')} />
<Button label="=" operation onClick={() => setOp('=')} />
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
buttons: {
flexDirection: 'row',
flexWrap: 'wrap',
}
})
export default App;