PHP计算器使用一种输入形式

时间:2018-04-10 09:24:59

标签: php

<?php
// current formula in input box
if (isset($_POST['txt'])) {
    $current_txt = $_POST['txt'];
}

if (isset($_POST['select1'])) {
    $message = "1";
}
if (isset($_POST['select2'])) {
    $message = "2";
}
if (isset($_POST['select3'])) {
    $message = "3";
}
if (isset($_POST['select4'])) {
    $message = "4";
}
if (isset($_POST['select5'])) {
    $message = "5";
}
if (isset($_POST['select6'])) {
    $message = "6";
}
if (isset($_POST['select7'])) {
    $message = "7";
}
if (isset($_POST['select8'])) {
    $message = "8";
}
if (isset($_POST['select9'])) {
    $message = "9";
}
if (isset($_POST['select0'])) {
    $message = "0";
}
if (isset($_POST['select+'])) {
    $message = "+";
}
if (isset($_POST['select-'])) {
    $message = "-";
}
if (isset($_POST['select/'])) {
    $message = "/";
}
if (isset($_POST['select*'])) {
    $message = "*";
}
if (isset($_POST['clear'])) {
    $message = "clear";
}
if (isset($_POST['calculate'])) {
    $message = "calc";
}

if ($message == "calc") {
    // Calculate using eval 
    $current_txt .= " = " . eval('return ' . $current_txt . ';');
} else {
    // Append input to formula
    $current_txt .= $message;
}

if ($message == "clear") {
    $current_txt = "";
}
?>

<div style="padding-left: 200px; margin-top: 100px">
    <form  method="post">
        Enter value:  <input type="text" name="txt" value="<?php
        echo $current_txt;
        ?>" >
        <div style="padding-left: 105px"><br>

            <input type="submit" name="select1" value="1">
            <input type="submit" name="select2" value="2">
            <input type="submit" name="select3" value="3">
            <input type="submit" name="select4" value="4"><br><br>
            <input type="submit" name="select5" value="5">
            <input type="submit" name="select6" value="6">
            <input type="submit" name="select7" value="7">
            <input type="submit" name="select8" value="8"><br><br>
            <input type="submit" name="select9" value="9">
            <input type="submit" name="select0" value="0">
            <input type="submit" name="select+" value="+">
            <input type="submit" name="select-" value="-"><br><br>
            <input type="submit" name="select/" value="/">
            <input type="submit" name="select*" value="*">
            <input type="submit" name="clear" value="clear"> <br><br>
            <input type="submit" name="calculate" value="calculate">

        </div>
    </form>
</div>

当我做任何操作时,它都有效。例如,如果我想添加两个数字,如5 + 6 = 11,但如果我想再添加一个数字5到评估的答案示例11 + 5 = 16那么它的显示错误,这意味着只有一次我可以做操作再次,如果我想对评估的答案显示错误进行操作,我正在尝试修复此问题,但我没有办法,所以任何人都可以让我知道如何解决这个问题。任何建议?

3 个答案:

答案 0 :(得分:0)

更新(我已经通知你的行)行:

 if ($message == "calc") {
    // Calculate using eval 
    // $current_txt .= " = " . eval('return ' . $current_txt . ';');
      $current_txt = eval('return ' . $current_txt . ';');
 } 

希望它能解决您的问题:

答案 1 :(得分:0)

if ($message == "calc") 
{
// Calculate using eval
//Add Below line for extact last expression calculation
$cal_arr=array_reverse(explode("=",$current_txt));
$current_txt .= " = " . eval('return ' . $cal_arr[0] . ';');   
}

答案 2 :(得分:0)

只需要添加两个更改

第一次使用结果

添加上一个数字
if ($message == "calc") {
  $result = eval('return ' . $current_txt . ';');
  $current_txt = $current_txt.'='.$result;

 }

第二次从带有爆炸功能的字符串中获取结果值

if (isset($_POST['txt'])) {
    $current_txt = $_POST['txt'];
    $current_txt = end(explode("=",$current_txt));
}