Advertisement
Felanpro

React Native (Calculator Version 3)

May 31st, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. // THIS CALCULATOR LOGIC IS SO FAR THE BEST
  2.  
  3. import { StatusBar } from 'expo-status-bar';
  4. import { StyleSheet, Text, View, SafeAreaView, TouchableHighlight, TouchableOpacity, Button } from 'react-native';
  5. import React, { useState, useContext } from 'react';
  6.  
  7. Calculator = () =>
  8. {
  9. const [firstNumber, setFirstNumber] = useState("");
  10. const [operation, setOperation] = useState(null);
  11. const [result, setResult] = useState(0);
  12. const [initialState, setInitialState] = useState(true);
  13. const [textField, setTextField] = useState("");
  14.  
  15. function onNumberPress(buttonValue)
  16. {
  17. setFirstNumber(firstNumber + buttonValue);
  18. setTextField(firstNumber + buttonValue);
  19. }
  20.  
  21. function onOperatorPress(buttonValue)
  22. {
  23. switch(buttonValue)
  24. {
  25. case "+":
  26. setOperation("+");
  27. break;
  28.  
  29. case "-":
  30. setOperation("-");
  31. break;
  32.  
  33. default:
  34. break;
  35. }
  36.  
  37. if(initialState == true)
  38. {
  39. if(firstNumber != "")
  40. {
  41. setInitialState(false);
  42. setResult(parseInt(firstNumber));
  43. setFirstNumber("");
  44. }
  45. }
  46. else if(initialState == false)
  47. {
  48.  
  49. if( (firstNumber != "") )
  50. {
  51. switch(operation)
  52. {
  53. case "+":
  54. setResult(parseInt(result) + parseInt(firstNumber));
  55. setTextField(parseInt(result) + parseInt(firstNumber));
  56. break;
  57.  
  58. case "-":
  59. setResult(parseInt(result) - parseInt(firstNumber));
  60. setTextField(parseInt(result) - parseInt(firstNumber));
  61. break;
  62.  
  63. default:
  64. break;
  65. }
  66. setFirstNumber("");
  67. console.log("Calculation performed!");
  68. }
  69. }
  70. }
  71.  
  72. function onEqualsPress()
  73. {
  74. if( (initialState == false) && (firstNumber != "") )
  75. {
  76.  
  77. switch(operation)
  78. {
  79. case "+":
  80. setResult(parseInt(result) + parseInt(firstNumber));
  81. setTextField(parseInt(result) + parseInt(firstNumber));
  82. break;
  83.  
  84. case "-":
  85. setResult(parseInt(result) - parseInt(firstNumber));
  86. setTextField(parseInt(result) - parseInt(firstNumber));
  87. break;
  88.  
  89. default:
  90. break;
  91. }
  92.  
  93. setFirstNumber("");
  94. }
  95. }
  96.  
  97. function onClearPress()
  98. {
  99. setFirstNumber("");
  100. setTextField("");
  101. setResult(null);
  102. setOperation(null);
  103. setInitialState(true);
  104. }
  105.  
  106. // Write status to console to keep track of values
  107. console.log("firstNumber: " + firstNumber);
  108. console.log("operation: " + operation);
  109. console.log("result: " + result);
  110. console.log("---------------------------")
  111.  
  112. // Render all of the UI of Calculator ------------
  113. return(
  114. <View style={{
  115. flexDirection: "column",
  116. flex: 1,
  117. backgroundColor: "black"
  118. }}>
  119.  
  120. <View style={{
  121. flexDirection: "row",
  122. flex: 1,
  123. height: "60%",
  124. }}>
  125. <Text style={{
  126. color: "#fff",
  127. fontSize: 50
  128. }}>{textField}</Text>
  129. </View>
  130.  
  131. <View style={{
  132. flexDirection: "row",
  133. flex: 1,
  134. flexWrap: "wrap",
  135. height: "40%",
  136. backgroundColor: "orange"
  137. }}>
  138.  
  139. <TouchableHighlight style={styles.button} onPress={() => onNumberPress("7")}>
  140. <Text style={{
  141. color: "#fff",
  142. fontSize: 50,
  143. textAlign: "center"
  144. }}>7</Text>
  145. </TouchableHighlight>
  146.  
  147. <TouchableHighlight style={styles.button} onPress={() => onNumberPress("8")}>
  148. <Text style={{
  149. color: "#fff",
  150. fontSize: 50,
  151. textAlign: "center"
  152. }}>8</Text>
  153. </TouchableHighlight>
  154.  
  155. <TouchableHighlight style={styles.button} onPress={() => onNumberPress("9")}>
  156. <Text style={{
  157. color: "#fff",
  158. fontSize: 50,
  159. textAlign: "center"
  160. }}>9</Text>
  161. </TouchableHighlight>
  162.  
  163. <TouchableHighlight style={styles.button} onPress={() => onOperatorPress("-")}>
  164. <Text style={{
  165. color: "#fff",
  166. fontSize: 50,
  167. textAlign: "center"
  168. }}>-</Text>
  169. </TouchableHighlight>
  170.  
  171. <TouchableHighlight style={styles.button} onPress={() => onNumberPress("4")}>
  172. <Text style={{
  173. color: "#fff",
  174. fontSize: 50,
  175. textAlign: "center"
  176. }}>4</Text>
  177. </TouchableHighlight>
  178.  
  179. <TouchableHighlight style={styles.button} onPress={() => onNumberPress("5")}>
  180. <Text style={{
  181. color: "#fff",
  182. fontSize: 50,
  183. textAlign: "center"
  184. }}>5</Text>
  185. </TouchableHighlight>
  186.  
  187. <TouchableHighlight style={styles.button} onPress={() => onNumberPress("6")}>
  188. <Text style={{
  189. color: "#fff",
  190. fontSize: 50,
  191. textAlign: "center"
  192. }}>6</Text>
  193. </TouchableHighlight>
  194.  
  195. <TouchableHighlight style={styles.button} onPress={() => onOperatorPress("+")}>
  196. <Text style={{
  197. color: "#fff",
  198. fontSize: 50,
  199. textAlign: "center"
  200. }}>+</Text>
  201. </TouchableHighlight>
  202.  
  203. <TouchableHighlight style={styles.button} onPress={() => onNumberPress("1")}>
  204. <Text style={{
  205. color: "#fff",
  206. fontSize: 50,
  207. textAlign: "center"
  208. }}>1</Text>
  209. </TouchableHighlight>
  210.  
  211. <TouchableHighlight style={styles.button} onPress={() => onNumberPress("2")}>
  212. <Text style={{
  213. color: "#fff",
  214. fontSize: 50,
  215. textAlign: "center"
  216. }}>2</Text>
  217. </TouchableHighlight>
  218.  
  219. <TouchableHighlight style={styles.button} onPress={() => onNumberPress("3")}>
  220. <Text style={{
  221. color: "#fff",
  222. fontSize: 50,
  223. textAlign: "center"
  224. }}>3</Text>
  225. </TouchableHighlight>
  226.  
  227. <TouchableHighlight style={styles.button} onPress={() => onEqualsPress()}>
  228. <Text style={{
  229. color: "#fff",
  230. fontSize: 50,
  231. textAlign: "center"
  232. }}>=</Text>
  233. </TouchableHighlight>
  234.  
  235. <TouchableHighlight style={styles.button} onPress={() => onNumberPress("0")}>
  236. <Text style={{
  237. color: "#fff",
  238. fontSize: 50,
  239. textAlign: "center"
  240. }}>0</Text>
  241. </TouchableHighlight>
  242.  
  243. <TouchableHighlight style={styles.button} onPress={() => onClearPress()}>
  244. <Text style={{
  245. color: "#fff",
  246. fontSize: 50,
  247. textAlign: "center"
  248. }}>C</Text>
  249. </TouchableHighlight>
  250.  
  251. </View>
  252.  
  253. </View>
  254. );
  255. }
  256.  
  257. // IN APP WE EXECUTE THE APP (ABOVE IS THE LOGIC AND REST OF SOURCE CODE)------------------------------------
  258.  
  259. export default function App() {
  260. console.log("------------------------------------------");
  261. return (
  262. <SafeAreaView style={styles.container}>
  263.  
  264. <Calculator></Calculator>
  265.  
  266. <StatusBar style="auto" />
  267. </SafeAreaView>
  268. );
  269. }
  270.  
  271. const styles = StyleSheet.create({
  272. container: {
  273. flex: 1,
  274. backgroundColor: '#fff',
  275. },
  276. button: {
  277. width: "25%",
  278. backgroundColor: "green",
  279. }
  280. });
  281.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement