php查看表达式的值,用PHP编程计算中序表达式的值_php

过程: 用class 模拟链表实现堆栈 .:p

完整测试程序地址: http://test.hightman.net/stack/stack_05.php

在表单里可输入: (3+5)/2 之类的表达试,支持 ()+-/*

以下为全部代码

/* =================== Program Description ==================== */

/* Written by MingLian Mar. (hightman)                          */

/* ============================================================ */

error_reporting(E_ALL & ~E_NOTICE);

if (!defined("NULL"))

define("NULL", 0);

class s_node

http://www.gaodaima.com/46609.html用PHP编程计算中序表达式的值_php

{

var $data = NULL;

var $next = NULL;

}

function push(&$stack, $value)

{

$newnode = new s_node;

$newnode->data = $value;

$newnode->next = $stack;

$stack = $newnode;

}

function pop(&$stack, &$value)

{

if ($stack != NULL)

{

$value = $stack->data;

$stack = $stack->next;

}

else

$value = -1;

}

function is_operator($op)

{

return strchr("+-*/()", $op);

}

function privority($op)

{

if ($op == ')' || $op == '(')

return 1;

else if ($op == '+' || $op == '-')

return 2;

else if ($op == '*' || $op == '/')

return 3;

else

return 0;

}

function two_result($op, $n1, $n2)

{

switch ($op)

{

case '+' : return ($n2 + $n1);

case '-' : return ($n2 - $n1);

case '*' : return ($n2 * $n1);

case '/' : return ($n2 / $n1);

}

}

// main program

$expression = trim($_POST['expression']);

if (empty($expression))

{

print <<<__eof__>

Please input the inorder expression :

__EOF__;

exit();

}

$stack_op = NULL;

$stack_on = NULL;

$n1 = $n2 = 0;

$op = '';

$len = strlen($expression);

$tmp = '';

for ($i = 0; $i < $len; $i++)

{

if (is_operator($expression[$i]))

{

$tmp = trim($tmp);

if (!empty($tmp))

{

push($stack_on, $tmp);

$tmp = '';

}

if ($expression[$i] == '(' || empty($stack_op))

push($stack_op, $expression[$i]);

else if ($expression[$i] == ')')

{

while ($stack_op->data != '(')

{

pop($stack_on, $n1);

pop($stack_on, $n2);

pop($stack_op, $op);

push($stack_on, two_result($op, $n1, $n2));

}

pop($stack_op, $op); // pop the '('

}

else {

while (privority($expression[$i]) <= privority($stack_op->data))

{

pop($stack_on, $n1);

pop($stack_on, $n2);

pop($stack_op, $op);

push($stack_on, two_result($op, $n1, $n2));

}

push($stack_op, $expression[$i]);

}

}

else

$tmp .= $expression[$i];

}

$tmp = trim($tmp);

if (!empty($tmp))

{

push($stack_on, $tmp);

$tmp = '';

}

while (!empty($stack_op))

{

pop($stack_op, $op);

pop($stack_on, $n1);

pop($stack_on, $n2);

push($stack_on, two_result($op, $n1, $n2));

}

$result = 0;

pop($stack_on, $result);

print <<<__eof__>

The expression { $expression } result is '$result'

If you wan to try again, Please input the inorder expression :

__EOF__;

?>

欢迎大家阅读《用PHP编程计算中序表达式的值_php》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

e7ce419cf2d6ad34d01da2ceb8829eed.png

微信 赏一包辣条吧~

023a57327877fb4402bcc76911ec18ea.png

支付宝 赏一听可乐吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值