ผลต่างระหว่างรุ่นของ "01204111 model codes"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
 
(ไม่แสดง 46 รุ่นระหว่างกลางโดยผู้ใช้ 2 คน)
แถว 44: แถว 44:
 
* ?? ไม่ต้องสอนการประกาศตัวแปรแบบ <tt>const</tt>
 
* ?? ไม่ต้องสอนการประกาศตัวแปรแบบ <tt>const</tt>
 
* ไม่ต้องสอน <tt>ConvertTo()</tt> และการทำ type casting ระหว่างตัวเลขเป็นตัวอักษร (เช่น <tt>(int)'A'</tt> หรือ <tt>(char)65</tt>)
 
* ไม่ต้องสอน <tt>ConvertTo()</tt> และการทำ type casting ระหว่างตัวเลขเป็นตัวอักษร (เช่น <tt>(int)'A'</tt> หรือ <tt>(char)65</tt>)
 +
 +
=== ลำดับโปรแกรมที่พัฒนา ===
 +
 +
==== โจทย์: คำนวณบิลค่าอาหาร ====
 +
* หัวข้อการเรียนรู้
 +
** การคำนวณแบบบวกเลขอย่างง่าย
 +
** นิพจน์ทางคณิตศาสตร์ (math expression)
 +
** การส่งออกเอาท์พุท
 +
** ความหมายของคำสั่ง (statement)
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรม
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        Console.WriteLine(82+64+90+75+33);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรมที่ปรับเอาท์พุทให้เหมาะสมขึ้น
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        Console.Write("Total cost is ");
 +
        Console.WriteLine(82+64+90+75+33);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
==== โจทย์: บิลอาหารพร้อมส่วนลด 20% ====
 +
* หัวข้อการเรียนรู้
 +
** ลำดับความสำคัญของตัวดำเนินการ
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรมที่ผิด (การคูณเกิดขึ้นกับ 33 เท่านั้น)
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        Console.Write("Total cost is ");
 +
        Console.WriteLine(82+64+90+75+33 * 0.8);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรมที่ถูก (ใช้วงเล็บครอบนิพจน์บวกทั้งหมด)
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        Console.Write("Total cost is ");
 +
        Console.WriteLine( (82+64+90+75+33) * 0.8);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
==== โจทย์: สั่งอาหารซ้ำกันหลายเมนู ====
 +
* หัวข้อการเรียนรู้
 +
** ใช้วงเล็บช่วยให้เขียนโปรแกรมไม่กำกวม แม้ไม่ทำให้การทำงานเปลี่ยน
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรม (เขียนโดยอาศัยลำดับความสำคัญ * ที่มาก่อน +)
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        Console.Write("Total cost is ");
 +
        Console.WriteLine(1*82+3*64+2*90+5*75+10*33);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรม (เพิ่มวงเล็บเพื่อความชัดเจน)
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        Console.Write("Total cost is ");
 +
        Console.WriteLine( (1*82) + (3*64) + (2*90) + (5*75) + (10*33) );
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
==== โจทย์: แชร์ค่าอาหาร ====
 +
* หัวข้อการเรียนรู้
 +
** การหารแบบจำนวนเต็ม และการหารแบบทศนิยม
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรมที่ผิด (ใช้การหารแบบจำนวนเต็ม)
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        Console.Write("Total amount is ");
 +
        Console.WriteLine( 82+64+90+75+33 );
 +
        Console.Write("Each has to pay ");
 +
        Console.WriteLine( (82+64+90+75+33) / 5 );
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรมที่ถูก (เปลี่ยนให้เป็นการหารแบบทศนิยม)
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        Console.Write("Total amount is ");
 +
        Console.WriteLine( 82+64+90+75+33 );
 +
        Console.Write("Each has to pay ");
 +
        Console.WriteLine( (82+64+90+75+33) / 5.0 );
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรมที่ปรับปรุงแล้ว (ใช้ตัวแปรเก็บค่านิพจน์ที่คำนวณซ้ำซ้อน)
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        int total = 82+64+90+75+33;
 +
        Console.Write("Total amount is ");
 +
        Console.WriteLine(total);
 +
        Console.Write("Each has to pay ");
 +
        Console.WriteLine( total / 5.0 );
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
==== โจทย์: แชร์ค่าอาหารพร้อมให้ทิป ====
 +
* หัวข้อการเรียนรู้
 +
** การหารเอาเศษและตัวดำเนินการ %
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรม
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        int total;
 +
        total = 82+64+90+75+33;
 +
        Console.Write("Total amount: ");
 +
        Console.WriteLine( total );
 +
        Console.Write("Each has to pay: ");
 +
        Console.WriteLine( total / 5 );
 +
        Console.Write("Tip: ");
 +
        Console.WriteLine( total % 5 );
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
==== โจทย์: แปลงอุณหภูมิจากองศาเซลเซียสเป็นเคลวิน ====
 +
* หัวข้อการเรียนรู้
 +
** การอ่านอินพุทด้วย <tt>Console.ReadLine()</tt>
 +
** ชนิดข้อมูลแบบจำนวนและสตริง
 +
** ตัวดำเนินการ + ที่ให้พฤติกรรมแตกต่างกันเมื่อใช้กับจำนวนและสตริง
 +
** การใช้เมท็อด <tt>Parse()</tt> เพื่อเปลี่ยนสตริงเป็นจำนวน
 +
** การฟอร์แมตเอาท์พุท
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรมที่ผิด (บวกสตริงเข้ากับจำนวน)
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        Console.Write("Enter temperature in degrees Celcius: ");
 +
        string c = Console.ReadLine();
 +
        Console.Write("Kelvin: ");
 +
        Console.WriteLine(c+273.15);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรมที่ถูก #1
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        Console.Write("Enter temperature in degrees Celcius: ");
 +
        double c = double.Parse(Console.ReadLine());
 +
        Console.Write("Kelvin: ");
 +
        Console.WriteLine(c+273.15);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรมที่ถูก #2 (ฟอร์แมตข้อความให้เหมาะสม)
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        Console.Write("Enter temperature in degrees Celcius: ");
 +
        double c = double.Parse(Console.ReadLine());
 +
        Console.WriteLine("{0} degrees Celcius is equal to {1} Kelvin",c,c+273.15);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
==== โจทย์: แปลงอุณหภูมิจากองศาเซลเซียสเป็นหน่วยอื่น ๆ ====
 +
