PHP--编辑简单计算器

  1 <?php 
  2 session_start();
  3 $jieguo="";
  4 
  5 if(isset($_POST['button'])){
  6     $jieguo=$_POST['button'];
  7     $_SESSION['jg']=$_SESSION['jg'].$jieguo;
  8 }
  9 if(isset($_POST['button1'])){
 10     $jieguo=$_POST['button1'];
 11     $_SESSION['jg']=$_SESSION['jg'].$jieguo;
 12 }
 13 if(isset($_POST['button2'])){
 14     $jieguo=$_POST['button2'];
 15     $_SESSION['jg']=$_SESSION['jg'].$jieguo;
 16 }
 17 if(isset($_POST['button3'])){
 18     $jieguo=$_POST['button3'];
 19     $_SESSION['jg']=$_SESSION['jg'].$jieguo;
 20 }
 21 if(isset($_POST['button4'])){
 22     $jieguo=$_POST['button4'];
 23     $_SESSION['jg']=$_SESSION['jg'].$jieguo;
 24 }
 25 if(isset($_POST['button5'])){
 26     $jieguo=$_POST['button5'];
 27     $_SESSION['jg']=$_SESSION['jg'].$jieguo;
 28 }
 29 if(isset($_POST['button6'])){
 30     $jieguo=$_POST['button6'];
 31     $_SESSION['jg']=$_SESSION['jg'].$jieguo;
 32 }
 33 if(isset($_POST['button7'])){
 34     $jieguo=$_POST['button7'];
 35     $_SESSION['jg']=$_SESSION['jg'].$jieguo;
 36 }
 37 if(isset($_POST['button8'])){
 38     $jieguo=$_POST['button8'];
 39     $_SESSION['jg']=$_SESSION['jg'].$jieguo;
 40 }
 41 if(isset($_POST['button9'])){
 42     $jieguo=$_POST['button9'];
 43     $_SESSION['jg']=$_SESSION['jg'].$jieguo;
 44 }
 45 if(isset($_POST['button14'])){
 46     $jieguo=$_POST['button14'];
 47     $_SESSION['jg']=$_SESSION['jg'].$jieguo;
 48 }
 49 if(isset($_POST['button16'])){
 50     //$jieguo=$_POST['button'];
 51     $_SESSION['jg']="";
 52 }
 53 
 54 if(isset($_POST['button10'])){
 55     //$jieguo=$_POST['button'];
 56     $_SESSION['jg2']=$_SESSION['jg'];
 57     $_SESSION['jg']="";
 58     $_SESSION['ysf']="+";
 59 }
 60 if(isset($_POST['button11'])){
 61     //$jieguo=$_POST['button'];
 62     $_SESSION['jg2']=$_SESSION['jg'];
 63     $_SESSION['jg']="";
 64     $_SESSION['ysf']="-";
 65 }
 66 if(isset($_POST['button12'])){
 67     //$jieguo=$_POST['button'];
 68     $_SESSION['jg2']=$_SESSION['jg'];
 69     $_SESSION['jg']="";
 70     $_SESSION['ysf']="*";
 71 }
 72 if(isset($_POST['button13'])){
 73     //$jieguo=$_POST['button'];
 74     $_SESSION['jg2']=$_SESSION['jg'];
 75     $_SESSION['jg']="";
 76     $_SESSION['ysf']="/";
 77 }
 78 if(isset($_POST['button15'])){
 79     //$jieguo=$_POST['button'];
 80     $_SESSION['jg1']=$_SESSION['jg'];
 81     $_SESSION['jg']="";
 82     switch($_SESSION['ysf']){
 83         case "+":
 84           $_SESSION['jg']=strval($_SESSION['jg2'])+strval($_SESSION['jg1']);
 85           break;
 86         case "-":
 87           $_SESSION['jg']=strval($_SESSION['jg2'])-strval($_SESSION['jg1']);
 88           break;
 89         case "*":
 90           $_SESSION['jg']=strval($_SESSION['jg2'])*strval($_SESSION['jg1']);
 91           break;
 92         case "/":
 93           if($_SESSION['jg1']==0){
 94                echo "。。。。。。。。。。。。。。。。。被除数不能为零。。。。。。。。。。。。";
 95               }else{
 96                 $_SESSION['jg']=strval($_SESSION['jg2'])/strval($_SESSION['jg1']);
 97                  break;
 98           }
 99         
100         }
101 }
102 
103 ?>
104 <body>
105 <form id="form1" name="form1" method="post" action="jsq.php">
106 <table width="200" border="1">
107   <tr>
108     <td colspan="4"><label for="textfield"></label>
109     <input type="text" name="textfield" id="textfield" style="font-size:35px;" value="<?php echo $_SESSION['jg'];?>"/></td>
110   </tr>
111     <tr>
112     <td><input type="submit" name="button7" id="button7"  value="7" style="font-size: 35px;  50px;" />
113     </td>
114     <td><input type="submit" name="button8" id="button8"  value="8" style="font-size:35px;  50px;" /></td>
115     <td><input type="submit" name="button9" id="button9"  value="9" style="font-size:35px ; 50px;" /></td>
116     <td><input type="submit" name="button13" id="button13"  value="/" style="font-size:35px;  50px;" /></td>
117   </tr>
118   <tr>
119     <td> <input type="submit" name="button4" id="button4"  value="4" style="font-size: 35px;  50px;" /></td>
120     <td> <input type="submit" name="button5" id="button5"  value="5" style="font-size: 35px;  50px;" /></td>
121     <td> <input type="submit" name="button6" id="button6"  value="6" style="font-size:35px ; 50px;" /></td>
122     <td><input type="submit" name="button12" id="button12"  value="*" style="font-size:35px;  50px;" /></td>
123   </tr>
124   <tr>
125     <td><input type="submit" name="button1" id="button1"  value="1" style="font-size:35px;  50px;" /></td>
126     <td><input type="submit" name="button2" id="button2"  value="2" style="font-size:35px;  50px;" /></td>
127     <td><input type="submit" name="button3" id="button3" value="3" style="font-size:35px;  50px;" /></td>
128     <td><input type="submit" name="button10" id="button10"  value="+" style="font-size:35px;  50px;" /></td>
129   </tr>
130   <tr>
131     <td><input type="submit" name="button" id="button"  value="0" style="font-size:35px;  50px;" /></td>
132     <td><input type="submit" name="button14" id="button14"  value="." style="font-size:35px;  50px;" /></td>
133     <td><input type="submit" name="button11" id="button11"  value="-" style="font-size:35px;  50px;" /></td>
134     <td><input type="submit" name="button15" id="button15"  value="=" style="font-size:35px;  50px;" /></td>
135   </tr>
136   <tr>
137     <td colspan="4"><input type="submit" name="button16" id="button16"  value="清空" style="font-size:35px;  100%;" /></td>
138     </tr>
139 </table>
140  </form>
原文地址:https://www.cnblogs.com/hsm1012/p/14925173.html