2017-01-12 1 views
0

a, b, c를 폼으로 주어진 간단한 다항식 (x^2 + b x + c)을 해결해야하는 실험실 프로젝트가 있습니다. 이 코드를 구현했지만 html 스크립트도 작동하지 않는 것 같습니다. 거의 하루 동안 실수를 찾으려고했기 때문에 당신의 도움이 필요합니다. 그러나 나는 잘못된 것을 발견 할 수 없습니다.PHP의 3 항의 방정식 솔버

<!DOCTYPE html> 
<html> 
    <meta charset="utf-8"/> 
    <head> 
     <title> 
      Exercise 2 
     </title> 
    </head> 
    <body> 
    <? if ((!isset($_POST['submit']))) 
    {?> 
     <h2>Please fill the trinomial coefficients a*x^2+b*x+c=0</h2> <br> 
     <form method="post"> 
     a= <input type="text" name="a" /> <br> 
     b= <input type="text" name="b" /> <br> 
     c= <input type="text" name="c" /> <br> 
     <input type="submit" value="Solve the equation" /> 
     </form> 
     <?php 
    } 
    else 
    { 
     $a=$_POST["a"]; 
     $b=$_POST["b"]; 
     $c=$_POST["c"]; 
     if ($a!=0) 
     { 
      $d=pow($b,2)-(4*a*c); 
      if ($d>=0) 
      { 
       $d=sqrt($d); 
       x1=(-$b-$d)/(2*$a); 
       x2=(-$b+$d)/(2*$a); 
       if (x1=x2) 
       { 
        echo ("1 solution: ". $x1); 
       } 
       else 
       { 
        echo ("Solutions x1 and x2 " .$x1. ", ". $x2); 
       } 
      } 
      else 
      { 
       $d=sqrt(-$d); 
       x1=-b/(2*$a); 
       x2=-$d; 
       x3=$d; 
       echo ("The trinomial has two complex solutions x1 and x2, which are: x1=". x1. " + ". x2. "*i and x2=". x1. " + ". x3. "*i"); 
      } 
     } 
     else 
     { 
      echo ("This is not a trinomial"); 
     } 
    } 
    ?> 
    </body> 
</html> 

답변

0

이 작동 :

<!DOCTYPE html> 
<html> 
    <meta charset="utf-8"/> 
    <head> 
     <title> 
      Exercise 2 
     </title> 
    </head> 
    <body> 
    <?php if ((!isset($_POST['submit']))) 
    {?> 
     <h2>Please fill the trinomial coefficients a*x^2+b*x+c=0</h2> <br> 
     <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
     a= <input type="text" name="a" /> <br> 
     b= <input type="text" name="b" /> <br> 
     c= <input type="text" name="c" /> <br> 
     <input type="submit" name="submit" value="Solve the equation" /> 
     </form> 
     <?php 
    } 
    else 
    { 

     $a=$_POST["a"]; 
     $b=$_POST["b"]; 
     $c=$_POST["c"]; 
     if ($a!=0) 
     { 
      $d=pow($b,2)-(4*$a*$c); 
      if ($d>=0) 
      { 
       $d=sqrt($d); 
       $x1=(-$b-$d)/(2*$a); 
       $x2=(-$b+$d)/(2*$a); 
       if ($x1==$x2) 
       { 
        echo ("1 solution: ". $x1); 
       } 
       else 
       { 
        echo ("Solutions x1 and x2 " .$x1. ", ". $x2); 
       } 
      } 
      else 
      { 
       $d=sqrt(-$d); 
       $x1=-$b/(2*$a); 
       $x2=-$d; 
       $x3=$d; 
       echo ("The trinomial has two complex solutions x1 and x2, which are: x1=". $x1. " + ". $x2. "*i and x2=". $x1. " + ". $x3. "*i"); 
      } 
     } 
     else 
     { 
      echo ("This is not a trinomial"); 
     } 
    } 
    ?> 
    </body> 
</html> 
0

는 일부 코드는 청소 코드를 주목해야한다 한 가지에 댓글을했다가 수정되었습니다.

<?php 
    if(isset($_POST['submit'])){ // had a syntax error with an extra (before isset also instead of checking if its not set just check if it is set makes it faster 
    //Submit has been pressed 
    $a=$_POST["a"]; 
    $b=$_POST["b"]; 
    $c=$_POST["c"]; 
     if ($a!=0){ 
     $d=pow($b,2)-(4*a*c); 
      if ($d>=0){ 
       $d=sqrt($d); 
       $x1=(-$b-$d)/(2*$a); 
       $x2=(-$b+$d)/(2*$a); 
       if ($x1=$x2){ 
        echo ("1 solution: ". $x1); 
       } else { 
        echo ("Solutions x1 and x2 " .$x1. ", ". $x2); 
       } 
      } else { 
       $d=sqrt(-$d); 
       $x1=-$b/(2*$a); 
       $x2=-$d; 
       $x3=$d; 
       echo ("The trinomial has two complex solutions x1 and x2, which are: x1=". x1. " + ". x2. "*i and x2=". x1. " + ". x3. "*i"); 
      } 
     } 
    } else { 
    //Submit has not been pressed yet 
    } 
?> 
<html> 
    <meta charset="utf-8"/> 
    <head> 
     <title> 
      Exercise 2 
     </title> 
    </head> 
    <body> 
     <h2>Please fill the trinomial coefficients a*x^2+b*x+c=0</h2> <br> 
     <form method="post"> 
     a= <input type="text" name="a" /> <br> 
     b= <input type="text" name="b" /> <br> 
     c= <input type="text" name="c" /> <br> 
     <input type="submit" value="Solve the equation" /> 
     </form> 
    </body> 
</html>