* หัวข้อการเรียนรู้
 +
** การวิเคราะห์โจทย์
 +
** การฟอร์แมตตัวเลขให้มีจำนวน​ทศนิยมที่กำหนด
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรม
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        Console.Write("Enter temperature in degrees Celcius: ");
 +
        double c = double.Parse(Console.ReadLine());
 +
        double f = ((c*9.0)/5.0)+32;
 +
        double r = (c*4)/5;
 +
        double k = c + 273.15;
 +
        Console.WriteLine("{0:f2} degrees Celsius is equal to:", c);
 +
        Console.WriteLine("  {0:f2} degrees Fahrenheit", f);
 +
        Console.WriteLine("  {0:f2} degrees Romer", r);
 +
        Console.WriteLine("  {0:f2} Kelvin", k);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
==== โจทย์: คำนวณเงินฝากธนาคาร ====
 +
* หัวข้อการเรียนรู้
 +
** การวิเคราะห์โจทย์
 +
** การใช้ไลบรารี <tt>Math</tt>
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างโปรแกรม
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class Program
 +
{
 +
    static void Main()
 +
    {
 +
        Console.Write("Principle (Baht): ");
 +
        double principle = double.Parse(Console.ReadLine());
 +
        Console.Write("Rate (% per year): ");
 +
        double rate = double.Parse(Console.ReadLine());
 +
        Console.Write("Time (years): ");
 +
        int year = int.Parse(Console.ReadLine());
 +
        Console.Write("Amount: ");
 +
        double amount = principle * Math.Pow( 1 + (rate/100), year);
 +
        Console.WriteLine(amount);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
  
 
== โปรแกรมย่อยและไลบรารี ==
 
== โปรแกรมย่อยและไลบรารี ==
แถว 54: แถว 386:
 
** ครอบคลุมเฉพาะ pass by value
 
** ครอบคลุมเฉพาะ pass by value
 
** ?? การกำหนดพารามิเตอร์ด้วย keyword argument ตัวอย่างเช่น
 
** ?? การกำหนดพารามิเตอร์ด้วย keyword argument ตัวอย่างเช่น
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
คลิก "ขยาย" เพื่อดูตัวอย่างโปรแกรม
 +
<div class="mw-collapsible-content">
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 
double bmi(double weight, double height)
 
double bmi(double weight, double height)
แถว 62: แถว 397:
 
Console.WriteLine(bmi(height:175,weight:72));
 
Console.WriteLine(bmi(height:175,weight:72));
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</div>
 +
</div>
  
 
* สโคปของตัวแปร
 
* สโคปของตัวแปร
แถว 67: แถว 404:
 
=== ลำดับของโปรแกรมที่จะพัฒนา ===
 
=== ลำดับของโปรแกรมที่จะพัฒนา ===
  
'''1. โปรแกรมคำนวณพื้นที่วงกลม 1'''
+
==== 1. โปรแกรมคำนวณพื้นที่วงกลม 1 ====
 
<div class="toccolours mw-collapsible mw-collapsed">
 
<div class="toccolours mw-collapsible mw-collapsed">
 
ตัวอย่างแรกสุดที่แสดงการเรียกโปรแกรมย่อยโดยไม่ต้องส่งพารามิเตอร์ใดๆ เพื่อแสดง top-down design ขั้นพื้นฐาน
 
ตัวอย่างแรกสุดที่แสดงการเรียกโปรแกรมย่อยโดยไม่ต้องส่งพารามิเตอร์ใดๆ เพื่อแสดง top-down design ขั้นพื้นฐาน
แถว 94: แถว 431:
 
</div>
 
</div>
  
'''2. โปรแกรมคำนวณพื้นที่วงกลม 2'''
+
==== 2. โปรแกรมคำนวณพื้นที่วงกลม 2 ====
 
<div class="toccolours mw-collapsible mw-collapsed">
 
<div class="toccolours mw-collapsible mw-collapsed">
 
ทำงานเดียวกับโปรแกรมแรก แต่มีการเรียกโปรแกรมย่อยที่มีพารามิเตอร์แบบ pass by value และรีเทิร์นผลลัพธ์ แสดงการออกแบบโปรแกรมแบบ modular และ stepwise refinement มากยิ่งขึ้น
 
ทำงานเดียวกับโปรแกรมแรก แต่มีการเรียกโปรแกรมย่อยที่มีพารามิเตอร์แบบ pass by value และรีเทิร์นผลลัพธ์ แสดงการออกแบบโปรแกรมแบบ modular และ stepwise refinement มากยิ่งขึ้น
แถว 128: แถว 465:
 
</div>
 
</div>
  
'''3. โปรแกรมคำนวณค่าเฉลี่ยของตัวแปร 3 ตัว (version 2)'''
+
==== 3. โปรแกรมคำนวณค่าเฉลี่ยของตัวแปร 3 ตัว (version 2) ====
 
<div class="toccolours mw-collapsible mw-collapsed">
 
<div class="toccolours mw-collapsible mw-collapsed">
 
หาค่าเฉลี่ยของจำนวนเต็มสามตัว เป็นโปรแกรมที่ปรับมาจากโปรแกรมที่ Main ทำทุกอย่าง เป็นอีกตัวอย่างที่แสดงการใช้โปรแกรมย่อย
 
หาค่าเฉลี่ยของจำนวนเต็มสามตัว เป็นโปรแกรมที่ปรับมาจากโปรแกรมที่ Main ทำทุกอย่าง เป็นอีกตัวอย่างที่แสดงการใช้โปรแกรมย่อย
แถว 169: แถว 506:
 
</div>
 
</div>
  
'''4. โปรแกรมคำนวณค่าเฉลี่ยของตัวแปร 3 ตัว (version 3)'''
+
==== 4. โปรแกรมคำนวณค่าเฉลี่ยของตัวแปร 3 ตัว (version 3) ====
 
<div class="toccolours mw-collapsible mw-collapsed">
 
<div class="toccolours mw-collapsible mw-collapsed">
 
หาค่าเฉลี่ยของจำนวนเต็มสามตัว ใช้ out พารามิเตอร์ในการส่งค่ากลับ
 
หาค่าเฉลี่ยของจำนวนเต็มสามตัว ใช้ out พารามิเตอร์ในการส่งค่ากลับ
แถว 207: แถว 544:
 
int i = int.Parse(Console.ReadLine());
 
int i = int.Parse(Console.ReadLine());
 
return i;
 
return i;
 +
}
 +
}
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
==== 5. โปรแกรมคำนวณพื้นที่สี่เหลี่ยมคางหมู ====
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ปรับแก้จากตัวอย่างการคำนวณค่าเฉลี่ย แสดงตัวอย่างการนำโปรแกรมที่แบ่งโครงสร้างที่ดีไว้แล้วมาปรับแก้
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
namespace SRch2_trapezoid
 +
{
 +
class Program
 +
{
 +
public static void Main()
 +
{
 +
double side1, side2, height;
 +
Console.WriteLine("Give me the size of your trapezoid.");
 +
readTrapezoid(out side1, out side2, out height);
 +
Console.WriteLine("Trapezoid's area is {0}", trapezoidArea(side1, side2, height));
 +
 +
Console.ReadKey(true);
 +
}
 +
 +
static void readTrapezoid(out double a, out double b, out double h)
 +
{ // read the two parallel side lengths (a and b), and height of a trapezoid (h)
 +
a = readDouble("Parallel side 1's length: ");
 +
b = readDouble("Parallel side 2's length: ");
 +
h = readDouble("Height: ");
 +
 +
}
 +
 +
static double trapezoidArea(double a, double b, double h)
 +
{
 +
return 0.5*(a+b)*h;
 +
}
 +
 +
static double readDouble(string prompt)
 +
{
 +
Console.Write(prompt);
 +
double d = double.Parse(Console.ReadLine());
 +
return d;
 
}
 
}
 
}
 
}
แถว 216: แถว 599:
 
