-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
117 lines (106 loc) · 2.82 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 from 'react'
import {View, Text, TouchableOpacity, Button} from 'react-native'
import styles from './src/styles/_calculator'
class App extends React.Component {
constructor() {
super()
this.state = {
calculationText: "",
resultText: ''
}
this.operation = ['Del', '+', '-', '*', '/'];
}
calculateResult() {
const text = this.state.calculationText
console.log(text, eval(text))
this.setState({
resultText: eval(text)
})
//BODMAS
// eval(text)
// now parse this text
}
validate() {
const text = this.state.calculationText
switch(text.slice(-1)) {
case '+':
case '-':
case '*':
case '/':
return false
}
return true
}
buttonbPressed(text) {
//console.log(text)
if(text == '=') {
return this.validate() && this.calculateResult()
}
this.setState({
calculationText: this.state.calculationText + text
})
}
operate(operation) {
switch(operation) {
case 'Del' :
let text = this.state.calculationText.split('')
text.pop()
this.setState({
calculationText: text.join('')
})
break;
case '+':
case '-':
case '*':
case '/':
const lastChar = this.state.calculationText.split('').pop()
if(this.operation.indexOf(lastChar) > 0) return
if(this.state.text == "") return
this.setState({
calculationText: this.state.calculationText + operation
})
}
}
render() {
let rows = [];
let numb = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [".", 0, '=']];
for(let i = 0; i < 4; i++) {
let row = []
for(let j = 0; j < 3; j++) {
row.push(
<TouchableOpacity onPress={() => this.buttonbPressed(numb[i][j])}>
<Text style={styles.btnText}>{numb[i][j]}</Text>
</TouchableOpacity>
)
}
rows.push(<View style={styles.row}>{row}</View>)
}
let oper = [];
for(let i = 0; i < 5; i++) {
oper.push(
<TouchableOpacity style={styles.btn} onPress={() => this.operate(this.operation[i])}>
<Text style={[styles.btnText, {color: 'white'}]}>{this.operation[i]}</Text>
</TouchableOpacity>
)
}
return (
<View style={styles.container}>
<View style={styles.calculation}>
<Text style={styles.calculationText}>{this.state.calculationText}</Text>
</View>
<View style={styles.result}>
<Text style={styles.resultText}>{this.state.resultText}</Text>
</View>
<View style={styles.buttons}>
<View style={styles.numbers}>
{rows}
</View>
<View style={styles.operation}>
{oper}
</View>
</View>
</View>
);
}
}
export default App;