PHP implementation of simple arithmetic calculator function Example explained

Source: Internet
Author: User
This article mainly introduces the PHP implementation of the simple arithmetic calculator function, combined with an example of PHP based on the stack implementation of the expression arithmetic function, the need for friends can refer to the following

This paper describes the simple arithmetic calculator function implemented by PHP. Share to everyone for your reference, as follows:

PHP implements a simple arithmetic calculator (it does not support the precedence of parentheses). It's great to use the data structure of the stack to calculate the expression.

The structure of the stack can be used here, because the PHP array "natural" has the characteristics of the stack, where the array is used directly. Of course, you can use the stack structure to write, the same reason.

Predecessor (Polish scientist) computes the four expression with parentheses, using the inverse Polish algorithm (suffix expression). What a god!! In fact code is not difficult, difficult is the guidance of the algorithm, to understand the algorithm before coding.

<?php$num_arr = Array ();//Declare the number stack $op_arr = array ();//declare symbol stack $STR = "10+6*2-18/2-2";p reg_match_all ('/./', $str, $arr);//  The operation string is decomposed into a $arr array $str_arr = $arr [0]; $length = count ($str _arr); $pre _num = ";//start into stack for ($i =0; $i < $length; $i + +) {$val  = $str _arr[$i]; The number if (Is_numeric ($val)) {$pre _num. = $val;//The case of the next character may also be a number (multi-digit) if ($i +1>= $length | | isoper ($STR _arr[$i +1]      ) {//Next is operator or head, then plug the number into the digital stack Array_push ($num _arr, $pre _num);    $pre _num = "; }//symbol to determine priority, choose whether to stack} else if (Isoper ($val)) {if (count ($op _arr) >0) {//Judgment priority, as long as it is not greater than the priority of the top of the symbol stack, starts the calculation until the priority is greater than the top of the stack , the operator is then put into the stack while (end ($op _arr) && priority ($val) <= priority (end ($op _arr)) {Calc ($num _arr, $op      _arr);  }} array_push ($op _arr, $val); }}//echo ' <pre> ';//print_r ($num _arr);//print_r ($op _arr);//The remaining while in the calculation stack (count ($num _arr) >0) {Calc ($num _  Arr, $op _arr);    if (count ($num _arr) ==1) {$result = Array_pop ($num _arr);  Break }}echo $str, ' = ', $result;//calculate, get the numberStack of two numbers, the operator function for the top of the symbol stack, calc (& $num _arr, & $op _arr) {if (count ($num _arr) >0) {$num 1 = array_pop ($num _arr);    $num 2 = Array_pop ($num _arr);    $op = Array_pop ($op _arr);    if ($op = = ' * ') $re = $num 1* $num 2;    if ($op = = '/') $re = $num 2/$num 1;//Here Note the order, the stack is advanced and then out, so $num2 is the divisor if ($op = = ' + ') $re = $num 1 $num    if ($op = = '-') $re = $num 2-$num 1;  Array_push ($num _arr, $re);  }}//Gets the priority function order ($STR) {if ($str = = ' * ' | | $str = = '/') {return 1;  } else {return 0;  }}//determines whether the operator function Isoper ($oper) {$oper _array = array (' + ', '-', ' * ', '/');  if (In_array ($oper, $oper _array)) {return true; } return false;}

Operation Result:

10+6*2-18/2-2 = 11

PS: Here again for you to recommend several calculation tools for further reference:

Online unary function (equation) Solver calculation tool:
Http://tools.jb51.net/jisuanqi/equ_jisuanqi

Scientific Calculator Online Use _ Advanced calculator online calculation:
Http://tools.jb51.net/jisuanqi/jsqkexue

Online Calculator _ Standard calculator:
Http://tools.jb51.net/jisuanqi/jsq

Articles you may be interested in:

Explanation of how to implement the non-fixed number of parameters in Laravel routing

Laravel Framework Blade Template Beginner's Tutorial

PHP generated thumbnail quality poor solution code explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.