最近一直在学习C/C++,可学的都是原理语法之类的,没有实战成绩甚是令人不爽。想用C/C++写个计算器一直是我的夙愿,刚敲键盘,就不知可否了,想来想去还是对计算器的算法不是很清楚。由于本人是学php出身,所以先使用php将计算器算法给实现了一下,以便更好的学习C/C++。这个简单的计算器采用的是逆波兰式来做的,仅支持加减乘除四种运算,纯粹个人练习记录一下,还望多多支持。
将算术表达式转换成逆波兰式/** * php简单实现算术表达式转换成逆波兰式,并求解。 * 仅支持加减乘除四种运算 * @author joe, joenali@163.com * @date 2013-01-17 * <pre> * require 'Calc.php'; * $calc = new Calc('(1+9)/2'); * echo $calc->getExpression(); * echo $calc->calculate(); * </pre> */html' target='_blank'>class Calc { protected $_stackOperator = array('@'); protected $_stackOut = array(); protected $_operator = array('@', '(', ')', '+', '-', '*', '/'); protected $_priority = array('@' => 0, '(' => 10, ')' => 10, '+' => 20, '-' => 20, '*' => 30, '/' => 30); public function __construct($expression) { $this->convert($expression); } /** * 解析字符串表达式 * 解析字符串表达式,将数字和运算符分离,用数组存储 * @param string $expression * @return array */ protected function expressionParase($expression) { $arr = str_split($expression); $data = $tmp = array(); do { $item = array_shift($arr); if (in_array($item, $this->_operator)) { if ($tmp) { array_push($data, implode('', $tmp)); $tmp = array(); } array_push($data, $item); } else { array_push($tmp, $item); } } while(count($arr)); array_push($data, implode('', $tmp)); return $data; } /** * 生成逆波兰式 * @param string $expression */ protected function convert($expression) { foreach ($this->expressionParase($expression) as $char) { if (preg_match("/^[0-9]+$/", $char)) { array_push($this->_stackOut, $char); } else if (in_array($char, $this->_operator)) { if ('(' == $char) { array_push($this->_stackOperator, $char); } else if (')' == $char) { while (count($this->_stackOperator) > 1) { $drop = array_pop($this->_stackOperator); if ('(' == $drop) { break; } else { array_push($this->_stackOut, $drop); } } } else { while (count($this->_stackOperator)) { $oTop = end($this->_stackOperator); if ($this->_priority[$char] > $this->_priority[$oTop]) { array_push($this->_stackOperator, $char); break; } else { $drop = array_pop($this->_stackOperator); array_push($this->_stackOut, $drop); } } } } } while (count($this->_stackOperator)) { $drop = array_pop($this->_stackOperator); if ('@' == $drop) { break; } else { array_push($this->_stackOut, $drop); } } } /** * 获取逆波兰式 * @return string */ public function getExpression() { return implode('', $this->_stackOut); } /** * 计算逆波兰式 * @return int */ public function calculate() { $stack = array(); foreach ($this->_stackOut as $char) { if (preg_match("/^[0-9]+$/", $char)) { array_push($stack, $char); } else if (in_array($char, $this->_operator)) { $b = array_pop($stack); $a = array_pop($stack); array_push($stack, $this->operator($a, $b, $char)); } } return end($stack); } protected function operator($a, $b, $o) { switch ($o) { case '+': return intval($a) + intval($b); break; case '+': return intval($a) + intval($b); break; case '-': return intval($a) - intval($b); break; case '*': return intval($a) * intval($b); break; case '/': return intval($a) / intval($b); break; } }}PHP编程
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
新闻热点
疑难解答