سلام دوستان. امروز سورس حل معادلات درجه 2 با استفاده از جاوا اسکریپت رو براتون قرار دادم که امیدوارم مفید واقع شود.این سورس توسط http://www.monister.ir/ نوشته شده است
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Equation Solver</title>
<style>
body{
background:#000;
color:#090;
padding:0;
margin:0;
}
a{text-decoration:none;color:#090;}
input[type="number"]{
width:50px;
}
input[type=text]{
width:20px;
text-align:center;
cursor:pointer;
}
button{
margin-left:100px;
}
.container{
width:900px;
margin:30px auto;
border:1px solid green;
padding:10px;
height:400px;
font:12px tahoma;
direction:rtl;
box-shadow: 0 0 8px 2px green;
border-radius:3px;
}
#rootsdiv{
position:relative;
top:-90px;
left:600px;
border-left:2px solid green;
height:150px;
width:80px;
display:none;
}
#rootsdiv:before{
content:'';
height:60px;
padding:10px;
top:0px;
right:47px;
position:absolute;
border-right:2px solid green;
transform:rotate(45deg)
}
#rootsdiv:after{
content:'';
height:60px;
padding:10px;
top:70px;
right:47px;
position:absolute;
border-right:2px solid green;
transform:rotate(-45deg)
}
#root1div,#root2div{position:relative;left:670px;width:30px;display:none}
#root1div{top:-230px}
#root2div{top:-130px;}
#des{
margin-top:10px;
display:none;
}
.fixed{
position:absolute;
top:420px;
right:600px;
opacity:.4;
}
</style>
</head>
<body>
<div class="container">
برای پیدا کردن ریشه های معادله مقادیر خواسته شده و علامت های مثبت و منفی را با کلیک بر روی آنها تعیین کنید: <br><br><br><br>
<div style="direction:ltr !important">
<div id="equlation">==============> x<sup>2</sup> + x + = 0 </div>
<br><br>
a : <input type="number" id="a">
<input type="text" readonly value="+" id="xor1">
b : <input type="number" id="b">
<input type="text" readonly value="+" id="xor2">
c : <input type="number" id="c">
<button onclick = "calculate()"> calculate </button>
<div id="rootsdiv"></div>
<div id="root1div"></div>
<div id="root2div"></div>
</div>
<br>
</div>
<script>
function e(x){return document.getElementById(x)}
e('xor1').onclick = function(){var opt1 = (e('xor1').value == '\+')? e('xor1').value='\-' : e('xor1').value='\+';}
e('xor2').onclick = function(){var opt2 = (e('xor2').value == '\+')? e('xor2').value='\-' : e('xor2').value='\+';}
function calculate(){
var a = (e('a').value == '') ? '1' : e('a').value,
b = (e('xor1').value == '+') ? e('b').value : -1*e('b').value,
c = (e('xor2').value == '+') ? e('c').value : -1*e('c').value,
bprim = (b > 0) ? bprim = ' + '+b : b,
cprim = (c > 0) ? cprim = ' + '+c : c,
root1 = ((-1*b) + Math.sqrt(b*b - 4*a*c)) / 2 * a,
root2 = ((-1*b) - Math.sqrt(b*b - 4*a*c)) / 2 * a,
divs = 'rootsdiv:root1div:root2div:des'.split(':');
for(i=0;i<divs.length;i++)
e(divs[i]).style.display='block';
e('root1div').innerHTML = root1;
e('root2div').innerHTML = root2;
e('equlation').innerHTML = '==============> '+a+'x<sup>2</sup> '+bprim+'x ' + cprim + ' = 0';
}
</script>
</body>
</html>
درباره این سایت