当前位置: 首页>>代码示例>>PHP>>正文


PHP Calculator::calc方法代码示例

本文整理汇总了PHP中Calculator::calc方法的典型用法代码示例。如果您正苦于以下问题:PHP Calculator::calc方法的具体用法?PHP Calculator::calc怎么用?PHP Calculator::calc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Calculator的用法示例。


在下文中一共展示了Calculator::calc方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: calc

                    break;
            }
            if ($need_push) {
                array_push($this->stack, $r);
            }
        }
    }
    function calc($expression)
    {
        $this->stack = [];
        $this->parser->parse($expression, [$this, '_calc']);
        return $this->stack[0];
    }
    function tree($expression)
    {
        $this->parser->printTree($expression);
    }
}
/////////////////////////////////////////////////////////////
if (realpath($_SERVER['SCRIPT_FILENAME']) == __FILE__) {
    $calc = new Calculator();
    while ($exp = fgets(STDIN)) {
        try {
            $result = $calc->calc($exp);
            echo "{$result}\n";
        } catch (Exception $e) {
            echo $e->getMessage(), "\n";
        }
    }
    exit;
}
开发者ID:nobody1986,项目名称:phpcc,代码行数:31,代码来源:calc.php

示例2: testCalc

 function testCalc()
 {
     $calc = new Calculator();
     $result = $calc->calc('123');
     $this->assertEquals(123, $result);
     $result = $calc->calc('100 - (35 - 4)');
     $this->assertEquals(100 - (35 - 4), $result);
     $result = $calc->calc('2 * (3 + 4)');
     $this->assertEquals(2 * (3 + 4), $result);
     $result = $calc->calc('9 / 3 * 2');
     $this->assertEquals(9 / 3 * 2, $result);
     $result = $calc->calc('(4+1) * (6 - 1)');
     $this->assertEquals((4 + 1) * (6 - 1), $result);
     $result = $calc->calc('-(11+29)');
     $this->assertEquals(-(11 + 29), $result);
     $result = $calc->calc('4 - -3 * 4');
     $this->assertEquals(4 - -3 * 4, $result);
     $result = $calc->calc('-1 * -1');
     $this->assertEquals(-1 * -1, $result);
     $result = $calc->calc('2.5 * 8');
     $this->assertEquals(2.5 * 8, $result);
     $result = $calc->calc('cos pi/2');
     $this->assertEquals(cos(M_PI) / 2, $result);
     $result = $calc->calc('4 pi-1');
     $this->assertEquals(4 * M_PI - 1, $result);
     $result = $calc->calc('2cos -pi');
     $this->assertEquals(2 * cos(-M_PI), $result);
 }
开发者ID:nobody1986,项目名称:phpcc,代码行数:28,代码来源:calc_test.php

示例3: switch

        switch ($this->operator) {
            case "+":
                return $this->num1 + $this->num2;
                break;
            case "-":
                return $this->num1 - $this->num2;
                break;
            case "*":
                return $this->num1 * $this->num2;
                break;
            case "/":
                if ($this->num2 == 0) {
                    echo "<script>alert('除数不能为0!');</script>";
                    return "除数不能为0!";
                } else {
                    return $this->num1 / $this->num2;
                }
                break;
            default:
                break;
        }
    }
}
if (isset($_POST["btn"])) {
    if (isset($_POST["op"])) {
        $calculator1 = new Calculator($_POST["num1"], $_POST["num2"], $_POST["op"]);
        echo "计算结果:" . $calculator1->calc();
    } else {
        echo "<script>alert('请选择操作!');</script>";
    }
}
开发者ID:XINCGer,项目名称:phpWebTraining,代码行数:31,代码来源:file_7_2.php


注:本文中的Calculator::calc方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。