-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
255 lines (210 loc) · 6.5 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
import React, { Component } from 'react';
import { StyleSheet, View, Dimensions } from 'react-native';
import Button from './src/components/Button';
import Display from './src/components/Display';
const initialState = {
displayValue: '0',
operation: null,
values: [0, 0],
};
export default class App extends Component {
state = { ...initialState };
/**
* Clears the display and the memory.
*/
clearMemory = () => {
this.setState({ displayValue: '0' });
this.setState({ values: [0, 0] });
};
/**
* Sets the next operation.
* @param {string} op operation
*/
setOperation = (op) => {
this.setState({ operation: op });
this.setState({ displayValue: op });
};
/**
* Executes the operation
* @param {string} op mathemathical operators ('+', '-', '*', '/')
*/
executeOperation = (op) => {
let v1 = Number(this.state.values[0]);
let v2 = Number(this.state.values[1]);
this.setState({ operation: null });
if (op === '/') {
if (! v2) {
return NaN;
}
if (v1 % v2 == 0) {
return v1 / v2;
}
return (v1 / v2).toFixed(4);
}
if (op === '*') {
if (! v2 || ! v1) {
return 0;
}
return v1 * v2;
}
if (op === '+') {
return v1 + v2;
}
if (op === '-') {
return v1 - v2;
}
}
/**
* Calculates the result
*/
getResult = () => {
const op = this.state.operation;
if (! op) {
return;
}
let result = this.executeOperation(op);
this.setState({ displayValue: result })
this.setState({ values: [result, 0] })
};
/**
* Inserts a value into the display and the memory
* @param {string} digit last digit typed.
* @param {boolean} secondValue if it is the second operation value or not
*/
insertValue = (digit, secondValue) => {
let v1 = this.state.values[0].toString();
let v2 = this.state.values[1].toString();
if (! secondValue) {
if (v1.length > 8) return;
if (digit == '0' && v1 == '0') return;
if (digit != '0' && v1 == '0') {
let newDisplay = '' + digit + '';
this.setState({ displayValue: newDisplay });
this.setState({ values: [ newDisplay , v2 ] });
}
if (digit != '0' && v1 != '0') {
let newDisplay = '' + v1 + digit + '';
this.setState({ displayValue: newDisplay });
this.setState({ values: [ newDisplay , v2 ] });
}
if (digit == '0' && v1 != '0') {
let newDisplay = '' + v1 + digit + '';
this.setState({ displayValue: newDisplay });
this.setState({ values: [ newDisplay , v2 ] });
}
} else {
this.setState({ displayValue: null })
if (v2.length > 8) return;
if (digit == '0' && v2 == '0') return;
if (digit != '0' && v2 == '0') {
let newDisplay = '' + digit + '';
this.setState({ displayValue: newDisplay });
this.setState({ values: [ v1 , newDisplay ] });
}
if (digit != '0' && v2 != '0') {
let newDisplay = '' + v2 + digit + '';
this.setState({ displayValue: newDisplay });
this.setState({ values: [ v1 , newDisplay ] });
}
if (digit == '0' && v2 != '0') {
let newDisplay = '' + v2 + digit + '';
this.setState({ displayValue: newDisplay });
this.setState({ values: [ v1 , newDisplay ] });
}
}
};
/**
* Adds a digit to the display.
* @param {string} digit pressed digit.
*/
addDigit = (digit) => {
const op = this.state.operation;
if (op) {
this.insertValue(digit, true);
} else {
this.insertValue(digit, false);
}
};
/**
* Deletes the last digit from the display and memory
*/
deleteLastDigit = () => {
let values = this.state.values;
if (values[0] == 0 && values[1] == 0) {
return;
}
if (values[0] != 0 && values[1] == 0) {
if (values[0].toString().length == 1) {
this.setState({ displayValue: 0 })
this.setState({ values: [0, 0] })
} else {
let newValue = values[0].toString().slice(0, -1);
this.setState({ displayValue: newValue })
this.setState({ values: [newValue, 0] })
}
return;
}
if (values[0] != 0 && values[1] != 0) {
if (values[1].length == 1) {
this.setState({ displayValue: 0 })
this.setState({ values: [values[0], 0] })
} else {
let newValue = values[1].slice(0, -1);
this.setState({ displayValue: newValue })
this.setState({ values: [values[0], newValue] })
}
return;
}
}
render() {
return (
<View style={styles.container}>
<Display value={this.state.displayValue} ></Display>
<View style={styles.buttons}>
<Button label='AC' double onClick={this.clearMemory} />
<Button label='C' operation onClick={this.deleteLastDigit} />
<Button label='/' operation onClick={() => this.setOperation('/')} />
</View>
<View style={styles.buttons}>
<Button label='7' onClick={() => this.addDigit(7)} />
<Button label='8' onClick={() => this.addDigit(8)} />
<Button label='9' onClick={() => this.addDigit(9)} />
<Button label='*' operation onClick={() => this.setOperation('*')} />
</View>
<View style={styles.buttons}>
<Button label='4' onClick={() => this.addDigit(4)} />
<Button label='5' onClick={() => this.addDigit(5)} />
<Button label='6' onClick={() => this.addDigit(6)} />
<Button label='-' operation onClick={() => this.setOperation('-')} />
</View>
<View style={styles.buttons}>
<Button label='1' onClick={() => this.addDigit(1)} />
<Button label='2' onClick={() => this.addDigit(2)} />
<Button label='3' onClick={() => this.addDigit(3)} />
<Button label='+' operation onClick={() => this.setOperation('+')} />
</View>
<View style={styles.buttons}>
<Button label='0' double onClick={() => this.addDigit(0)} />
<Button label='.' onClick={() => this.addDigit('.')} />
<Button label='=' operation onClick={() => this.getResult()} />
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
buttons: {
flexDirection: 'row',
flexWrap: 'wrap',
width: Dimensions.get('window').width,
},
zButton : {
justifyContent: 'flex-end',
flexDirection: 'row',
flexWrap: 'wrap',
}
});