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