用class 模拟链表实现堆栈
时间:2006-03-07 来源:manbuzhe0301
用class 模拟链表实现堆栈
--用PHP编程实现表达式的值。
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 ;
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 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]) 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
If you wan to try again, Please input the inorder expression :
__EOF__
?>
--用PHP编程实现表达式的值。
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 ;
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 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]) 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);
If you wan to try again, Please input the inorder expression :
__EOF__
?>
相关阅读 更多 +