最近一直在學(xué)習(xí)C/C++,可學(xué)的都是原理語法之類的,沒有實(shí)戰(zhàn)成績(jī)甚是令人不爽。想用C/C++寫個(gè)計(jì)算器一直是我的夙愿,剛敲鍵盤,就不知可否了,想來想去還是對(duì)計(jì)算器的算法不是很清楚。由于本人是學(xué)php出身,所以先使用php將計(jì)算器算法給實(shí)現(xiàn)了一下,以便更好的學(xué)習(xí)C/C++。這個(gè)簡(jiǎn)單的計(jì)算器采用的是逆波蘭式來做的,僅支持加減乘除四種運(yùn)算,純粹個(gè)人練習(xí)記錄一下,還望多多支持。
將算術(shù)表達(dá)式轉(zhuǎn)換成逆波蘭式/** * php簡(jiǎn)單實(shí)現(xiàn)算術(shù)表達(dá)式轉(zhuǎn)換成逆波蘭式,并求解。 * 僅支持加減乘除四種運(yùn)算 * @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);    }    /**     * 解析字符串表達(dá)式     * 解析字符串表達(dá)式,將數(shù)字和運(yùn)算符分離,用數(shù)組存儲(chǔ)     * @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);    }    /**     * 計(jì)算逆波蘭式     * @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編程        鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選