Menu
Coddy logo textTech

복습 - 간단한 계산기

Coddy PHP 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 91개 중 9번째.

challenge icon

챌린지

중급

Calculator 클래스를 calculator.php에 완성하여 지금까지 배운 모든 OOP 개념을 보여 주세요. driver.php 파일이 여러분의 class를 가져와 테스트합니다.

여러분의 Calculator class에는 다음이 포함되어야 합니다.

  1. Property: 실행 중인 합계를 저장할 public $result property
  2. Constructor (__construct): $result0으로 초기화하고 "Calculator ready"를 print
  3. Destructor (__destruct): "Calculator shutting down"을 print
  4. 각각 $this를 사용하여 result를 업데이트하고 반환하는 Methods:
    • add($number): number를 result에 더함
    • subtract($number): result에서 number를 뺌
    • multiply($number): result에 number를 곱함
    • divide($number): integer division을 사용하여 result를 number로 나눔(intdiv). number가 0이면 "Error: Division by zero"를 print하고 unchanged result를 반환
    • clear(): result를 0으로 재설정
    • getResult(): current result를 반환

모든 arithmetic methods는 연산 후의 새 result를 반환해야 합니다.

사용 예:

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 컴파일러