-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
113 lines (101 loc) · 2.99 KB
/
App.tsx
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
/* eslint-disable semi */
/* eslint-disable prettier/prettier */
import React, { useState } from 'react';
import { View, TextInput, Button, Text, StyleSheet } from 'react-native';
import NativeCalculatorApp from './tm/NativeCalculatorApp';
const App = () => {
const [x, setX] = useState('');
const [y, setY] = useState('');
const [result, setResult] = useState('');
const handleAddition = () => {
if (x !== '' && y !== '') {
const sum = NativeCalculatorApp.add(parseFloat(x), parseFloat(y))
setResult(`Addition Result: ${sum}`)
} else {
setResult('Please enter numbers in both fields')
}
}
const handleSubtraction = () => {
if (x !== '' && y !== '') {
const diff = NativeCalculatorApp.sub(parseFloat(x), parseFloat(y))
setResult(`Subtraction Result: ${diff}`);
} else {
setResult('Please enter numbers in both fields');
}
}
const handleMultiplication = () => {
if (x !== '' && y !== '') {
const prod = NativeCalculatorApp.mul(parseFloat(x), parseFloat(y))
setResult(`Multiplication Result: ${prod}`);
} else {
setResult('Please enter numbers in both fields');
}
}
const handleDivision = () => {
if (x !== '' && y !== '') {
const quot = NativeCalculatorApp.div(parseFloat(x), parseFloat(y))
setResult(`Division Result: ${quot}`);
} else {
setResult('Please enter numbers in both fields');
}
}
const handleTheAnswer = () => {
const answer = NativeCalculatorApp.the_answer();
setResult(`The answer to Life, The Universe, and Everything is: ${answer}`)
}
// Similarly, you can implement functions for multiplication and division
return (
<View style={styles.container}>
<TextInput
style={styles.input}
placeholder="Enter number 1"
keyboardType="numeric"
value={x}
onChangeText={(text) => setX(text)}
/>
<TextInput
style={styles.input}
placeholder="Enter number 2"
keyboardType="numeric"
value={y}
onChangeText={(text) => setY(text)}
/>
<View style={styles.buttonContainer}>
<Button title="Add" onPress={handleAddition} />
<Button title="Subtract" onPress={handleSubtraction} />
<Button title="Multiply" onPress={handleMultiplication} />
<Button title="Divide" onPress={handleDivision} />
<Button title="The Answer" onPress={handleTheAnswer} />
</View>
<Text style={styles.resultText}>{result}</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 20,
margin: 10,
},
input: {
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 5,
padding: 10,
marginBottom: 10,
width: '80%',
},
buttonContainer: {
flexDirection: 'row',
justifyContent: 'space-around',
width: '100%',
marginBottom: 20,
},
resultText: {
fontSize: 18,
fontWeight: 'bold',
},
});
export default App