多分PHPのformからうまく値を反映できていないので、phpファイルの$denom = 2.0 * $this->a;の部分がdevision by zero というエラーが出ます。全然何をすれば良いか分からずもがいています。回答お願いします。
PHP
1<!DOCTYPE html> 2<html lang="en"> 3 <head> 4 <title> Answer </title> 5 <meta charset="utf-8"/> 6 <style> 7 </style> 8 </head> 9 10<body> 11 12<?php 13class Equation{ 14 private $a; 15 private $b; 16 private $c; 17 18 public function __construct($a, $b, $c){ 19 $this->a = (int)$a; 20 $this->b = (int)$b; 21 $this->c = (int)$c; 22 } 23 24 public function determination(){ 25 $root_part = sqrt($this->b * $this->b - 4.0 * $this->a * $this->c); 26 if ($root_part>0){ 27 return 2; 28 }elseif($root_part==0){ 29 return 1; 30 }else{ 31 return 0; 32 } 33 } 34 35 public function posans(){ 36 $root_part = sqrt($this->b * $this->b - 4.0 * $this->a * $this->c); 37 $denom = 2.0 * $this->a; 38 $pos = (-$this->b + $root_part) / $denom; 39 return $pos; 40 } 41 public function negans(){ 42 $root_part = sqrt($this->b * $this->b - 4.0 * $this->a * $this->c); 43 $denom = 2.0 * $this->a; 44 $neg = (-$this->b - $root_part) / $denom; 45 return $neg; 46 } 47 } 48 49 50 51function Equation(){ 52 $a = $_GET["a"]; 53 $b = $_GET["b"]; 54 $c = $_GET["c"]; 55 56 $denom1 = 2.0 * $a; 57 if (($b * $b - 4.0 * $a * $c)>=0){ 58 $root_part1 = round(sqrt($b * $b) - 4.0 * $a * $c,2); 59 }else{ 60 $root_part1 = round(sqrt(abs($b * $b)) - 4.0 * $a * $c,2); 61 } 62 63 $fomula = new Equation(a, b, c); 64 $ans1 = $fomula->posans(); 65 $ans2 = $fomula->negans(); 66 67 if ($fomula->determination() === 2){ 68 printf($ans1 , $ans2); 69 }elseif($fomula->determination() === 1){ 70 printf($ans1); 71 }elseif($fomula->determination() === 0){ 72 printf(-$b / $denom1, "+", $root_part1 / $denom1, "i"); 73 printf(-$b / $denom1, "-", $root_part1 / $denom1, "i"); 74 printf("Imaginary parts are rounded off to two decial place"); 75 } 76} 77 78Equation(); 79 80?> 81</body> 82</html> 83
HTML
1<!DOCTYPE html> 2<html lang="en"> 3 <head> 4 <title> Quadratic Equation Solver </title> 5 <meta charset="utf-8"/> 6 </head> 7<form action="quadratic.php" method="get"> 8 <body> 9 <h1> Quadratic Equation Solver </h1> 10 11 <p> 12 This program calculates quadratic equation. First I need some data. 13 </p> 14 15 <h2>Throw Parameters</h2> 16 17 <div class="in_block"> 18 19 <p> 20 Quadratic Equation<br /> 21 ax<sup>2</sup>+bx+c=0 22 </p> 23 24 25 <p><input type="text" name="a" /></p> 26 <p><input type="text" name="b" /></p> 27 <p><input type="text" name="c" /></p> 28 <p><input type="submit" value="calculates" /></p> 29 30</div> 31</form> 32 33 34 </body> 35</html> 36
Equation()の冒頭でvar_dump($_GET);した結果を教えてください。想定通りの情報がきていますか?また、Equationクラスをnewするときの引数が変数ではなく定数になっています。クラスと関数を別々に作った理由がよく分かりませんが、同名にするのはあまり良くありません。クラス内にまとめるか、別名にした方が良いでしょう。
回答2件
あなたの回答
tips
プレビュー