=== โปรแกรมตัวอย่างอื่น ๆ ===
 
=== โปรแกรมตัวอย่างอื่น ๆ ===
 
==== หาพื้นที่สี่เหลี่ยม ====
 
==== หาพื้นที่สี่เหลี่ยม ====
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ตัวอย่างแสดงการแยกส่วนของการคำนวณเป็นโปรแกรมย่อย
 +
<div class="mw-collapsible-content">
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 
using System;
 
using System;
แถว 234: แถว 620:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</div>
 +
</div>
  
 
==== ตัวอย่าง 2 (มีการใช้หลาย method) ====
 
==== ตัวอย่าง 2 (มีการใช้หลาย method) ====
แถว 243: แถว 631:
 
* ทุกตัวอย่างมีการใช้โปรแกรมย่อยเสมอ
 
* ทุกตัวอย่างมีการใช้โปรแกรมย่อยเสมอ
 
=== ตัวอย่างโปรแกรม ===
 
=== ตัวอย่างโปรแกรม ===
 +
==== แก้สมการกำลังสอง ====
 +
เป็นตัวอย่างของการเขียนโปรแกรมแก้สมการกำลังสอง <math>ax^2 + bx + c</math> โดยพิจารณาแบบ top-down
 +
 +
ส่วนแรกเป็นโปรแกรมหลักที่รับสัมประสิทธิ์ a,b,c และเรียกใช้โปรแกรมย่อยในการประมวลผล
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
เมท็อด Main
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
    public static void Main (string[] args)
 +
    {
 +
        double a, b, c;
 +
        ReadCoefficients (out a, out b, out c);
 +
 +
        SolveAndOutput (a, b, c);
 +
    }
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
เริ่มเขียนส่วนรับอินพุต
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
เขียนเมท็อด ReadCoefficients
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
    static void ReadCoefficients(out double a, out double b, out double c)
 +
    {
 +
        Console.Write ("Please enter a:");
 +
        a = ReadDouble ();
 +
        Console.Write ("Please enter b:");
 +
        b = ReadDouble ();
 +
        Console.Write ("Please enter c:");
 +
        c = ReadDouble ();
 +
    }
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ที่ใช้เมท็อด ReadDouble
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
    static double ReadDouble()
 +
    {
 +
        string line = Console.ReadLine ();
 +
        return Double.Parse (line);
 +
    }
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
เมื่อเขียนถึงจุดนี้ควรจะทดสอบโปรแกรมเสียก่อน อาจจะต้อง comment SolveAndOutput ไปก่อน และเพิ่มคำสั่งในการพิมพ์ค่า a b c ออกมา
 +
 +
โครงหลักของการแก้สมการ SolveAndOutput มีเงื่อนไขเกี่ยวกับค่า <math>b^2-4ac</math>  ที่ต้องรวมการแสดงผลไว้ด้วยเพราะว่าแสดงผลได้สองแบบ (และเรายังคืนค่าเป็น complex ไม่เป็น) ซึ่งถ้าแบ่งงานอีกแบบคือให้โปรแกรมย่อยคืนค่าเป็นจำนวนเชิงซ้อนตลอดเวลา ก็จะเขียนได้สะอาดกว่านี้  ในตอนแรกเราจะเขียนแค่ส่วนหาคำตอบกรณีที่เป็นจำนวนจริงก่อนเพื่อให้สามารถทดสอบโปรแกรมได้เมื่อทำส่วนย่อยแรกนี้เสร็จ
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
เขียนในเมท็อด SolveAndOutput
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
    static void SolveAndOutput(double a, double b, double c)
 +
    {
 +
        if (HasRealSolutions (a, b, c)) {
 +
            double sol1, sol2;
 +
            FindRealSolutions (a, b, c, out sol1, out sol2);
 +
            OutputRealSolutions (sol1, sol2);
 +
        } else {
 +
        }
 +
    }
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
ส่วนตรวจสอบว่ามีคำตอบเป็นจำนวนจริง (ไม่ได้ตรวจว่ามีคำตอบเดียวหรือเปล่า --- สามารถเก็บไปเป็นการบ้านที่ทำในแลบได้)
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ในการตรวจสอบจะคำนวณ inner term ด้วยเมท็อด CalculateInnerTerm ก่อน จากนั้นจึงค่อนตรวจสอบค่า
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
    static double CalculateInnerTerm(double a, double b, double c)
 +
    {
 +
        return b * b - 4 * a * c;
 +
    }
 +
 +
    static bool HasRealSolutions(double a, double b, double c)
 +
    {
 +
        return CalculateInnerTerm (a, b, c) >= 0;
 +
    }
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
จากนั้นจึงเมท็อดที่เหลือในการหาคำตอบกรณีคำตอบเป็นจำนวนจริง
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
มีเมท็อด FindRealSolutions และ OutputRealSolutions
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
    static void FindRealSolutions(double a, double b, double c,
 +
                                  out double sol1, out double sol2)
 +
    {
 +
        double innerTerm = CalculateInnerTerm (a, b, c);
 +
 +
        sol1 = (-b + Math.Sqrt(innerTerm)) / (2 * a);
 +
        sol2 = (-b - Math.Sqrt(innerTerm)) / (2 * a);
 +
    }
 +
 +
    static void OutputRealSolutions(double sol1, double sol2)
 +
    {
 +
        Console.WriteLine ("There are two real solutions: {0} and {1}", sol1, sol2);
 +
    }
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
เมื่อเขียนถึงตรงนี้ควรจะทดสอบโปรแกรมก่อน (ไม่ควรเขียนต่อ) เราควรเน้นให้นิสิตทดสอบโปรแกรมเป็นระยะ ๆ ไม่ใช่เขียนรวดเดียวแล้วทดสอบทีเดียว
 +
 +
