Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. *
  5. * @format
  6. * @flow
  7. */
  8.  
  9. import React, {Component} from 'react';
  10. import {
  11. StyleSheet,
  12. Text,
  13. View,
  14. TouchableOpacity,
  15. Dimensions,
  16. } from 'react-native';
  17. import _ from 'lodash';
  18.  
  19. export default class App extends Component {
  20. constructor() {
  21. super();
  22.  
  23. const isPortrait = () => {
  24. const dim = Dimensions.get('screen');
  25. return dim.height >= dim.width;
  26. };
  27.  
  28. this.state = {
  29. resultText: ' ',
  30. done: false,
  31. operation: '',
  32. orientation: isPortrait() ? 'portrait' : 'landscape',
  33. };
  34.  
  35. Dimensions.addEventListener('change', () => {
  36. this.setState({
  37. orientation: isPortrait() ? 'portrait' : 'landscape',
  38. });
  39. });
  40. }
  41.  
  42. calcuateResult() {
  43. //=
  44. const array = this.state.resultText.split(' ');
  45. let [first, second] = array;
  46. second = second.substring(1);
  47. let res = 0;
  48. switch (this.state.operation) {
  49. case '+':
  50. res = parseFloat(first) + parseFloat(second);
  51. break;
  52. case '-':
  53. res = parseFloat(first) - parseFloat(second);
  54. break;
  55. case '/':
  56. res = parseFloat(first) / parseFloat(second);
  57. break;
  58. case '*':
  59. res = parseFloat(first) * parseFloat(second);
  60. break;
  61. case 'x^2':
  62. res = parseFloat(first);
  63. res = res * res;
  64. break;
  65. case 'x^3':
  66. res = parseFloat(first);
  67. res = res * res * res;
  68. break;
  69. case 'sqrt':
  70. res = Math.sqrt(parseFloat(first));
  71. break;
  72. case 'log10':
  73. res = Math.log10(parseFloat(first));
  74. break;
  75. case 'ln':
  76. res = Math.log(parseFloat(first));
  77. break;
  78. default:
  79. res = this.state.resultText;
  80. }
  81. this.setState({
  82. resultText: res.toFixed(4).toString(),
  83. operation: '',
  84. });
  85. }
  86.  
  87. buttonPressed(text) {
  88. if (text == '=' && this.state.resultText.includes(' ')) {
  89. return this.calcuateResult();
  90. } else if (text !== '=') {
  91. this.setState({
  92. resultText: this.state.resultText + text,
  93. });
  94. }
  95. }
  96.  
  97. handleOperation(text) {
  98. if (text == 'AC') {
  99. this.setState({
  100. resultText: '0',
  101. });
  102. } else if (this.state.operation == '') {
  103. this.setState({
  104. resultText: this.state.resultText + ' ' + text,
  105. operation: text,
  106. });
  107. }
  108. }
  109.  
  110. render() {
  111. let key = 0;
  112. let rows = [];
  113. let nums = [[1, 2, 3], [4, 5, 6], [7, 8, 9], ['.', 0, '=']];
  114. for (let i = 0; i < 4; i++) {
  115. let row = [];
  116. for (let j = 0; j < 3; j++) {
  117. row.push(
  118. <TouchableOpacity
  119. key={key}
  120. onPress={() => this.buttonPressed(nums[i][j])}
  121. style={styles.btn}>
  122. <Text style={styles.btnText}>{nums[i][j]}</Text>
  123. </TouchableOpacity>,
  124. );
  125. key++;
  126. }
  127. rows.push(
  128. <View key={key} style={styles.row}>
  129. {row}
  130. </View>,
  131. );
  132. key++;
  133. }
  134.  
  135. let operation = [];
  136. let ops = [];
  137.  
  138. if (this.state.orientation === 'portrait') ops = ['AC', '+', '-', '*', '/'];
  139. else ops = ['AC', '+', '-', '*', '/', 'ln', 'sqrt', 'log10', 'x^2', 'x^3'];
  140.  
  141. for (let i = 0; i < ops.length; i++) {
  142. operation.push(
  143. <TouchableOpacity
  144. key={key}
  145. onPress={() => this.handleOperation(ops[i])}
  146. style={styles.btn}>
  147. <Text style={[styles.btnText, styles.white]}>{ops[i]}</Text>
  148. </TouchableOpacity>,
  149. );
  150. key++;
  151. }
  152. if (this.state.orientation === 'portrait') {
  153. return (
  154. <View style={styles.container}>
  155. <View style={styles.result}>
  156. <Text style={styles.resultText}>{this.state.resultText}</Text>
  157. </View>
  158. <View style={styles.buttons}>
  159. <View style={styles.numbers}>{rows}</View>
  160.  
  161. <View style={styles.operations}>{operation}</View>
  162. </View>
  163. </View>
  164. );
  165. } else {
  166. return (
  167. <View style={styles.container}>
  168. <View style={styles.result}>
  169. <Text style={styles.resultText}>{this.state.resultText}</Text>
  170. </View>
  171. <View style={styles.buttons_land}>
  172. <View style={styles.numbers_land}>{rows}</View>
  173.  
  174. <View style={styles.operations_land}>{operation}</View>
  175. </View>
  176. </View>
  177. );
  178. }
  179. }
  180. }
  181.  
  182. const styles = StyleSheet.create({
  183. container: {
  184. flex: 1,
  185. },
  186. white: {
  187. color: 'white',
  188. },
  189. btn: {
  190. flex: 1,
  191. alignItems: 'center',
  192. alignSelf: 'stretch',
  193. justifyContent: 'center',
  194. },
  195. btnText: {
  196. fontSize: 30,
  197. },
  198. result: {
  199. flex: 2,
  200. backgroundColor: 'gray',
  201. justifyContent: 'center',
  202. alignItems: 'flex-end',
  203. },
  204. resultText: {
  205. fontSize: 30,
  206. color: 'white',
  207. },
  208. calculationText: {
  209. fontSize: 24,
  210. color: 'white',
  211. },
  212. buttons: {
  213. flex: 7,
  214. flexDirection: 'row',
  215. },
  216. row: {
  217. flexDirection: 'row',
  218. flex: 1,
  219. justifyContent: 'space-around',
  220. alignItems: 'center',
  221. },
  222. numbers: {
  223. flex: 3,
  224. backgroundColor: 'orange',
  225. },
  226. operations: {
  227. flex: 1,
  228. justifyContent: 'space-around',
  229. backgroundColor: 'black',
  230. alignItems: 'stretch',
  231. },
  232. buttons_land: {
  233. flex: 7,
  234. flexDirection: 'row',
  235. alignItems: 'stretch',
  236. },
  237. numbers_land: {
  238. flex: 1,
  239. backgroundColor: 'orange',
  240. alignItems: 'stretch',
  241. },
  242. operations_land: {
  243. flex: 1,
  244. justifyContent: 'space-around',
  245. backgroundColor: 'black',
  246. alignItems: 'center',
  247. flexWrap: 'wrap',
  248. },
  249. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement