public $num2; public $operator; public $result; function __construct($num1 = 0, $num2 = 0, $operator = '+') { $this->num1 = $num1; $this->num2 = $num2; $this->operator = $operator; } function calculate() { switch ($this->operator) { case '+': $this->result = $this->num1 + $this->num2; break; case '-': $this->result = $this->num1 - $this->num2; break; case '*': $this->result = $this->num1 * $this->num2; break; case '/': $this->result = $this->num1 / $this->num2; break; } return $this->result; } } $way = new count($_GET['firstNum'], $_GET['secondNum'], $_GET['operator']); $result = $way->calculate(); echo '<h4>计算结果为:' . $result . '</h4>';
case '+': $this->result1 = $this->num1 + $this->num2; break; case '-': $this->result1 = $this->num1 - $this->num2; break; } return $this->result1; } function calculate2() { switch ($this->operator) { case '+': $this->result2 = $this->num11 + $this->num22; break; case '-': $this->result2 = $this->num11 - $this->num22; break; } return $this->result2; } } $way = new count($_GET['firstNum'], $_GET['firstNum1'], $_GET['secondNum'], $_GET['secondNum2'], $_GET['operator']); $result1 = $way->calculate1(); $result2 = $way->calculate2(); echo '<h4>计算结果为:' . $result1; if ($result2 < 0) { echo $result2 . 'i</h4>'; } else { echo '+' . $result2 . 'i</h4>'; }