ผลต่างระหว่างรุ่นของ "01204111 model codes"
Jittat (คุย | มีส่วนร่วม) |
Jittat (คุย | มีส่วนร่วม) |
||
แถว 298: | แถว 298: | ||
== นิพจน์เชิงตรรกและโครงสร้างคำสั่งแบบทางเลือก == | == นิพจน์เชิงตรรกและโครงสร้างคำสั่งแบบทางเลือก == | ||
* ทุกตัวอย่างมีการใช้โปรแกรมย่อยเสมอ | * ทุกตัวอย่างมีการใช้โปรแกรมย่อยเสมอ | ||
− | === | + | === ตัวอย่างโปรแกรมอื่น ๆ === |
+ | ==== คำนวณค่ามากที่สุดของจำนวนสามจำนวน ==== | ||
+ | <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> | ||
+ | |||
==== คิดค่าส่งไปรษณีย์ ==== | ==== คิดค่าส่งไปรษณีย์ ==== | ||
<div class="toccolours mw-collapsible mw-collapsed"> | <div class="toccolours mw-collapsible mw-collapsed"> |
รุ่นแก้ไขเมื่อ 23:02, 20 มิถุนายน 2559
ตัวอย่างโปรแกรมที่ควรเขียนได้และเข้าใจเมื่อเรียนเนื้อหาแต่ละส่วน
เนื้อหา
- 1 แนะนำคอมพิวเตอร์และการโปรแกรม
- 2 ตัวแปร นิพจน์ โปรแกรมเชิงลำดับอย่างง่าย อินพุต/เอาท์พุต
- 3 โปรแกรมย่อยและไลบรารี
- 4 นิพจน์เชิงตรรกและโครงสร้างคำสั่งแบบทางเลือก
- 5 โครงสร้างคำสั่งแบบทางเลือกหลายชั้น
- 6 โครงสร้างคำสั่งแบบวนซ้ำ
- 7 โครงสร้างคำสั่งแบบวนซ้ำและอาร์เรย์ 1 มิติ
- 8 โครงสร้างคำสั่งแบบวนซ้ำหลายชั้น
- 9 โปรแกรมย่อยขั้นสูง
- 10 อาเรย์หลายมิติ
- 11 การแก้โจทย์เชิงประยุกต์
แนะนำคอมพิวเตอร์และการโปรแกรม
- 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)
โปรแกรมย่อยและไลบรารี
- เป้าหมาย: การใช้โปรแกรมย่อยเพื่อการแบ่งปัญหาเป็นปัญหาย่อย และเพื่อให้โปรแกรมอ่านเข้าใจง่าย (ไม่ใช่เพื่อทำให้โปรแกรมสั้นลง)
- 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
นิพจน์เชิงตรรกและโครงสร้างคำสั่งแบบทางเลือก
- ทุกตัวอย่างมีการใช้โปรแกรมย่อยเสมอ
ตัวอย่างโปรแกรมอื่น ๆ
คำนวณค่ามากที่สุดของจำนวนสามจำนวน
แสดงตัวอย่างการเขียนใน 8 รูปแบบ (ถ้าไม่ได้สอน expression ที่ใช้ ?: คงไม่ต้องยกตัวอย่างรูปแบบที่ 7/8)
คิดค่าส่งไปรษณีย์
แสดงการแบ่งงานเป็นหลายกรณี
โครงสร้างคำสั่งแบบทางเลือกหลายชั้น
- ไม่ต้องสอน switch/case
- ใช้ flow-chart และตัวอย่างเยอะ ๆ
โครงสร้างคำสั่งแบบวนซ้ำ
- Notes: while, do-while ใช้ flow-chart ช่วย, แทรก ++/-- ณ จุดนี้
โครงสร้างคำสั่งแบบวนซ้ำและอาร์เรย์ 1 มิติ
- Notes: for-loop, ใช้ flow chart ไฟล์อินพุต
โครงสร้างคำสั่งแบบวนซ้ำหลายชั้น
- Notes: continue และ break
โปรแกรมย่อยขั้นสูง
- Notes: เช่น pass by reference, ส่ง array เข้าเมท็อด, string processing
อาเรย์หลายมิติ
- Notes: นำเข้าข้อมูลจาก csv
การแก้โจทย์เชิงประยุกต์
- Notes: เสริมเนื้อหาเช่น GUI