上次做数学题,解方程解的难受,于是乎,在参考别人源码的过程中,写出了自己的计算器
1 <html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html" charset="utf-8"> 4 <title>在线一元二次方程式计算器</title> 5 </head> 6 <body> 7 <form name="fquad"> 8 <p align="center">解二次方程式计算<br> 9 </p> 10 <table align="center"> 11 <tbody> 12 <tr> 13 <td bgcolor="#990000"> 14 <h2><font color="#ffffff"><input size="4" name="fa" type="text"> x<sup>2</sup>+ <input size="4" name="fb" type="text"> x + <input size="4" name="fc" type="text"> = 0 <input onclick="checkQuad()" type="button" value="解题"> <input type="reset" value="重置"> </font></h2> 15 <p align="center"><font color="#ffffff" face="Arial"><b>一元二次方程的解法</b></font></p> 16 </td> 17 </tr> 18 <tr> 19 <td bgcolor="#990000"> 20 <h2><font color="#ffffff">x<sub><a style="text-decoration: none" ><font color="#ffffff">1</font></a></sub>=<input size="45" name="x1" type="text"> <br> 21 x<sub>2</sub>=<input size="45" name="x2" type="text"> </font></h2> 22 </td> 23 </tr> 24 <tr> 25 26 </tr> 27 </tbody> 28 </table> 29 </form> 30 <p align="center">Made by CRoot</p> 31 <script language="JavaScript"> 32 <!-- 33 var rootparti; 34 var rootpart; 35 var det; 36 var rootparti1; 37 var rootparti2; 38 var a; 39 var b; 40 var c; 41 var x1; 42 var x2; 43 var i = "i"; 44 function checkQuad() { 45 var a = document.fquad.fa.value; 46 var b = document.fquad.fb.value; 47 var c = document.fquad.fc.value; 48 if (a == 0 && c != 0) { 49 x1 = -c / b; 50 x2 = "Not a quadratic equation, but here is your answer for x"; 51 document.fquad.x1.value=x1; 52 document.fquad.x2.value=x2; 53 } 54 else if (a == "" && c != 0) { 55 x1 = -c / b; 56 x2 = "Not a quadratic equation"; 57 document.fquad.x1.value=x1; 58 document.fquad.x2.value=x2; 59 } 60 else { 61 quad(); 62 } 63 } 64 function quad() { 65 var a = document.fquad.fa.value; 66 var b = document.fquad.fb.value; 67 var c = document.fquad.fc.value; 68 det = Math.pow(b,2) - 4 * a * c; 69 rootpart = Math.sqrt(det) / (2 * a); 70 rootparti = (Math.sqrt(-det) / (2 * a)) + i; 71 if (parseFloat(rootparti) < 0) { 72 rootparti1 = rootparti; 73 rootparti2 = (-1 * parseFloat(rootparti)) + i; 74 } 75 else { 76 rootparti1 = (-1 * parseFloat(rootparti)) + i; 77 rootparti2 = rootparti; 78 } 79 if (rootparti1 == "1i") { 80 rootparti1 = i; 81 rootparti2 = "-i"; 82 } 83 else if (rootparti1 == "-1i") { 84 rootparti1 = "-i"; 85 rootparti2 = i; 86 } 87 if (det == 0) { 88 x1 = x2 = -b / (2 * a); 89 } 90 else if (det > 0) { 91 x1 = (-b + Math.sqrt(det)) / (2 * a); 92 x2 = (-b - Math.sqrt(det)) / (2 * a); 93 } 94 else if ((-b / (2 * a)) == 0) { 95 x1 = rootparti1; 96 x2 = rootparti2; 97 } 98 else { 99 x1 = (-b / (2 * a) + " + " + rootparti1); 100 x2 = (-b / (2 * a) + " + " + rootparti2); 101 } 102 document.fquad.x1.value=x1; 103 document.fquad.x2.value=x2; 104 } 105 // will solve for complex numbers 106 107 // --> 108 </script> 109 </body> 110 </html>
Made by CRoot