Heim  >  Artikel  >  Backend-Entwicklung  >  用PHP实现的四则运算表达式计算_PHP教程

用PHP实现的四则运算表达式计算_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:44:50887Durchsuche

 

题目要求:有一个四则运算的字符串表达式,编写一个函数,计算四则运算的结果

 

 

 

PHP实现:

 

 1

 2

 3 /**

 4  * 计算四则运算表达式

 5  */

 6

 7 error_reporting(E_ALL);

 8

 9 $exp = '(1+2*(3+5)/4)*(3+(5-4)*2)';

10 $arr_exp = array();

11

12 for($i=0;$i

13     $arr_exp[] = $exp[$i];

14 }

15 $result = calcexp( array_reverse($arr_exp) );

16 echo $exp . '=' . $result;

17

18 function calcexp( $exp ){

19     $arr_n = array();

20     $arr_op = array();

21    

22     while( ($s = array_pop( $exp )) != '' ){

23         if( $s == '(' ){

24             $temp = array(); $quote = 1; $endquote = 0;

25             while( ($t = array_pop($exp)) != '' ){

26                 if( $t == '(' ){

27                     $quote++;

28                 }

29                 if( $t == ')' ){

30                     $endquote++;

31                     if( $quote == $endquote ){

32                         break;

33                     }

34                 }

35                 array_push($temp, $t);

36             }

37             $temp = array_reverse($temp);

38             array_push($arr_n, calcexp($temp) );

39         }else if( $s == '*' || $s == '/' ){

40             $n2 = array_pop($exp);

41             if( $n2 == '(' ){

42                 $temp = array(); $quote = 1; $endquote = 0;

43                 while( ($t = array_pop($exp)) != '' ){

44                     if( $t == '(' ){

45                         $quote++;

46                     }

47                     if( $t == ')' ){

48                         $endquote++;

49                         if( $quote == $endquote )

50                             break;

51                     }

52                     array_push($temp, $t);

53                 }

54                 $temp = array_reverse($temp);

55                 $n2 = calcexp($temp);

56             }

57            

58             $op = $s;

59             $n1 = array_pop($arr_n);

60            

61             $result = operation($n1, $op, $n2);

62             array_push($arr_n, $result);

63         }elseif( $s == '+' || $s == '-' ){

64             array_push($arr_op, $s);

65         }else{

66             array_push($arr_n, $s);

67         }

68     }

69    

70     $n2 = array_pop($arr_n);

71     while( ($op = array_pop($arr_op)) != '' ){

72         $n1 = array_pop($arr_n);

73         $n2 = operation($n1, $op, $n2);

74     }

75    

76     return $n2;

77 }

78

79 function operation( $n1, $op, $n2 ){

80     switch ($op) {

81         case '+':

82             return intval($n1) + intval($n2);

83             break;

84         case '-':

85             return intval($n1) - intval($n2);

86             break;

87         case '*':

88             return intval($n1) * intval($n2);

89             break;

90         case '/':

91             return intval($n1) / intval($n2);

92             break;

93     }

94 }

 

 

 

这个实现方式中使用了两个堆栈,一个用来存储数字,一个用来存储运算符,遇到括号以后就递归进入括号内运算,实现方式有点笨拙,后面补充一下“逆波兰表达式”的算法实现。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478722.htmlTechArticle题目要求:有一个四则运算的字符串表达式,编写一个函数,计算四则运算的结果 PHP实现: 1 ?php 2 3 /** 4 * 计算四则运算表达式 5 */ 6 7 er...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn