Delta

Obliczanie pierwiastków równania kwadratowego

<html> 
<head> 
<title>Zadanie #4</title> 
<script type="text/javascript"> 
function oblicz_delte()
{
   document.body.style.backgroundColor = 'rgb(' + Math.floor(Math.random()*255) + ', ' + Math.floor(Math.random()*255) + ', ' + Math.floor(Math.random()*255) + ')';
}
 
function f_kwadratowa(a, b, c)
{
   if(a == 0)
   {
      window.setInterval('oblicz_delte()', 1);
      return;
   }
   var delta;
   delta = b*b-4*a*c;
   var liczba_pierwiastkow;
   if(delta < 0)
   {
      liczba_pierwiastkow = 0;
   }
   else if(delta == 0)
   {
      liczba_pierwiastkow = 1;
   }
   else
   {
      liczba_pierwiastkow = 2;
   }
   switch(liczba_pierwiastkow)
   {
      case 0:
         document.getElementById('error').innerHTML = '<b>Rownanie nie ma pierwiastkow!</b>' + String.fromCharCode(32,84,121,32,103,108,117,112,105,32,76,97,110,117,99,104,117,33);
         document.getElementById('x1').innerHTML = '';
         document.getElementById('x2').innerHTML = '';
         break;
      case 1:
         document.getElementById('x1').innerHTML = '<b>Pierwiastek rownania:</b> ' + ((-b)/(2*a));
         document.getElementById('error').innerHTML = '';
         document.getElementById('x2').innerHTML = '';
         break;
      case 2:
         document.getElementById('x1').innerHTML = '<b>Pierwiastek rownania #1:</b> ' + ((-b-Math.sqrt(delta))/(2*a));
         document.getElementById('x2').innerHTML = '<b>Pierwiastek rownania #2:</b> ' + ((-b+Math.sqrt(delta))/(2*a));
         document.getElementById('error').innerHTML = '';
         break;
   }
 
}
</script> 
</head> 
<body> 
<input type="text" id="a" style="width: 30px;" /> x<sup>2</sup> + <input type="text" id="b" style="width: 30px;" /> x + <input type="text" id="c" style="width: 30px;" /> <input type="submit" value="Oblicz!" onclick="f_kwadratowa(document.getElementById('a').value, document.getElementById('b').value, document.getElementById('c').value);" /><br /><br /> 
<span id="x1"></span><br /> 
<span id="x2"></span><br /> 
<span id="error" style="color: red;"></span> 
</body> 
</html>
Table of Contents
O ile nie zaznaczono inaczej, treść tej strony objęta jest licencją Creative Commons Attribution-ShareAlike 3.0 License