จากนั้นเราจะเพิ่มส่วนคำนวณคำตอบในกรณีที่คำตอบเป็น complex ในเมท็อด SolveAndOutput (ในส่วน else)
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
เขียนเพิ่มเติมในเมท็อด SolveAndOutput
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
    static void SolveAndOutput(double a, double b, double c)
 +
    {
 +
        if (HasRealSolutions (a, b, c)) {
 +
            double sol1, sol2;
 +
            FindRealSolutions (a, b, c, out sol1, out sol2);
 +
            OutputRealSolutions (sol1, sol2);
 +
        } else {
 +
            double sol1real, sol1img, sol2real, sol2img;
 +
            FindComplexSolutions (
 +
                a, b, c,
 +
                out sol1real, out sol1img,
 +
                out sol2real, out sol2img
 +
            );
 +
            OutputComplexSolutions (sol1real, sol1img, sol2real, sol2img);
 +
        }
 +
    }
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
โค้ดที่เหลือกรณีทีคำตอบเป็น complex (และไม่เป็นจำนวนจริง) เขียนคล้าย ๆ เดิม แต่ในการคืนคำตอบจะคืนสองคำตอบแยกเป็นส่วน real part และ imaginary part เราเลือกที่จะคำนวณคำตอบสองคำตอบให้เรียบร้อยใน FindComplexSolutions เลย แม้ว่าจะดูว่าเป็นการทำงานซ้ำซ้อน (เพราะว่า real part เท่ากัน) แต่เนื่องจากเราไม่ต้องการให้เงื่อนไขและรายละเอียดดังกล่าวไปปะปนอยู่ในส่วนแสดงผลลัพธ์
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ด้านล่างเป็นเมท็อด FindComplexSolutions และ OutputComplexSolutions
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
    static void FindComplexSolutions(double a, double b, double c,
 +
                                    out double sol1real, out double sol1img,
 +
                                    out double sol2real, out double sol2img)
 +
    {
 +
        double innerTerm = CalculateInnerTerm (a, b, c);
 +
 +
        double realPart = -b / (2 * a);
 +
        double imgPart = Math.Sqrt(-innerTerm) / (2 * a);
 +
 +
        sol1real = realPart;
 +
        sol1img = imgPart;
 +
 +
        sol2real = realPart;
 +
        sol2img = -imgPart;
 +
    }
 +
 +
    static void OutputComplexSolutions(double sol1real, double sol1img,
 +
                                      double sol2real, double sol2img)
 +
    {
 +
        Console.WriteLine (
 +
            "There are two complex solutions: {0}+{1}i and {2}-{3}i",
 +
            sol1real, sol1img,
 +
            sol2real, sol2img
 +
        );
 +
    }
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 
==== คิดค่าส่งไปรษณีย์ ====
 
==== คิดค่าส่งไปรษณีย์ ====
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
แสดงการแบ่งงานเป็นหลายกรณี
 +
<div class="mw-collapsible-content">
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 
using System;
 
using System;
แถว 288: แถว 850:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</div>
 +
</div>
  
 
== โครงสร้างคำสั่งแบบทางเลือกหลายชั้น ==
 
== โครงสร้างคำสั่งแบบทางเลือกหลายชั้น ==
 
* ไม่ต้องสอน switch/case
 
* ไม่ต้องสอน switch/case
 
* ใช้ flow-chart และตัวอย่างเยอะ ๆ
 
* ใช้ flow-chart และตัวอย่างเยอะ ๆ
 +
=== ตัวอย่างโปรแกรม ===
 +
