Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { StyleSheet, Text, View} from 'react-native';
  3. import Button from "./components/Button";
  4.  
  5. export default class App extends React.Component {
  6.   state={
  7.     operand:"0",
  8.     memory:"0",
  9.     display:"0",
  10.     operator:"",
  11.     newNumber:1
  12.   }
  13.  
  14.  
  15.    HandleButtonOperatorPressed = (value) => {
  16.    
  17.     operator=this.state.operator
  18.     this.setState({newNumber:1})
  19.     if(operator=="add"){
  20.       console.log(value);
  21.       let display=parseInt(this.state.display);
  22.       let memory=parseInt(this.state.memory);
  23.       let result=String(display+memory);
  24.       this.setState({display:result});
  25.  
  26.     }else if(operator=="del"){
  27.       console.log(value);
  28.       //do somet
  29.     }
  30.     this.setState({operator:value})
  31. }
  32.  
  33.  
  34.   HandleButtonOperandPressed = (value) =>{
  35.     if(this.state.newNumber){
  36.       this.setState({display:"",newNumber:0})
  37.     }
  38.     if(this.state.display=="0"){
  39.       if(value!="0")
  40.         this.setState({display:value})
  41.     }else{
  42.       if(this.state.display.length<17){
  43.         number=this.state.display+value;
  44.         this.setState({display:number})
  45.       }
  46.     }
  47.   }
  48.  
  49.     /*if(this.state.display==="0") numero="";
  50.     if(this.state.after!=0){
  51.         this.setState({after:0});
  52.         numero="";
  53.     }
  54.     if(this.state.display.length<17){
  55.       numero=numero+value;
  56.       this.setState({display:numero});
  57.     }*/
  58.  
  59.  
  60.   /*Add = ()=>{
  61.    
  62.         let Operand= parseInt(this.state.display);
  63.         let Memory=parseInt(this.state.memoria);
  64.         let PartialResult=Operand + Memory;
  65.         this.setState({display:String(PartialResult)})
  66.         this.setState({operando:Operand});
  67.         this.setState({memoria:String(PartialResult)});
  68.         this.setState({operatore:"add"});
  69.         this.setState({after:1});
  70.        
  71.   }
  72.  
  73.   Equal = () =>{
  74.     if(this.state.operatore==="add"){
  75.         let Operand;
  76.         if(this.state.after==0){
  77.             Operand= parseInt(this.state.display);
  78.             this.setState({after:1});
  79.         }
  80.         else Operand=this.state.operando;
  81.         let Memory=parseInt(this.state.memoria);
  82.         let PartialResult=Operand + Memory;
  83.         this.setState({display:String(PartialResult)});
  84.         this.setState({memoria:String(PartialResult)});
  85.     }else{
  86.         this.setState({display:this.state.memoria});
  87.     }
  88.   }
  89.  
  90.   Del =() =>{
  91.     if(this.state.operando.length>=1){
  92.       this.setState({operando:this.state.operando.substring(0,this.state.operando.length-1)});
  93.     }
  94.   }*/
  95.  
  96.   render() {
  97.     return (
  98.       <View style={styles.container}>
  99.       <View style={styles.display}>
  100.         <Text style={{textAlign:"right", fontSize:30}}>{
  101.             this.state.display
  102.         }</Text>
  103.       </View>
  104.         <View style={styles.keyboard}>
  105.             <View style={[styles.buttonrow]}>
  106.               <Button value='<-' onPress={() =>this.HandleButtonOperatorPressed("del")}/>
  107.             </View>
  108.             <View style={styles.buttonrow}>
  109.               <Button value='7' onPress={() => this.HandleButtonOperandPressed("7")}/>
  110.               <Button value='8' onPress={() => this.HandleButtonOperandPressed("8")}/>
  111.               <Button value='9' onPress={() => this.HandleButtonOperandPressed("9")}/>
  112.               <Button value='='onPress={() =>this.HandleButtonOperatorPressed("equal")}/>
  113.             </View>
  114.             <View style={styles.buttonrow}>
  115.               <Button value='4' onPress={() => this.HandleButtonOperandPressed("4")}/>
  116.               <Button value='5' onPress={() => this.HandleButtonOperandPressed("5")}/>
  117.               <Button value='6' onPress={() => this.HandleButtonOperandPressed("6")}/>
  118.               <Button value='+' onPress={() => this.HandleButtonOperatorPressed("add")}/>
  119.             </View>
  120.             <View style={styles.buttonrow}>
  121.               <Button value='1' onPress={() => this.HandleButtonOperandPressed("1")}/>
  122.               <Button value='2' onPress={() => this.HandleButtonOperandPressed("2")}/>
  123.               <Button value='3' onPress={() => this.HandleButtonOperandPressed("3")}/>
  124.               <Button value='0' onPress={() => this.HandleButtonOperandPressed("0")}/>
  125.             </View>
  126.         </View>
  127.       </View>
  128.     );
  129.   }
  130. }
  131.  
  132. const styles = StyleSheet.create({
  133.   container: {
  134.     flex: 1,
  135.     backgroundColor: '#fff',
  136.     alignItems: 'center',
  137.     justifyContent: 'space-around',
  138.   },
  139.   buttonrow: {
  140.     flexDirection:'row',
  141.     justifyContent:"space-between",
  142.   },
  143.   keyboard:{
  144.     height:180,
  145.     width:190,
  146.     justifyContent:"space-between",
  147.   },
  148.   display:{
  149.     height:50,
  150.     width:300,
  151.     backgroundColor:"rgb(220,220,220)",
  152.     borderRadius:5
  153.   }
  154. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement