PHP算法每日一练 -- 堆栈(计算器)

  1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2 <html xmlns="http://www.w3.org/1999/xhtml">
  3 <head>
  4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5 <title> 计算器 </title>
  6 <meta name="keywords" content="" />
  7 <meta name="description" content="" />
  8 </head>
  9 
 10 <body>
 11 <form>
 12 请输入一个表达式:<input type="text" name="exp" value="<?php echo $_GET['exp']; ?>">
 13 <input type="submit" value="计算">
 14 </br>
 15 </form>
 16 <?php
 17 $input  = $_GET['exp'];
 18 echo $input.'=';
 19 echo '<hr/>';
 20 $strStark  = new myStark();
 21 $numStark  = new myStark();
 22 for($i =0;$i<strlen($input);$i++){
 23     //逐行遍历
 24     if(!isOper($input[$i])){
 25         //数值入数栈
 26         if($i>0 &&  $isNum){
 27             $numStark->push($numStark->pop().$input[$i]);
 28         }else{
 29             $numStark->push($input[$i]);
 30         }
 31         $isNum = true; //标志为数
 32     }else{
 33         $isNum = false; //标志为非数
 34         while($strStark->isEmpty() && oper($strStark->getTop()) >= oper($input[$i])){
 35             $num1 = $numStark->pop();
 36             $num2 = $numStark->pop();
 37             $oper = $strStark->pop();
 38             $res = pay($num1,$oper,$num2);
 39             $numStark->push($res);
 40         }
 41         $strStark->push($input[$i]);
 42     }
 43 }
 44 while($strStark->isEmpty()){
 45     $num1 = $numStark->pop();
 46     $num2 = $numStark->pop();
 47     $oper = $strStark->pop();
 48     $res = pay($num1,$oper,$num2);
 49     $numStark->push($res);
 50 }
 51 echo $numStark->stark[$numStark->top];
 52 ?>
 53 </body>
 54 </html>
 55 
 56 <?php
 57 function isOper($str){
 58     if(in_array($str,array('+','-','*','/','(',')','[',']','{','}'))){
 59         return true;
 60     }else{
 61         return false;
 62     }
 63 }
 64 function oper($str){
 65     switch($str){
 66         case '+':case '-':
 67             $oper = 1;
 68             break;
 69         case '*':case '/':
 70             $oper =  2;
 71             break;
 72     }
 73     return $oper;
 74 }
 75 function pay($x,$str,$y){
 76     switch($str){
 77         case '+':$return = $x+$y;break;
 78         case '-':$return = $y-$x;break;
 79         case '*':$return = $x*$y;break;
 80         case '/':$return = $y/$x;break;
 81     }
 82     return $return;
 83 }
 84 class myStark{
 85     public $top = -1;
 86     public $maxStark = 5;
 87     public $stark = array();
 88     public function isEmpty(){
 89         if($this->top == -1){
 90             return false;
 91         }else{
 92             return true;
 93         }
 94     }
 95     /*栈顶*/
 96     public function getTop(){
 97         return $this->stark[$this->top];
 98     }
 99     /*入栈*/
100     public function push($val){
101         if($this->top > $this->maxStark - 1){
102             return false;
103         }
104         $this->top ++;
105         $this->stark[$this->top] = $val;
106     }
107     /*出栈*/
108     public function pop(){
109         if($this->top <= -1){
110             return false;
111         }
112         $return = $this->stark[$this->top];
113         $this->top -- ;
114         return $return;
115     }
116     /*打印*/
117     public function show(){
118         for($i = $this->top;$i>-1;$i--){
119             echo 'stack['.$i.']='.$this->stark[$i].'<br />';
120         }
121     }
122 }
123 
124 ?>

 

源代码下载:Calculator_1205.zip

 

 

posted @ 2012-12-05 00:04  linzj  阅读(1233)  评论(0编辑  收藏  举报