==== คำนวณค่ามากที่สุดของจำนวนสามจำนวน ====
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
แสดงตัวอย่างการเขียนใน 8 รูปแบบ (ถ้าไม่ได้สอน expression ที่ใช้ ?: คงไม่ต้องยกตัวอย่างรูปแบบที่ 7/8)
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
namespace max_of_3_many_versions_modular
 +
{
 +
class Program
 +
{
 +
public static void Main(string[] args)
 +
{
 +
Console.WriteLine("Hello World! We'll find the max of 3 integers.");
 +
 +
// read 3 integers
 +
int x = readInt("Enter 1st integer: ");
 +
int y = readInt("Enter 2nd integer: ");
 +
int z = readInt("Enter 3rd integer: ");
 +
 +
Console.WriteLine("version 1: max = {0}", max3ver1(x, y, z));
 +
Console.WriteLine("version 2: max = {0}", max3ver2(x, y, z));
 +
Console.WriteLine("version 3: max = {0}", max3ver3(x, y, z));
 +
Console.WriteLine("version 4: max = {0}", max3ver4(x, y, z));
 +
Console.WriteLine("version 5: max = {0}", max3ver5(x, y, z));
 +
Console.WriteLine("version 6: max = {0}", max3ver6(x, y, z));
 +
Console.WriteLine("version 7: max = {0}", max3ver7(x, y, z));
 +
Console.WriteLine("version 8: max = {0}", max3ver8(x, y, z));
 +
 +
Console.ReadKey(true);
 +
}
 +
 +
static int readInt(string prompt)
 +
{
 +
Console.Write(prompt);
 +
return int.Parse(Console.ReadLine());
 +
}
 +
 +
// version 1: (if without else)
 +
// exactly 6 comparisons in all cases
 +
static int max3ver1(int a, int b, int c)
 +
{
 +
int max = int.MinValue; // to please the compiler
 +
if (a >= b && a >= c)
 +
max = a;
 +
if (b >= a && b >= c)
 +
max = b;
 +
if (c >= a && c >= b)
 +
max = c;
 +
return max;
 +
}
 +
 +
// version 2: (if without else)
 +
// a little more efficient than version 1
 +
// performs 2 to 6 comparisons depending on inputs
 +
static int max3ver2(int a, int b, int c)
 +
{
 +
if (a >= b && a >= c)
 +
return a;
 +
if (b >= a && b >= c)
 +
return b;
 +
if (c >= a && c >= b)
 +
return c;
 +
return int.MinValue; // to please the compiler
 +
}
 +
 +
// version 3: (nested if-else is used)
 +
// still more efficient than version 2
 +
// only 2 or 3 comparisons depending on inputs
 +
static int max3ver3(int a, int b, int c)
 +
{
 +
if (a >= b && a >= c) // try a
 +
return a;
 +
else // a is not the max
 +
if (b > c)
 +
return b;
 +
else
 +
return c;
 +
}
 +
 +
// version 4: (nested if-else is used)
 +
// a little more efficient than version 3
 +
// exactly 2 comparisons in all cases
 +
static int max3ver4(int a, int b, int c)
 +
{
 +
if (a > b)
 +
// b is not the max
 +
if (a > c)
 +
return a;
 +
else
 +
return c;
 +
else // a is not the max
 +
if (b > c)
 +
return b;
 +
else
 +
return c;
 +
}
 +
 +
// version 5: (no nested if, just a sequence of if's)
 +
// exactly 2 comparisons in all cases
 +
// not more efficient than version 4
 +
// but more easily extended to 4 or more integers
 +
static int max3ver5(int a, int b, int c)
 +
{
 +
int max;
 +
if (a > b)
 +
max = a;
 +
else
 +
max = b;
 +
if (c > max)
 +
max = c;
 +
return max;
 +
}
 +
 +
// version 6: a cute, enlightening, climactic, lazy-programming version
 +
// This version is probably hailed by Lao zi as well as the Jedi order
 +
// By the way, it actually has exactly the same logic as version 5.
 +
static int max3ver6(int a, int b, int c)
 +
{
 +
return Math.Max(Math.Max(a,b),c);
 +
}
 +
 +
// version 7: a non-readable C-style version (lol ha ha ha)
 +
// However this version actually has exactly the same logic as version 5 and 6.
 +
static int max3ver7(int a, int b, int c)
 +
{
 +
int max;
 +
if (c > (max=a>b?a:b))
 +
    max = c;
 +
return max;
 +
}
 +
 +
// version 8: even less readable, uglily elegant version (can't laugh now)
 +
// We are entering the dark side here.
 +
// Nevertheless this version still has exactly the same logic as version 5, 6, and 7.
 +
static int max3ver8(int a, int b, int c)
 +
{
 +
int max;
 +
return (max=a>b?a:b) > c ? max:c;
 +
}
 +
 +
}
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
  
 
== โครงสร้างคำสั่งแบบวนซ้ำ ==
 
== โครงสร้างคำสั่งแบบวนซ้ำ ==
แถว 298: แถว 1,009:
 
== โครงสร้างคำสั่งแบบวนซ้ำและอาร์เรย์ 1 มิติ ==
 
== โครงสร้างคำสั่งแบบวนซ้ำและอาร์เรย์ 1 มิติ ==
 
: ''Notes: for-loop, ใช้ flow chart ไฟล์อินพุต''
 
: ''Notes: for-loop, ใช้ flow chart ไฟล์อินพุต''
 +
=== ตัวอย่างโปรแกรม ===
 +
==== คำนวณผลรวมของจำนวนเต็ม 5 จำนวน (ไม่ใช้ array) ====
 +
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
สองตัวอย่างแรกจะใช้เพื่อแสดงความจำเป็นของการใช้อาร์เรย์
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class MainClass
 +
{
 +
    static int ReadInt()
 +
    {
 +
        return int.Parse (Console.ReadLine ());
 +
    }
 +
 +
    public static void Main (string[] args)
 +
    {
 +
        int x1, x2, x3, x4, x5;
 +
 +
        x1 = ReadInt ();
 +
        x2 = ReadInt ();
 +
        x3 = ReadInt ();
 +
        x4 = ReadInt ();
 +
        x5 = ReadInt ();
 +
 +
        int sum = x1 + x2 + x3 + x4 + x5;
 +
 +
        Console.WriteLine ("Total = {0}", sum);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
==== คำนวณผลรวมของจำนวนเต็ม 10 จำนวน (ไม่ใช้ array) ====
 +
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
เอาโปรแกรมเดิมมาแก้ให้เป็น 10 ตัวแปร
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class MainClass
 +
{
 +
    static int ReadInt()
 +
    {
 +
        return int.Parse (Console.ReadLine ());
 +
    }
 +
 +
    public static void Main (string[] args)
 +
    {
 +
        int x1, x2, x3, x4, x5, x6, x7, x8, x9, x10;
 +
 +
        x1 = ReadInt ();
 +
        x2 = ReadInt ();
 +
        x3 = ReadInt ();
 +
        x4 = ReadInt ();
 +
        x5 = ReadInt ();
 +
        x6 = ReadInt ();
 +
        x7 = ReadInt ();
 +
        x8 = ReadInt ();
 +
        x9 = ReadInt ();
 +
        x10 = ReadInt ();
 +
 +
        int sum = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10;
 +
 +
        Console.WriteLine ("Total = {0}", sum);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
==== คำนวณผลรวมของจำนวนเต็ม 10 จำนวน (ใช้ array + while loop) ====
 +
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ประกาศตัวแปรแบบ array และคำนวณผลรวม
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class MainClass
 +
{
 +
    static int ReadInt()
 +
    {
 +
        return int.Parse (Console.ReadLine ());
 +
    }
 +
 +
    public static void Main (string[] args)
 +
    {
 +
        int[] x = new int[10];
 +
 +
        int i = 0;
 +
        while (i < 10) {
 +
            x [i] = ReadInt ();
 +
            i++;
 +
        }
 +
 +
        int sum = 0;
 +
        i = 0;
 +
        while (i < 10) {
 +
            sum += x[i];
 +
            i++;
 +
        }
 +
 +
        Console.WriteLine ("Total = {0}", sum);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
==== คำนวณผลรวมของจำนวนเต็ม 10 จำนวน (ใช้ array + for loop) ====
 +
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ประกาศตัวแปรแบบ array และคำนวณผลรวม
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class MainClass
 +
{
 +
    static int ReadInt()
 +
    {
 +
        return int.Parse (Console.ReadLine ());
 +
    }
 +
 +
    public static void Main (string[] args)
 +
    {
 +
        int[] x = new int[10];
 +
 +
        for(int i = 0; i < 10; i++) {
 +
            x [i] = ReadInt ();
 +
        }
 +
 +
        int sum = 0;
 +
        for(int i=0; i < 10; i++) {
 +
            sum += x[i];
 +
        }
 +
 +
        Console.WriteLine ("Total = {0}", sum);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
==== คำนวณผลรวมของจำนวนเต็ม 10 จำนวน (ใช้ array + for loop + โปรแกรมย่อย) ====
 +
ถ้ายังไม่สอนตอนนี้ ก็ข้ามตัวอย่างนี้ไปก่อนได้ครับ
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
ประกาศตัวแปรแบบ array และคำนวณผลรวม โดยแบ่งงานเป็นส่วน ๆ ด้วยโปรแกรมย่อย
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
 +
class MainClass
 +
{
 +
    static int ReadInt()
 +
    {
 +
        return int.Parse (Console.ReadLine ());
 +
    }
 +
 +
    static int [] ReadArray(int size)
 +
    {
 +
        int[] x = new int[size];
 +
        for (int i = 0; i < size; i++) {
 +
            x [i] = ReadInt ();
 +
        }
 +
        return x;
 +
    }
 +
 +
    static int FindSum(int[] x, int size)
 +
    {
 +
        int sum = 0;
 +
        for (int i = 0; i < size; i++) {
 +
            sum += x [i];
 +
        }
 +
        return sum;
 +
    }
 +
 +
    public static void Main (string[] args)
 +
    {
 +
        int[] x = ReadArray (10);
 +
        int sum = FindSum(x, 10);
 +
        Console.WriteLine ("Total = {0}", sum);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
  
 
== โครงสร้างคำสั่งแบบวนซ้ำหลายชั้น ==
 
== โครงสร้างคำสั่งแบบวนซ้ำหลายชั้น ==

รุ่นแก้ไขปัจจุบันเมื่อ 16:16, 1 สิงหาคม 2559

ตัวอย่างโปรแกรมที่ควรเขียนได้และเข้าใจเมื่อเรียนเนื้อหาแต่ละส่วน

เนื้อหา

แนะนำคอมพิวเตอร์และการโปรแกรม

  • 1 คาบ
  • องค์ประกอบคอมพิวเตอร์เบื้องต้น
    • ฮาร์ดแวร์ ซอฟต์แวร์ และระบบปฏิบัติการ
  • การแทนข้อมูล
  • ระบบเลขฐาน
  • เครือข่ายคอมพิวเตอร์และอินเทอร์เน็ต (?)
  • มโนทัศน์การโปรแกรม
    • ภาษาระดับต่ำ
    • ภาษาระดับสูง
    • การแปลภาษา
    • ขั้นตอนวิธีและการแตกปัญหาเป็นปัญหาย่อย
    • โฟลว์ชาร์ท

ตัวอย่างโปรแกรม

โปรแกรมการกินข้าว

1. ขณะที่ ข้าวในจานยังไม่หมด ให้ทำดังนี้
1.1    ถ้า ยังกินไหว ให้ทำดังนี้
1.1.1      ตักข้าวเข้าปาก
1.2    ถ้าไม่เช่นนั้น
1.2.1      เลิกกิน
2. ถ้า ยังไม่อิ่ม และ เงินยังไม่หมด ให้ทำดังนี้
2.1    ซื้อข้าวอีกจาน
2.2    กลับไปทำข้อ 1

ตัวแปร นิพจน์ โปรแกรมเชิงลำดับอย่างง่าย อินพุต/เอาท์พุต

  • 1 คาบ
  • แนะนำ data type เท่าที่จำเป็น
    • int สำหรับจำนวนเต็ม (ไม่ต้องมี short หรือ byte)
    • double สำหรับทศนิยม (ไม่ต้องมี float)
    • char
    • string
  • ตัวดำเนินการพื้นฐาน +, -, *, /, %
    • ลำดับความสำคัญ และวงเล็บ
    • ยังไม่ต้องสอน ++, --, += และ -= ในตอนนี้
  • อาศัย interactive shell ในการแสดงลำดับการคำนวณ การใช้ตัวแปร และการนำเอาลำดับเหล่านี้มารวมกันเป็นโปรแกรมเพื่อทำงานทีเดียว
  •  ??การประกาศตัวแปรด้วยคีย์เวิร์ด var
  • การใช้ Console.ReadLine() และ Console.WriteLine() เมื่อเริ่มนำมาเขียนเป็นโปรแกรม
  • แทรกเกร็ดเรื่อง formatting โดยใช้ Console.Write() ไปเรื่อย ๆ
  •  ?? ไม่ต้องสอนการประกาศตัวแปรแบบ const
  • ไม่ต้องสอน ConvertTo() และการทำ type casting ระหว่างตัวเลขเป็นตัวอักษร (เช่น (int)'A' หรือ (char)65)

ลำดับโปรแกรมที่พัฒนา

โจทย์: คำนวณบิลค่าอาหาร

  • หัวข้อการเรียนรู้
    • การคำนวณแบบบวกเลขอย่างง่าย
    • นิพจน์ทางคณิตศาสตร์ (math expression)
    • การส่งออกเอาท์พุท
    • ความหมายของคำสั่ง (statement)
ขยาย

ตัวอย่างโปรแกรม

ขยาย

ตัวอย่างโปรแกรมที่ปรับเอาท์พุทให้เหมาะสมขึ้น

โจทย์: บิลอาหารพร้อมส่วนลด 20%

  • หัวข้อการเรียนรู้
    • ลำดับความสำคัญของตัวดำเนินการ
ขยาย

ตัวอย่างโปรแกรมที่ผิด (การคูณเกิดขึ้นกับ 33 เท่านั้น)

ขยาย

ตัวอย่างโปรแกรมที่ถูก (ใช้วงเล็บครอบนิพจน์บวกทั้งหมด)

โจทย์: สั่งอาหารซ้ำกันหลายเมนู

  • หัวข้อการเรียนรู้
    • ใช้วงเล็บช่วยให้เขียนโปรแกรมไม่กำกวม แม้ไม่ทำให้การทำงานเปลี่ยน
ขยาย

ตัวอย่างโปรแกรม (เขียนโดยอาศัยลำดับความสำคัญ * ที่มาก่อน +)

ขยาย

ตัวอย่างโปรแกรม (เพิ่มวงเล็บเพื่อความชัดเจน)

โจทย์: แชร์ค่าอาหาร

  • หัวข้อการเรียนรู้
    • การหารแบบจำนวนเต็ม และการหารแบบทศนิยม
ขยาย

ตัวอย่างโปรแกรมที่ผิด (ใช้การหารแบบจำนวนเต็ม)

ขยาย

ตัวอย่างโปรแกรมที่ถูก (เปลี่ยนให้เป็นการหารแบบทศนิยม)

ขยาย

ตัวอย่างโปรแกรมที่ปรับปรุงแล้ว (ใช้ตัวแปรเก็บค่านิพจน์ที่คำนวณซ้ำซ้อน)

โจทย์: แชร์ค่าอาหารพร้อมให้ทิป

  • หัวข้อการเรียนรู้
    • การหารเอาเศษและตัวดำเนินการ %
ขยาย

ตัวอย่างโปรแกรม

โจทย์: แปลงอุณหภูมิจากองศาเซลเซียสเป็นเคลวิน

  • หัวข้อการเรียนรู้
    • การอ่านอินพุทด้วย Console.ReadLine()
    • ชนิดข้อมูลแบบจำนวนและสตริง
    • ตัวดำเนินการ + ที่ให้พฤติกรรมแตกต่างกันเมื่อใช้กับจำนวนและสตริง
    • การใช้เมท็อด Parse() เพื่อเปลี่ยนสตริงเป็นจำนวน
    • การฟอร์แมตเอาท์พุท
ขยาย

ตัวอย่างโปรแกรมที่ผิด (บวกสตริงเข้ากับจำนวน)

ขยาย

ตัวอย่างโปรแกรมที่ถูก #1

ขยาย

ตัวอย่างโปรแกรมที่ถูก #2 (ฟอร์แมตข้อความให้เหมาะสม)

โจทย์: แปลงอุณหภูมิจากองศาเซลเซียสเป็นหน่วยอื่น ๆ

  • หัวข้อการเรียนรู้
    • การวิเคราะห์โจทย์
    • การฟอร์แมตตัวเลขให้มีจำนวน​ทศนิยมที่กำหนด
ขยาย

ตัวอย่างโปรแกรม

โจทย์: คำนวณเงินฝากธนาคาร

  • หัวข้อการเรียนรู้
    • การวิเคราะห์โจทย์
    • การใช้ไลบรารี Math
ขยาย

ตัวอย่างโปรแกรม

โปรแกรมย่อยและไลบรารี

  • เป้าหมาย: การใช้โปรแกรมย่อยเพื่อการแบ่งปัญหาเป็นปัญหาย่อย และเพื่อให้โปรแกรมอ่านเข้าใจง่าย (ไม่ใช่เพื่อทำให้โปรแกรมสั้นลง)
  • 1 คาบ
  • การเรียกใช้ฟังก์ชันในคลาส Math และทบทวนการเรียกใช้ฟังก์ชันที่เคยทำมาแล้ว (ReadLine, WriteLine, Parse ฯลฯ)
  • การสร้างโปรแกรมย่อยและฟังก์ชันขึ้นมาด้วยตนเองเพื่อคำนวณสูตรที่ไม่มีให้ในไลบรารี
  • พารามิเตอร์และการส่งค่า
    • ความหมายของพารามิเตอร์และอาร์กิวเมนต์
    • ครอบคลุมเฉพาะ pass by value
    •  ?? การกำหนดพารามิเตอร์ด้วย keyword argument ตัวอย่างเช่น
ขยาย

คลิก "ขยาย" เพื่อดูตัวอย่างโปรแกรม

  • สโคปของตัวแปร

ลำดับของโปรแกรมที่จะพัฒนา

1. โปรแกรมคำนวณพื้นที่วงกลม 1

ขยาย

ตัวอย่างแรกสุดที่แสดงการเรียกโปรแกรมย่อยโดยไม่ต้องส่งพารามิเตอร์ใดๆ เพื่อแสดง top-down design ขั้นพื้นฐาน

2. โปรแกรมคำนวณพื้นที่วงกลม 2

ขยาย

ทำงานเดียวกับโปรแกรมแรก แต่มีการเรียกโปรแกรมย่อยที่มีพารามิเตอร์แบบ pass by value และรีเทิร์นผลลัพธ์ แสดงการออกแบบโปรแกรมแบบ modular และ stepwise refinement มากยิ่งขึ้น

3. โปรแกรมคำนวณค่าเฉลี่ยของตัวแปร 3 ตัว (version 2)

ขยาย

หาค่าเฉลี่ยของจำนวนเต็มสามตัว เป็นโปรแกรมที่ปรับมาจากโปรแกรมที่ Main ทำทุกอย่าง เป็นอีกตัวอย่างที่แสดงการใช้โปรแกรมย่อย

4. โปรแกรมคำนวณค่าเฉลี่ยของตัวแปร 3 ตัว (version 3)

ขยาย

หาค่าเฉลี่ยของจำนวนเต็มสามตัว ใช้ out พารามิเตอร์ในการส่งค่ากลับ

5. โปรแกรมคำนวณพื้นที่สี่เหลี่ยมคางหมู

ขยาย

ปรับแก้จากตัวอย่างการคำนวณค่าเฉลี่ย แสดงตัวอย่างการนำโปรแกรมที่แบ่งโครงสร้างที่ดีไว้แล้วมาปรับแก้

โปรแกรมตัวอย่างอื่น ๆ

หาพื้นที่สี่เหลี่ยม

ขยาย

ตัวอย่างแสดงการแยกส่วนของการคำนวณเป็นโปรแกรมย่อย

ตัวอย่าง 2 (มีการใช้หลาย method)

TODO

ตัวอย่าง 3 (มีการใช้หลาย method, ใน method มีการเรียนใช้ method อื่น)

TODO

นิพจน์เชิงตรรกและโครงสร้างคำสั่งแบบทางเลือก

  • ทุกตัวอย่างมีการใช้โปรแกรมย่อยเสมอ

ตัวอย่างโปรแกรม

แก้สมการกำลังสอง

เป็นตัวอย่างของการเขียนโปรแกรมแก้สมการกำลังสอง โดยพิจารณาแบบ top-down

ส่วนแรกเป็นโปรแกรมหลักที่รับสัมประสิทธิ์ a,b,c และเรียกใช้โปรแกรมย่อยในการประมวลผล

ขยาย

เมท็อด Main

เริ่มเขียนส่วนรับอินพุต

ขยาย

เขียนเมท็อด ReadCoefficients

ขยาย

ที่ใช้เมท็อด ReadDouble

เมื่อเขียนถึงจุดนี้ควรจะทดสอบโปรแกรมเสียก่อน อาจจะต้อง comment SolveAndOutput ไปก่อน และเพิ่มคำสั่งในการพิมพ์ค่า a b c ออกมา

โครงหลักของการแก้สมการ SolveAndOutput มีเงื่อนไขเกี่ยวกับค่า ที่ต้องรวมการแสดงผลไว้ด้วยเพราะว่าแสดงผลได้สองแบบ (และเรายังคืนค่าเป็น complex ไม่เป็น) ซึ่งถ้าแบ่งงานอีกแบบคือให้โปรแกรมย่อยคืนค่าเป็นจำนวนเชิงซ้อนตลอดเวลา ก็จะเขียนได้สะอาดกว่านี้ ในตอนแรกเราจะเขียนแค่ส่วนหาคำตอบกรณีที่เป็นจำนวนจริงก่อนเพื่อให้สามารถทดสอบโปรแกรมได้เมื่อทำส่วนย่อยแรกนี้เสร็จ

ขยาย

เขียนในเมท็อด SolveAndOutput

ส่วนตรวจสอบว่ามีคำตอบเป็นจำนวนจริง (ไม่ได้ตรวจว่ามีคำตอบเดียวหรือเปล่า --- สามารถเก็บไปเป็นการบ้านที่ทำในแลบได้)

ขยาย

ในการตรวจสอบจะคำนวณ inner term ด้วยเมท็อด CalculateInnerTerm ก่อน จากนั้นจึงค่อนตรวจสอบค่า

จากนั้นจึงเมท็อดที่เหลือในการหาคำตอบกรณีคำตอบเป็นจำนวนจริง

ขยาย

มีเมท็อด FindRealSolutions และ OutputRealSolutions

เมื่อเขียนถึงตรงนี้ควรจะทดสอบโปรแกรมก่อน (ไม่ควรเขียนต่อ) เราควรเน้นให้นิสิตทดสอบโปรแกรมเป็นระยะ ๆ ไม่ใช่เขียนรวดเดียวแล้วทดสอบทีเดียว

จากนั้นเราจะเพิ่มส่วนคำนวณคำตอบในกรณีที่คำตอบเป็น complex ในเมท็อด SolveAndOutput (ในส่วน else)

ขยาย

เขียนเพิ่มเติมในเมท็อด SolveAndOutput

โค้ดที่เหลือกรณีทีคำตอบเป็น complex (และไม่เป็นจำนวนจริง) เขียนคล้าย ๆ เดิม แต่ในการคืนคำตอบจะคืนสองคำตอบแยกเป็นส่วน real part และ imaginary part เราเลือกที่จะคำนวณคำตอบสองคำตอบให้เรียบร้อยใน FindComplexSolutions เลย แม้ว่าจะดูว่าเป็นการทำงานซ้ำซ้อน (เพราะว่า real part เท่ากัน) แต่เนื่องจากเราไม่ต้องการให้เงื่อนไขและรายละเอียดดังกล่าวไปปะปนอยู่ในส่วนแสดงผลลัพธ์

ขยาย

ด้านล่างเป็นเมท็อด FindComplexSolutions และ OutputComplexSolutions

คิดค่าส่งไปรษณีย์

ขยาย

แสดงการแบ่งงานเป็นหลายกรณี

โครงสร้างคำสั่งแบบทางเลือกหลายชั้น

  • ไม่ต้องสอน switch/case
  • ใช้ flow-chart และตัวอย่างเยอะ ๆ

ตัวอย่างโปรแกรม

คำนวณค่ามากที่สุดของจำนวนสามจำนวน

ขยาย

แสดงตัวอย่างการเขียนใน 8 รูปแบบ (ถ้าไม่ได้สอน expression ที่ใช้ ?: คงไม่ต้องยกตัวอย่างรูปแบบที่ 7/8)

โครงสร้างคำสั่งแบบวนซ้ำ

Notes: while, do-while ใช้ flow-chart ช่วย, แทรก ++/-- ณ จุดนี้

โครงสร้างคำสั่งแบบวนซ้ำและอาร์เรย์ 1 มิติ

Notes: for-loop, ใช้ flow chart ไฟล์อินพุต

ตัวอย่างโปรแกรม

คำนวณผลรวมของจำนวนเต็ม 5 จำนวน (ไม่ใช้ array)

ขยาย

สองตัวอย่างแรกจะใช้เพื่อแสดงความจำเป็นของการใช้อาร์เรย์

คำนวณผลรวมของจำนวนเต็ม 10 จำนวน (ไม่ใช้ array)

ขยาย

เอาโปรแกรมเดิมมาแก้ให้เป็น 10 ตัวแปร

คำนวณผลรวมของจำนวนเต็ม 10 จำนวน (ใช้ array + while loop)

ขยาย

ประกาศตัวแปรแบบ array และคำนวณผลรวม

คำนวณผลรวมของจำนวนเต็ม 10 จำนวน (ใช้ array + for loop)

ขยาย

ประกาศตัวแปรแบบ array และคำนวณผลรวม

คำนวณผลรวมของจำนวนเต็ม 10 จำนวน (ใช้ array + for loop + โปรแกรมย่อย)

ถ้ายังไม่สอนตอนนี้ ก็ข้ามตัวอย่างนี้ไปก่อนได้ครับ

ขยาย

ประกาศตัวแปรแบบ array และคำนวณผลรวม โดยแบ่งงานเป็นส่วน ๆ ด้วยโปรแกรมย่อย

โครงสร้างคำสั่งแบบวนซ้ำหลายชั้น

Notes: continue และ break

โปรแกรมย่อยขั้นสูง

Notes: เช่น pass by reference, ส่ง array เข้าเมท็อด, string processing

อาเรย์หลายมิติ

Notes: นำเข้าข้อมูลจาก csv

การแก้โจทย์เชิงประยุกต์

Notes: เสริมเนื้อหาเช่น GUI