zoukankan      html  css  js  c++  java
  • PHP计算工资

    在PHP中实现公式表达式四则运算大概有两种方法:

    1)使用系统函数eval

    <?php
    //使用系统函数eval
    $str = 'L*((k-J)-(C+k))/M';
    $param = array('L' => 0.5, 'k' => 2, 'J' => 1, 'C' => 6, 'M' => 4);
    
    $str2 = '';
    for($i = 0; $i < strlen($str); $i++) {
        $tmp = substr($str, $i, 1);
        if (array_key_exists($tmp, $param)) {
            $str2 .= $param[$tmp];
        } else {
            $str2 .= $tmp;
        }
    }
    eval("\$str = $str2;");
    printf('%5.2d', $str);
    /*End of php*/

    2)将表达式转换成逆波兰表达式进行计算。

    首先需要分配2个栈,一个作为临时存储运算符的栈S1(含一个结束符号),一个作为输入逆波兰 式的栈S2(空栈),S1栈可先放入优先级最低的运算符#,注意,中缀式应以此最低优先级的运算符结束。可指定其他字符,不一定非#不可。从中缀式的左端 开始取字符,逐序进行如下步骤:

    1)若取出的字符是操作数,则分析出完整的运算数,该操作数直接送入S2栈;若取出的是运算符,并且当前S1栈顶为(,则当前运算符直接入S1栈。

    2)若取出的字符是运算符,则将该运算符与S1栈栈顶元素比较,如果该运算符优先级大于S1栈栈顶运算符优先级,则将该运算符进S1栈,否者,将S1栈的栈顶运算符弹出,送入S2栈中,直至S1栈栈顶运算符低于(不包括等于)该运算符优先级,则将该运算符送入S1栈。

    3)若取出的字符是,则直接送入S1栈栈顶。

    4)若取出的字符是,则将距离S1栈栈顶最近的之间的运算符,逐个出栈,依次送入S2栈,此时抛弃

    5)重复上面的1~4步,直至处理完所有的输入字符

    6)若取出的字符是“#”,则将S1栈内所有运算符(不包括“#”),逐个出栈,依次送入S2栈。

    完成以上步骤,S2栈便为逆波兰式输出结果。不过S2应做一下逆序处理。便可以按照逆波兰式的计算方法计算了!

    <?php
    /**
     * math_rpn 
     *
     * 实现逆波兰式算法
     *   
     * @author   sparkHuang 260558820@qq.com
     * @version  RPN 1.0.0 
     * 
     */
    
    class math_rpn {
    	//初始的计算表达式
    	private $_expression = '';
    	//处理后的逆波兰表达式
    	private $_rpnexp = array();
    	
    	//模拟栈结构的数组
    	private $_stack  = array('#');
    	
    	//正则判断
    	//private $_reg    = '/^([A-Za-z0-9\(\)\+\-\*\/])*$/';
    	
    	//优先级
    	private $_priority = array('#' => 0, '(' => 10, '+' => 20, '-' => 20, '*' => 30, '/' => 30);
    	
    	//四则运算
    	private $_operator = array('(', '+', '-', '*', '/', ')');
    	
    	public function __construct($expression) {
    		$this->_init($expression);
    	}
    	
    	private function _init($expression) {
    		$this->_expression = $expression;
    	}
    	
    	public function exp2rpn() {
    		$len = strlen($this->_expression);
    		
    		for($i = 0; $i < $len; $i++) {
    			$char = substr($this->_expression, $i, 1);
    			
    			if ($char == '(') {
    				$this->_stack[] = $char;
    				continue;
    			} else if ( ! in_array($char, $this->_operator)) {
    				$this->_rpnexp[] = $char;
    				continue;
    			} else if ($char == ')') {
    				for($j = count($this->_stack); $j >= 0; $j--) {
    					$tmp = array_pop($this->_stack);
    					if ($tmp == "(") {
    						break;	
    					} else {
    						$this->_rpnexp[] = $tmp;
    					}
    				}
    				continue;
    			} else if ($this->_priority[$char] <= $this->_priority[end($this->_stack)]) {
    				$this->_rpnexp[] = array_pop($this->_stack);
    				$this->_stack[]  = $char;
    				continue;
    			} else {
    				$this->_stack[] = $char;
    				continue;
    			}
    		}
    		for($i = count($this->_stack); $i >= 0; $i--) {
    			if (end($this->_stack) == '#') break;
    			$this->_rpnexp[] = array_pop($this->_stack);	
    		}
    		return $this->_rpnexp;
    	}
    }
    
    $expression = "(A*(B+C)-E+F)*G";
    var_dump($expression);
    $mathrpn = new math_rpn($expression);
    var_dump($mathrpn->exp2rpn());
    
    /*End of php*/
    

      

  • 相关阅读:
    【转】CSR蓝牙驱动程序引起的Win7奇怪问题
    c# .net WebRequest 始终报域名无法解析
    sql server 安装时提示要重启
    https 不检验证书
    windows 日志,IIS应用程序池回收日志
    excel sum
    .net core 连接sql server 时提示Connection Timeout Expired
    python2.0_day20_bbs系统开发
    SVN常用命令与分支操作
    SVN使用教程之-分支/标记 合并 subeclipse
  • 原文地址:https://www.cnblogs.com/kelite/p/2923044.html
Copyright © 2011-2022 走看看