محاسبه مساحت اشکال هندسی ساده (با javascript)
این کد برای محاسبه مساحت اشکال هندسی ساده (مربع، مستطیل، مثلث، دایره و کره) به کار می رود. ابتدا باید نوع شکل هندسی را مشخص کنید و سپس اطلاعاتی در مورد شکل، از شما دریافت می شود. در آخر، مساحت شکل، نمایش داده می شود :
کدها :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="JavaScript">
function doArea(num) {
switch(num) {
case 0 : return (""); break;
case 1 : var length = prompt("Please enter the length of your square:", "");
length = length * length;
return (length); break;
case 2 : var width = prompt("Please enter the width of the base:", "");
var height = prompt("Please enter the height of the triangle:", "");
return (width * height / 2); break;
case 3 : var width = prompt("Please enter the width of your rectangle:", "");
var height = prompt("Please enter the height of your rectangle:", "");
return (width * height); break;
case 4 : var radius = prompt("Please enter the radius of the circle: ", "");
return (Math.PI * Math.pow(radius, 2)); break;
case 5 : var radius = prompt("Please enter the radious of the sphere:", 0);
return (4 * Math.PI * (Math.pow(radius, 2))); break;
}
}
</script>
</head>
<body>
<center>
<form name=calcarea>
Find the area of a
<select name="shape" size="1" onChange="this.form.area.value = doArea(this.selectedIndex);">
<option> ...
<option value="square">Square
<option value="triangle">Triangle
<option value="rectangle">Rectangle
<option value="circle">Circle
<option value="sphere">Sphere
</select>
= <input type=text name=area size=10>
</form>
</center>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="JavaScript">
function doArea(num) {
switch(num) {
case 0 : return (""); break;
case 1 : var length = prompt("Please enter the length of your square:", "");
length = length * length;
return (length); break;
case 2 : var width = prompt("Please enter the width of the base:", "");
var height = prompt("Please enter the height of the triangle:", "");
return (width * height / 2); break;
case 3 : var width = prompt("Please enter the width of your rectangle:", "");
var height = prompt("Please enter the height of your rectangle:", "");
return (width * height); break;
case 4 : var radius = prompt("Please enter the radius of the circle: ", "");
return (Math.PI * Math.pow(radius, 2)); break;
case 5 : var radius = prompt("Please enter the radious of the sphere:", 0);
return (4 * Math.PI * (Math.pow(radius, 2))); break;
}
}
</script>
</head>
<body>
<center>
<form name=calcarea>
Find the area of a
<select name="shape" size="1" onChange="this.form.area.value = doArea(this.selectedIndex);">
<option> ...
<option value="square">Square
<option value="triangle">Triangle
<option value="rectangle">Rectangle
<option value="circle">Circle
<option value="sphere">Sphere
</select>
= <input type=text name=area size=10>
</form>
</center>
</body>
</html>
دسته بندی
کدهای آماده برای محاسبه گرها
نظرات 0 0 0