Advertisement
annukaka

action kalkulator

Jun 9th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.29 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. ACT.PHP
  5.  
  6. ini skrip action kalkulator dengan 1 input.
  7. */
  8.  
  9. $i = 0;
  10. $ask = null;
  11. $return = null;
  12.  
  13. if (isset($_GET['ask'])) {
  14.     $ask = $_GET['ask'];
  15.     echo $ask . " = ";
  16. }
  17.  
  18. function math($ask)
  19. {
  20.  
  21.     $operator = array(
  22.                       "*" => "(\-?[0-9\.]+\*\-?[0-9\.]+)",
  23.                       "/" => "(\-?[0-9\.]+\/\-?[0-9\.]+)",
  24.                       "+" => "(\-?[0-9\.]+\+\-?[0-9\.]+)",
  25.                       "-" => "(\-?[0-9\.]+\-\-?[0-9\.]+)"
  26.                  );
  27.  
  28.     foreach ($operator as $key => $pattern) {
  29.  
  30.         for ($i=1; $i > 0 ; $i++) {
  31.  
  32.             if (preg_match($pattern,$ask)) {
  33.                 # code...
  34.            
  35.                 preg_match($pattern,$ask,$result);
  36.                 $askx = preg_replace($pattern, "x", $ask);
  37.                 $asktmp = $result[0];
  38.  
  39.                 /* menyelesaikan operasi dalam kurung */
  40.                 preg_match("/^\-?[0-9\.]+/",$asktmp,$nilai1);
  41.                 preg_match("/[0-9\.]+$/",$asktmp,$nilai2);
  42.                 $returntmp = null;
  43.  
  44.                 if (preg_match("(\-?[0-9\.]+([^0-9\.]{2,2})[0-9\.]+)", $asktmp)) {
  45.                     $nilai2[0] = "-".$nilai2[0];
  46.                 }
  47.                
  48.                 if ($key == "*") {
  49.                     $returntmp = $nilai1[0] * $nilai2[0];
  50.                 }
  51.                 if ($key == "/") {
  52.                     $returntmp = $nilai1[0] / $nilai2[0];
  53.                 }
  54.                 if ($key == "+") {
  55.                     $returntmp = $nilai1[0] + $nilai2[0];
  56.                 }
  57.                 if ($key == "-") {
  58.                     $returntmp = $nilai1[0] - $nilai2[0];
  59.                 }
  60.  
  61.                  $ask = preg_replace("/(x)/", $returntmp, $askx);
  62.  
  63.            
  64.                
  65.             }
  66.             else{break;}
  67.             # code...
  68.         }
  69.     }
  70.     return $ask;
  71.  
  72.    
  73.  
  74.  
  75.    
  76. }
  77.  
  78. // function
  79. function prepare($ask)
  80. {
  81.  
  82.     #do ()
  83.     $i = 1;
  84.     $pattern_kurung = "/\(([^\(\)]+)\)/";
  85.     for ($i=1; $i > 0 ; $i++) {
  86.        
  87.         if (preg_match($pattern_kurung,$ask)) {
  88.             preg_match($pattern_kurung,$ask,$result);
  89.  
  90.            
  91.            
  92.  
  93.             $askx = preg_replace($pattern_kurung, "x", $ask);
  94.             $asktmp = $result[1];
  95.  
  96.             /* menyelesaikan operasi dalam kurung */
  97.             $returntmp = math($asktmp);
  98.            
  99.  
  100.             $ask = preg_replace("/(x)/", $returntmp, $askx);
  101.  
  102.            
  103.             // break;
  104.  
  105.  
  106.  
  107.         }
  108.         else{
  109.  
  110.             return math($ask);
  111.            
  112.             break;
  113.         }
  114.  
  115.  
  116.     }
  117.  
  118.  
  119.    
  120. }
  121.  
  122.  
  123.  
  124. // check math syntax
  125. $pattern = "/^(\(*[\+\-]?\(*[\+\-]?\d+(.\d+)?\)*[\+\-\*\/]?)*$/i";
  126. if (!preg_match($pattern, $ask)==true) {
  127.     $return = "math syntax is wrong!";
  128.    
  129. }
  130. else{
  131.     echo $return = prepare($ask);
  132.    
  133.  
  134.  
  135. }
  136. header("location:index.php?return=".urlencode($return)."&ask=".urlencode($ask));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement