我有一个学校项目,我收到了这个信息:
for i=1 to m
if c_i is an operand: Transfer c_i to output.
if c_i is a left parentheses: Push c_i to tmp.
if c_i is a right parentheses: Pop elements from tmp and transfer
them to output until a left-parentheses
is met. Pop left-parentheses.
if c_i is an operator: Let the top tmp element be t. Pop and
transfer elements from tmp to output
until:
p(t) < p(c_i) or
t is a left-parentheses or
tmp is empty.
Push c_i to tmp.
Transfer the remaining elements in tmp to output.
我一直在做这些步骤,但我的输出只是som次。我猜我在操作员的if语句周围的地方想错了。我现在经常调试2天,而我无法找到解决方案。
如果有人想检查我的代码,我会很高兴。 operatorCheck函数用于解决这个问题:&#34;我们使用子程序p来指定运算符的优先级:
p(+) = 0,
p(−) = 0, p(∗) = 1, p(/) = 1
。这意味着加法和减法都有 与乘法和除法相比,优先级较低。&#34;
代码:http://pastebin.com/TA7UGiGc
谢谢!
答案 0 :(得分:0)
Marco,在您的代码中,第42行
<?php
$serverName = "server";
$uid = "user";
$pwd = "pass";
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd,
"Database"=>"database");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ){
echo "Unable to connect.</br>";
die( print_r( sqlsrv_errors(), true));
}
$fotoquery = "SELECT column_image FROM Table WHERE Column = 'xxxxxxxxxx'";
$stmt = sqlsrv_query( $conn, $fotoquery);
if( $stmt === false ){
echo "Error in executing query.</br>";
die( print_r( sqlsrv_errors(), true));
}
$dataImage = sqlsrv_fetch_array($stmt);
$varimg = base64_encode($dataImage[0]);
$data = base64_decode($varimg);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
?>
你必须改变&gt;与&lt;
在算法中,退出条件是
while (tmp.length() > 0 && !(operatorCheck(t) > operatorCheck(character)) && t != '(')
因为有一段时间你必须否定
tmp.length()==0 || p(t) < p(c_i) || t=='('
除此之外,Dante正确使用堆栈。如果您想避免自动装箱问题,可以为char
创建自己的原始堆栈类答案 1 :(得分:0)
您正在循环并删除tmp
中的第一个字母,而不会在while循环中获取要与之比较的下一个字母,因此您需要在while循环中获取一个新字符。
子字符串也从tmp
删除了错误的字符,它应该保留除第一个字母以外的所有内容以及用tmp.substring(1, tmp.length())
我修复了以下代码块:
else if (character == '+' || character == '-' || character == '*' || character == '/'){
while (true) {
char t = tmp.length() > 0 ? tmp.charAt(0): ' ';
if (operatorCheck(t) < operatorCheck(character) || t == '(' || tmp.length() < 0) {
break;
}
output += t;
tmp = tmp.substring(1, tmp.length());
}
tmp = character + tmp;
}