如何使用PHP计算器执行两个以上数字的算术运算

时间:2019-03-25 20:17:58

标签: php logic logical-operators

我用PHP编写了一个简单的计算器。我可以在提供两个数字后通过单击“ =”来执行二进制操作。但是我的减法部分存在一个逻辑问题。下面是该程序的代码。我在这里附加任务

`

    <?php

    session_start();

    if($_SERVER['REQUEST_METHOD'] == "POST"){
        $textboxValue = $_POST['display-bar'];
        $buttonValue = $_POST['button'];


        //The Operator Array
        $operations = ['=', '+', '-', '*', '/', 'C', 'Clear'];

        //Except operators, adding everything to the display screen. i-e 
        Numbers 0-9
        foreach ($operations as $key => $value) {
            if($buttonValue != $value){
                $joinedValue = $textboxValue.$buttonValue;
            }
        }


        if($buttonValue == '+'){

            //temp code
            $_SESSION['stored-operand'] = $_SESSION['stored-operand'] + 
            $textboxValue;
            $_SESSION['operation'] = '+';
            $joinedValue = '';


            /*$_SESSION['stored-operand'] = $textboxValue;
            $_SESSION['operation'] = '+';
            $joinedValue = '';*/

        }

` 这就是所有错误的地方。在减法块中。

`

        if($buttonValue == '-'){

            //temp code
            if(empty($_SESSION['stored-operand'])){
                echo $_SESSION['stored-operand'];
            }

            $_SESSION['stored-operand'] = (-1 * $_SESSION['stored-operand']) 
                                          - $textboxValue;


            $_SESSION['operation'] = '-';
            $joinedValue = '';
        }

        if($buttonValue == '*'){
            $_SESSION['stored-operand'] = $textboxValue;
            $_SESSION['operation'] = '*';
            $joinedValue = '';

        }


        if($buttonValue == '/'){
            $_SESSION['stored-operand'] = $textboxValue;
            $_SESSION['operation'] = '/';
            $joinedValue = '';

        }


        if($buttonValue == 'C'){
            session_destroy();
            $textboxValue = '';
            $buttonValue = '';
            $joinedValue = '';
        }


        if($buttonValue == '='){            
            switch ($_SESSION['operation']) {
                case '+':
                    $joinedValue = $_SESSION['stored-operand'] + $textboxValue;
                    break;

                    case '*':
                        $joinedValue = $_SESSION['stored-operand'] * $textboxValue;
                        break;

                        case '-':
                        $joinedValue = $_SESSION['stored-operand'] - $textboxValue;
                        break;

                            case '/':
                            $joinedValue = $_SESSION['stored-operand'] / $textboxValue;
                            break;


                default:
                    echo "Failure";
                    break;
            }

        }
    }else{
        $joinedValue = '';
    }
?>

`

这是HTML部分:

`

    <?php require_once('calculate.php'); ?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">

        body, *{
            font-family: segoe ui;
        }

        input{
            width: 60px;
            height: 60px;
            font-size: 1.2rem;
            border-radius: 5px;
            border:0;
            box-shadow: 0 0 1px 0;
            outline: none;
        }

        input:hover{
            cursor: pointer;
        }

        .full-width{
            width: 100%;
        }

        .success-button{
            background: #2671ff;
            color: white;
            font-weight: bold;
        }

        .clearfix{
            height: 30px;
        }

    </style>
</head>
<body>

    <div class="clearfix"></div>

    <form action="" method="POST">
        <table cellpadding="5" cellspacing="5" align="center">
            <caption><h2>Calculator</h2></caption>
        <tr>
            <td colspan=4>
                <input type="text" name="display-bar" value="<?php echo $joinedValue ?>" readonly="" class="full-width">
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" name="button" value="7">
            </td>
            <td>
                <input type="submit" name="button" value="8">
            </td>
            <td>
                <input type="submit" name="button" value="9">
            </td>
            <td>
                <input type="submit" name="button" value="C">
            </td>
        </tr>   
        <tr>
            <td>
                <input type="submit" name="button" value="4">
            </td>
            <td>
                <input type="submit" name="button" value="5">
            </td>
            <td>
                <input type="submit" name="button" value="6">
            </td>
            <td>
                <input type="submit" name="button" value="/">
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" name="button" value="1">
            </td>
            <td>
                <input type="submit" name="button" value="2">
            </td>
            <td>
                <input type="submit" name="button" value="3">
            </td>
            <td>
                <input type="submit" name="button" value="*">
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" name="button" value="0">
            </td>
            <td>
                <input type="submit" name="button" value=".">
            </td>
            <td>
                <input type="submit" name="button" value="-">
            </td>
            <td>
                <input type="submit" name="button" value="+">
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" name="button" value="=" class="full-width success-button">
            </td>
            <td colspan="2">
                <input type="reset" name="button" value="Clear" class="full-width">
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

`

0 个答案:

没有答案