Advertisement
Felanpro

React Native (Calculator Version 4)

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