2次方程式の解法のソース

次の----から---までをメモ帳で作成し,"\rensyu\ex03.html"として保存する。

-------------------------------------------------------------------

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;CHARSET=shift_jis">
<title>2次方程式の解法</title>

<script language="JavaScript">
<!--
function calc(a0, b0, c0)
{
   var a, b, c, d, res;
   a = a0; b = b0; c = c0;
   if (a == 0) {
      document.Result.result.value = "2次の項の係数がゼロです\n";
      return;
   }
   res = a + "x^2 "
   if (b > 0) {
      res = res + "+ " + b + "x ";
   }
   else {
      res = res + "- " + Math.abs(b) + "x ";
   }
   if (c > 0){
      res = res + "+ " + c + "\n";
   }
   else {
      res = res + "- " + Math.abs(c) + "\n";
   }
   d = b*b-4*a*c;
   if (d > 0) {
      if (b < 0) {
         x1 = (-b-Math.sqrt(d))/2/a;
         x2 = -b/a-x1;
      }
      else {
         x1 = (-b+Math.sqrt(d))/2/a;
         x2 = -b/a-x1;
      }
      document.Result.result.value = res + "2実解: x1 = " + x1 + ", x2 = " + x2 + "\n\n";
   }
   else if (d == 0) {
      document.Result.result.value = res + "重解: x = " + (-b/2/a) + "\n\n";
   }
   else {
      document.Result.result.value = res + "虚解: 実部 = " + (-b/2/a) + " 虚部 = " + (Math.sqrt(-d)/2/a) + "\n\n";
   } 
}
-->
</script>
</head>

<body bgcolor="#ffffff">

<h1>2次方程式の解</h1>

<form name=Result>

<p>
   2次の係数<input name="data2" value=1 size=5>
   1次の係数<input name="data1" value=1 size=5>
   定数項<input name="data0" value=1 size=5>
</p>

<input type="button" name="calcurate" value="計 算" onClick="calc(this.form.data2.value, this.form.data1.value, this.form.data0.value)">  
<input type="button" name="clear" value="入力欄クリア" onClick="this.form.data2.value=this.form.data1.value=this.form.data0.value=''">  
<input type="button" name="clear" value="出力欄クリア" onClick="this.form.result.value=''"><p>
<p>
出力欄<br><textarea name="result" ROWS=5 COLS=70></textarea>
</p>

</form>

<hr>
<A HREF="javascript:history.go(-1)">直前のページへ戻る</A>

</body>
</html>

-------------------------------------------------------------------


戻る