سه نمونه ماشین‌حساب برای ری‌اکت نیتیو three examples for React Native calculator

import * as React from 'react'; import { Text, View } from 'react-native'; import InputButton from './components/InputButton'; import { Styles } from './components/Styles'; const inputButtons = [ ['C', '+-', '%', '()'], [1, 2, 3, '/','log'], [4, 5, 6, '*','Sin'], [7, 8, 9, '-','Cos'], [0, '.', '=', '+','<>'], ]; const InitialState = { previousInputValue: 0, inputValue: 0, selectedSymbol: null, }; export default class App extends React.Component { constructor(props) { super(props); this.state = { previousInputValue: 0, inputValue: 0, selectedSymbol: null, }; } render() { return ( <View style={Styles.rootContainer}> <View style={Styles.displayContainer}> <Text style={Styles.displayText}>{this.state.inputValue}</Text> </View> <View style={Styles.inputContainer}>{this.renderInputButtons()}</View> </View> ); } renderInputButtons() { let views = []; for (var r = 0; r < inputButtons.length; r++) { let row = inputButtons[r]; let inputRow = []; for (var i = 0; i < row.length; i++) { let input = row[i]; inputRow.push( <InputButton value={input} highlight={this.state.selectedSymbol === input} onPress={this.onInputButtonPressed.bind(this, input)} key={r + '-' + i} /> ); } views.push( <View style={Styles.inputRow} key={'row-' + r}> {inputRow} </View> ); } return views; } onInputButtonPressed(input) { switch (typeof input) { case 'number': return this.handleNumberInput(input); case 'string': return this.handleStringInput(input); } } handleStringInput(str) { switch (str) { case '/': case '*': case '+': case '-': this.setState({ selectedSymbol: str, previousInputValue: this.state.inputValue, inputValue: 0, }); break; case '=': var symbol = this.state.selectedSymbol, inputValue = this.state.inputValue, previousInputValue = this.state.previousInputValue; if (!symbol) { return; } this.setState({ previousInputValue: 0, inputValue: eval(previousInputValue + symbol + inputValue), selectedSymbol: null, }); break; case 'C': this.setState({ previousInputValue: 0, inputValue: 0, selectedSymbool: null, }); break; } } handleNumberInput(num) { let inputValue = this.state.inputValue * 10 + num; this.setState({ inputValue: inputValue, }); } } import * as React from 'react'; import { Text, View } from 'react-native'; import InputButton from './components/InputButton'; import { Styles } from './components/Styles'; const inputButtons = [ ['C', '+-', '%', '()'], [2, 2, 3, '/','log'], [4, 5, 6, '*','Sin'], [7, 8, 9, '-','Cos','fac'], [0, '.', '=', '+','<>'], ]; const InitialState = { previousInputValue: 0, inputValue: 0, selectedSymbol: null, }; export default class App extends React.Component { constructor(props) { super(props); this.state = { previousInputValue: 0, inputValue: 0, selectedSymbol: null, }; } render() { return ( <View style={Styles.rootContainer}> <View style={Styles.displayContainer}> <Text style={Styles.displayText}>{this.state.inputValue}</Text> </View> <View style={Styles.inputContainer}>{this.renderInputButtons()}</View> </View> ); } renderInputButtons() { let views = []; for (var r = 0; r < inputButtons.length; r++) { let row = inputButtons[r]; let inputRow = []; for (var i = 0; i < row.length; i++) { let input = row[i]; inputRow.push( <InputButton value={input} highlight={this.state.selectedSymbol === input} onPress={this.onInputButtonPressed.bind(this, input)} key={r + '-' + i} /> ); } views.push( <View style={Styles.inputRow} key={'row-' + r}> {inputRow} </View> ); } return views; } onInputButtonPressed(input) { switch (typeof input) { case 'number': return this.handleNumberInput(input); case 'string': return this.handleStringInput(input); } } handleStringInput(str) { switch (str) { case '/': case '*': case '+': case '-': this.setState({ selectedSymbol: str, previousInputValue: this.state.inputValue, inputValue: 0, }); break; case '=': var symbol = this.state.selectedSymbol, inputValue = this.state.inputValue, previousInputValue = this.state.previousInputValue; if (!symbol) { return; } this.setState({ previousInputValue: 0, inputValue: eval(previousInputValue + symbol + inputValue), selectedSymbol: null, }); break; case 'C': this.setState({ previousInputValue: 0, inputValue: 0, selectedSymbool: null, }); break; } } handleNumberInput(num) { let inputValue = this.state.inputValue * 10 + num; this.setState({ inputValue: inputValue, }); } } import * as React from 'react'; import { Text, View } from 'react-native'; import InputButton from './components/InputButton'; import { Styles } from './components/Styles'; const inputButtons = [ ['C', '!', 'sin', 'cos'], [1, 2, 3, '/'], [4, 5, 6, '*'], [7, 8, 9, '-'], [0, '.', '=', '+'], ]; const InitialState = { previousInputValue: 0, inputValue: 0, selectedSymbol: null, }; export default class App extends React.Component { constructor(props) { super(props); this.state = { previousInputValue: 0, inputValue: 0, selectedSymbol: null, }; } render() { return ( <View style={Styles.rootContainer}> <View style={Styles.displayContainer}> <Text style={Styles.displayText}>{this.state.inputValue}</Text> </View> <View style={Styles.inputContainer}>{this.renderInputButtons()}</View> </View> ); } renderInputButtons() { let views = []; for (var r = 0; r < inputButtons.length; r++) { let row = inputButtons[r]; let inputRow = []; for (var i = 0; i < row.length; i++) { let input = row[i]; inputRow.push( <InputButton value={input} highlight={this.state.selectedSymbol === input} onPress={this.onInputButtonPressed.bind(this, input)} key={r + '-' + i} /> ); } views.push( <View style={Styles.inputRow} key={'row-' + r}> {inputRow} </View> ); } return views; } onInputButtonPressed(input) { switch (typeof input) { case 'number': return this.handleNumberInput(input); case 'string': return this.handleStringInput(input); } } handleStringInput(str) { switch (str) { case '/': case '*': case '+': case '-': this.setState({ selectedSymbol: str, previousInputValue: this.state.inputValue, inputValue: 0, }); break; case '=': var symbol = this.state.selectedSymbol, inputValue = this.state.inputValue, previousInputValue = this.state.previousInputValue; if (!symbol) { return; } this.setState({ previousInputValue: 0, inputValue: eval(previousInputValue + symbol + inputValue), selectedSymbol: null, }); break; case 'C': this.setState({ previousInputValue: 0, inputValue: 0, selectedSymbool: null, }); break; case '!': var number = this.state.inputValue; var digitOne = 1; for(var i=1; i< number; i++) { digitOne *= i+1; } alert(digitOne); break; } } handleNumberInput(num) { let inputValue = this.state.inputValue * 10 + num; this.setState({ inputValue: inputValue, }); } }