复习:简单计算器
本课属于 Coddy PHP 学习旅程的“面向对象编程”单元。第 9 课,共 91 课。
挑战
中等在 calculator.php 中创建一个完整的 Calculator 类,展示你学过的所有面向对象编程概念。driver.php 文件会导入并测试你的类。
你的 Calculator 类应包含:
- 属性:一个用于存储累计结果的
public $result属性 - 构造函数(
__construct):将$result初始化为0,并打印"Calculator ready" - 析构函数(
__destruct):打印"Calculator shutting down" - 方法:每个方法都使用
$this更新并返回结果:add($number):将该数字加到结果上subtract($number):从结果中减去该数字multiply($number):将结果乘以该数字divide($number):使用整数除法(intdiv)将结果除以该数字。如果该数字为0,则打印"Error: Division by zero"并返回未改变的结果clear():将结果重置为0getResult():返回当前结果
所有算术方法都必须在运算后返回新结果。
用法示例:
require_once 'calculator.php';
$calc = new Calculator(); // prints: Calculator ready
$calc->add(5); // result becomes 5
$calc->multiply(3); // result becomes 15
$calc->subtract(2); // result becomes 13
$calc->divide(0); // prints: Error: Division by zero (result stays 13)
$calc->divide(2); // result becomes 6 (integer division)
$calc->clear(); // result becomes 0
// When script ends: prints Calculator shutting down亲手试试
<?php
require_once 'calculator.php';
$testCase = trim(fgets(STDIN));
if ($testCase == "constructor_test") {
$calc = new Calculator();
echo "Initial result: " . $calc->getResult() . "\n";
echo "Calculator created successfully\n";
} elseif ($testCase == "addition_test") {
$calc = new Calculator();
$result1 = $calc->add(10);
echo "After adding 10: " . $result1 . "\n";
$result2 = $calc->add(5);
echo "After adding 5: " . $result2 . "\n";
echo "Final result: " . $calc->getResult() . "\n";
} elseif ($testCase == "subtraction_test") {
$calc = new Calculator();
$calc->add(20);
$result1 = $calc->subtract(8);
echo "After subtracting 8 from 20: " . $result1 . "\n";
$result2 = $calc->subtract(2);
echo "After subtracting 2: " . $result2 . "\n";
echo "Final result: " . $calc->getResult() . "\n";
} elseif ($testCase == "multiplication_test") {
$calc = new Calculator();
$calc->add(5);
$result1 = $calc->multiply(4);
echo "After multiplying by 4: " . $result1 . "\n";
$result2 = $calc->multiply(2);
echo "After multiplying by 2: " . $result2 . "\n";
echo "Final result: " . $calc->getResult() . "\n";
} elseif ($testCase == "division_test") {
$calc = new Calculator();
$calc->add(100);
$result1 = $calc->divide(4);
echo "After dividing by 4: " . $result1 . "\n";
$result2 = $calc->divide(5);
echo "After dividing by 5: " . $result2 . "\n";
echo "Final result: " . $calc->getResult() . "\n";
} elseif ($testCase == "division_by_zero_test") {
$calc = new Calculator();
$calc->add(50);
echo "Initial value: " . $calc->getResult() . "\n";
$result = $calc->divide(0);
echo "Result after division by zero: " . $result . "\n";
echo "Value unchanged: " . $calc->getResult() . "\n";
} elseif ($testCase == "clear_test") {
$calc = new Calculator();
$calc->add(25);
$calc->multiply(3);
echo "Before clear: " . $calc->getResult() . "\n";
$result = $calc->clear();
echo "After clear: " . $result . "\n";
echo "Current result: " . $calc->getResult() . "\n";
} elseif ($testCase == "comprehensive_test") {
$calc = new Calculator();
$calc->add(10);
$calc->multiply(3);
$calc->subtract(5);
$calc->divide(5);
echo "After sequence of operations: " . $calc->getResult() . "\n";
$calc->divide(0);
echo "After division by zero attempt: " . $calc->getResult() . "\n";
$calc->clear();
$calc->add(100);
echo "After clear and add 100: " . $calc->getResult() . "\n";
} else {
echo "Unknown test case\n";
}
面向对象编程中的所有课
自己练习: PHP 在线编译器