gpt4 book ai didi

php - 为什么我的 PHP 计算器不工作? (字符串函数..)

转载 作者:可可西里 更新时间:2023-11-01 01:05:36 25 4
gpt4 key购买 nike

这个计算器应该在网页文本框中输入带空格的输入(例如“2 + 2”),然后回显上面的答案。目前,输出要么是“0”,要么似乎会从输入中截取一个随机数。

我 95% 确定问题出在 substr 函数中——它们没有被赋予正确的值。这是我的猜测,因为输出行为。为了演示,

1 + 1 = 02 + 2 = 0...9 + 9 = 010 + 10 将 = 120 + 20 将 = 2

注意:从第 16 行开始的 $firstNumber、$secondNumber 和 $operator 初始声明值是任意的。

以下是 PHP 手册中的 substr( , ) 示例:

echo substr('abcdef', 1);     // bcdef   <br>
echo substr('abcdef', 1, 3); // bcd<br>
echo substr('abcdef', 0, 4); // abcd<br>
echo substr('abcdef', 0, 8); // abcdef<br>
echo substr('abcdef', -1, 1); // f<br>

.

//'if' is executed when submit is pressed
if (isset($_POST['test']))
{
$input = $_POST['test'];
$answer = calculate($input);
echo $answer;
}

//does string processing and number calculation
function calculate($input)
{
//declarations (random)
$firstNumber = 20;
$secondNumber = 30;
$operator = "+";
$answer = 7;

//string processing.
for($i = 0; $i <= strlen($input); $i++)
{
//if current position of the string scan is an operator,
if( trim(substr($input, $i, $i + 1), " ") == "+" ||trim(substr($input, $i, $i + 1), " ") == "-"
||trim(substr($input, $i, $i + 1), " ") == "*" ||trim(substr($input, $i, $i + 1), " ") == "/")
{
//then

//$operator = current position TO current position + 1
$operator = substr($input, $i, $i + 1);
//trim $operator
$operator = trim($operator, " ");

//$firstNumber = 0 TO current position - 1
$firstNumber = substr($input, 0, $i - 1);
//trim $operator
$firstNumber = trim($firstNumber, " ");

//$secondNumber = current position + 1 TO end of string
$secondNumber = substr($input, $i + 1, strlen($input));
//trim $operator
$secondNumber = trim($secondNumber, " ");
}

}

//if operator is ... then do that operation.
//example: if "+", then add $firstNumber and $secondNumber

if($operator == "+")
{
$answer = $firstNumber + $secondNumber;
}
else if($operator == "-")
{
$answer = $firstNumber - $secondNumber;
}
else if($operator == "*")
{
$answer = $firstNumber * $secondNumber;
}
else if($operator == "/")
{
$answer = $firstNumber / $secondNumber;
}

//return the calculated answer for echo
return $answer;
}

?>

<form action="test.php" method="POST">

<textarea name="test" rows="3" cols="30"></textarea>
<input type="submit" value="calculate!">

</form>

最佳答案

您的问题是您对 substr 的使用。

substr 接受一个字符串、一个起始位置和一个长度。这是你滥用的长度。在平移输入字符串时,您正在增加要查找的子字符串的长度。由于运算符的长度为 1,因此您应该使用

    //if current position of the string scan is an operator,
if( trim(substr($input, $i, 1), " ") == "+" || trim(substr($input, $i, 1), " ") == "-"
||trim(substr($input, $i, 1), " ") == "*" || trim(substr($input, $i, 1), " ") == "/")
{
....

您还需要在此处更新您的代码:

//$operator = current position TO current position + 1
$operator = substr($input, $i, 1);
//trim $operator
$operator = trim($operator, " ");

你可以做一些简单的事情

  potentialOperator = trim(substr($input, $i, 1), " ")

并将其与您支持的运营商进行比较。这将为您节省多次不必要的调用 trimsubstr这也意味着您只需识别一次运算符(operator)。

您可能还想查看带有 PREG_SPLIT_DELIM_CAPTUREpreg_split 来解析输入字符串,而不是逐字符扫描。

关于php - 为什么我的 PHP 计算器不工作? (字符串函数..),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11764486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com