ข้ามไปที่เนื้อหาหลัก

การคำนวนค่าความต้านทานแบบต่ออนุกรม




<!DOCTYPE html>
<html>
<head>
<title>การหาค่าความต้านทานแบบอนุกรม</title>
<meta charset="utf-8">
<script src="../js/jquery.min.js"></script>
<script>
var arr_resistance = [];
var resistance = '';

function display_input(max_fields){
var x = 1; //initlal text box count
$('#show_input').html('');
for(var i = x; i <= max_fields; i++) {
$('#show_input').append('<div>R'+i+' : <input type="text" class="input_resistance"/> โอห์ม</div>'); //add input box
if(resistance != ''){
resistance += ' + ';
}
resistance += 'R'+i;
}
$('#show_button').html('<div id="btn_nextstep"><button onclick="step1()">ขั้นตอนต่อไป (1)</button></div>'); //add input box
}

function check_data(){
var msg = "";
$( ".input_resistance" ).each(function(index) {
if($(this).val() == ""){
msg += "- ค่า R" + (index+1) + "\n";
}
});
if(msg == ""){
return true;
}else{
msg = "กรุณากรอกข้อมูลให้ครบถ้วน\n" + msg;
alert(msg);
return false;
}
}

function step1(){
if( check_data() == true){
$('#show_input').append('<br/><div><u>สูตรการคำนวณ การต่อตัวต้านทานแบบอนุกรม</u></div>');
$('#show_input').append('<div><b>RT = '+ resistance +'</b> </div>');
$('#show_button').html('<div id="btn_nextstep"><button onclick="step2()">ขั้นตอนต่อไป (2)</button></div>');
}
}

function step2(){
var input_resistance = '';
$( ".input_resistance" ).each(function() {
if(input_resistance != ''){
input_resistance += " + ";
}
input_resistance += $( this ).val();
arr_resistance.push($( this ).val());
});
$('#show_input').append('<div><b>RT = '+ input_resistance +'</b> </div>');
$('#show_button').html('<div id="btn_nextstep"><button onclick="process()">ดูผลลัพธ์</button></div>');
}

function process() {
myTotal = 0;
for(var i = 0, len = arr_resistance.length; i < len; i++) {
myTotal += parseFloat(arr_resistance[i]);
}
document.getElementById("result").innerHTML = '<b>ค่าความต้านทานรวม = ' + myTotal + ' โอห์ม</b>' ;
}
</script>

</head>
<body>

<p>การคำนวณค่าความต้านทานแบบต่ออนุกรม</p>

<h3>RT = R1 + R2.....+Rn</h3>

<div>
จำนวนตัวต้านทาน : <input type="text" id="qty" onkeyup="display_input(this.value)" /> ตัว
</div>

<p id="show_input"></p>
<p id="result"></p>
<p id="show_button"></p>

</body>
</html>

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

แจกฟรี!! ระบบต่างๆที่พัฒนาด้วย PHP สำหรับนำไปใช้ในงานต่างๆ

       สำหรับหลายท่านที่ขอโค้ดเข้ามาทาง Inbox ของเฟซบุ๊กแฟนเพจ หรือถามถึงระบบต่างๆหลังไมค์มานั้น ส่วนใหญ่ก็มีแจกอยู่แล้วในเว็บบอร์ด ThaiCreate.Com นะครับ และด้านล่างนี้ก็เป็นระบบต่างๆที่แจกให้นำไปลองใช้ลองศึกษากันครับ

สร้างแบบฟอร์มล็อกอิน HTML + PHP

       ผมจะสร้างระบบตัวอย่างเพื่อแสดงให้เห็นการทำงานของสคริปต์ PHP ที่ใช้ในการล็อกอินอย่างง่ายๆ นะครับ โดยที่ผมจะสร้างโฟลเดอร์ใหม่ขึ้นในไดร์ฟที่เราได้กำหนดให้เป็น Directory Root สำหรับรันสคริปต์ PHP ซึ่งผมกำหนดเอาไว้ที่ D:\www ดังนั้นผมจะสร้างโฟลเดอร์ teacher_assistant ขึ้นมาใหม่จะได้พาธเป็น D:\www\teacher_assistant สำหรับการเข้าถึง จะใช้ URL ดังนี้ http://localhost/teacher_assistant หลังจากสร้างโฟลเดอร์เสร็จเรียบร้อยก็จะเริ่มกระบวนการเตรียมโครงสร้างโปรเจ็กต์ด้วยเครื่องมือช่วยเขียนโปรแกรมชื่อว่า eclipse ซึ่งดูวิธีการสร้างโปรเจ็กต์ได้จากบทความ เริ่มต้นสร้างโปรเจ็กต์ ด้วยโปรแกรม eclipse เครื่องมือเขียนโปรแกรม php ในบทความเป็นการสร้างโปรเจ็กต์ที่มีพาธเป็นโฟลเดอร์ login แต่ในที่นี้ต้องเป็นพาธให้ตรงกับโฟลเดอร์ใหม่ที่เราได้สร้างขึ้นด้วยนะครับ

PHP กับการคิดส่วนลดเป็นเปอร์เซ็น 5%, 10%, 20% ตามช่วงราคาที่กำหนด

<?php     $total_price = 1000;     $discount = 0;         if($total_price >= 500 && $total_price < 1001){         $discount = 5;     }elseif($total_price >= 1001 && $total_price <= 5000){         $discount = 10;     }elseif($total_price >= 5001){         $discount = 20;     }         $discount_bath = ($total_price*$discount)/100; ?> <pre> ซื้อสินค้าครบ 0 ถึง 499 บาท ไม่ได้ส่วนลด ซื้อสินค้าครบ 500 ถึง 1000 บาท ได้ส่วนลด 5% ซื้อสินค้าครบ 1001 ถึง 5000บาท ได้ส่วนลด 10% ซื้อสินค้าครบ 5001 บาทขึ้นไป ได้ส่วนลด 20% </pre> <h3>รวมราคาสินค้า = <?php echo number_format($total_price,2);?></h3> <h5>ส่วนลด = <?php echo $discount;?>%  (<?php echo $discount_bath;?> บาท)</h5> <h2>ราคาหลังส่วนลด = <?php echo number_format($total_price - $discount_bath,2);?>บาท</h2> "PHP ไม่ได้สร้างสุดยอดโปรแกรม แต่ PHP ช่วยให้งานคุณง่ายขึ้นต