Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C#...

26
พื นฐานการเขียนโปรแกรม วีระยุทธ คุณรัตนสิริ

Transcript of Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C#...

Page 1: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

พ้ืนฐานการเขียนโปรแกรมวี ระยุทธ คุณ รัตนสิ ริ

Page 2: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

ภาษา C#รองรับการโปรแกรมเชิงวตัถุ (Object-Oriented Programming –OOP)

พฒันาโดยบริษทัไมโครซอฟต ์เพื่อเป็นส่วนหน่ึงของสถาปัตยกรรม .NET

มีโครงสร้างคลา้ยคลึงกบัภาษา C, C++ และ Java

ท างานร่วมกบัภาษาอ่ืนในสถาปัตยกรรม .NET ไดง่้าย

Page 3: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

สถาปัตยกรรม .NET

Page 4: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

การสร้างและคอมไพล์โปรแกรม◦ ใช้โปรแกรมประเภท Integrated Development Environment

(IDE) ที่รวม Text Editor และจัดการเรื่องการคอมไพล์ให้◦ เช่น Microsoft Visual C#

Page 5: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

ประเภทของแอพลิเคชันConsole Application◦ แสดงผลในรูปตัวอักษร◦ รับข้อมูลผ่านแป้นพิมพ์เพียงอย่างเดียว

Windows Application◦ ใช้ได้ทั้งแป้นพิมพ์และเมาส์◦ แสดงผลในรูปกราฟิคส์

Page 6: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

พื้นฐานการเขียนโปรแกรมด้วย C#

Page 7: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

เนื้อหาโครงสร้างโปรแกรม

ชนิดข้อมูล

ตัวแปร

นิพจน์

ค าสั่งแสดงผล

Page 8: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

โปรแกรมภาษา C#พิจารณาโปรแกรมต่อไปนี้

namespace HelloW {

class HelloWClass {

static void Main () {

System.Console.WriteLine("Hello World!");

System.Console.ReadLine();

}

}

}

Page 9: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

โปรแกรมภาษา C#ตัวอักษรเล็ก/ใหญ่มีความส าคัญ

ค าสั่งทุกค าสั่งต้องปิดท้ายด้วยเซมิโคลอน (;)

จ านวนช่องว่าง (ทั้งแนวตั้งแนวนอน) ไม่มีผลต่อการท างาน

ใช้เครื่องหมายปีกกา {} จับกลุ่มค าสั่ง

ข้อความที่อยู่ระหว่าง /* */ หรือหลัง // ถือเป็นคอมเม้นต์ ซึ่งไม่มีผลต่อการท างานของโปรแกรม

Page 10: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

โครงสร้างของโปรแกรมจุดเริ่มต้นของโปรแกรมจะอยู่ที:่

◦ เรียกว่าเมท็อด Main◦ เมท็อดต้องอยู่ภายใต้คลาส (class)◦ คลาสอาจอยู่ภายใต้เนมสเปส (namespace) หรือไม่ก็ได้

static void Main ()

{

... starting point ...

}

Page 11: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

โครงสร้างของโปรแกรมคลาสเปรียบเสมือนกล่องบรรจุเมท็อดหลาย ๆ เมท็อด

เนมสเปสเปรียบเสมือนกล่องบรรจุคลาสหลาย ๆ คลาส◦ เนมสเปสอาจอยู่ภายใต้เนมสเปสอื่นได้อีก

โปรแกรมหนึ่ง ๆ อาจประกอบด้วยหลายเนมสเปส หรืออาจไม่อยู่ในเนมสเปสใด ๆ เลยก็ได้

method1

method2

Namespace

Class

Page 12: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

โครงสร้างของโปรแกรม

ส าหรับโปรแกรมอย่างง่าย◦ โปรแกรมประกอบด้วยคลาสเพียงคลาสเดยีว◦ โปรแกรมอาจประกอบดว้ยเมท็อด Main เพียงเมท็อดเดียว

namespace HelloW {

class HelloWClass {

static void Main () {

System.Console.WriteLine("Hello World!");

System.Console.ReadLine();

}

}

}

Page 13: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

ค าสั่ง (Statement)ค าสั่งใช้อธิบายการท างานของโปรแกรมในแต่ละขั้นตอน

เมท็อดถูกสร้างขึ้นจากค าสั่งตั้งแต่หนึ่งถึงหลายค าสั่ง

class Hello {

static void Main () {

System.Console.WriteLine("Hello World!");

System.Console.ReadLine();

}

}

Statement#1

Statement#2

Page 14: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

ค าสั่ง (Statement)

method1

method2NamespaceClass

Statement#1

Statement#2

:

namespace HelloW {

class HelloWClass {

static void Main () {

System.Console.WriteLine("Hello World!");

System.Console.ReadLine();

}

}

}

Page 15: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

ค าสั่ง (Statement)

method1

method2NamespaceClass

Statement#1

Statement#2

:

Page 16: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

การตั้งชื่อทั้งเมท็อด คลาส เนมสเปส ต้องมีการตั้งชื่อก ากับในภาษา C# มีกฎการตั้งชื่อดังนี้◦ ขึ้นต้นด้วยอักขระภาษาอังกฤษ (A-Z, a-z) หรือตัวขีดเส้นใต้ (_)◦ ส่วนที่เหลือประกอบดว้ยอกัขระภาษาอังกฤษ ตัวเลข หรือตัวขีดเส้นใต้◦ ความยาวสูงสุด 63 ตัวอักษร◦ ต้องไม่ซ้ ากับค าสงวน (reserved words) เช่น class, namespace

ตัวอย่างชื่อที่ถูกกฎ◦ hEllO, E3_32ab, X_x_X022

ตัวอย่างชื่อที่ผิดกฎ◦ 32ABC, A.2, C#Program, while

Page 17: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

ค าสงวนค าเหล่านี้ห้ามน าไปใช้เป็นชื่อในภาษา C#

Page 18: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

ตัวแปร (Variable)ตัวแปรใช้ส าหรับเก็บค่าของข้อมูล

การประกาศตัวแปร

การก าหนดค่าให้ตัวแปร

ตัวอย่าง

<type> <name>;

<name> = <expression>;

int width, height;

int area;

width = 10; height = 20;

area = width * height;

Page 19: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

ชนิด ขนาด ความหมาย ช่วง

bool 1 byte ค่าความจริง (จริง-เทจ็) true / false

char 2 byte อกัขระโดด character code 0...65535

sbyte 1 byte จ านวนเตม็ -128...127

byte 1 byte จ านวนเตม็ไม่ติดลบ 0...255

short 2 bytes จ านวนเตม็ -32,768...32,767

ushort 2 bytes จ านวนเตม็ไม่ติดลบ 0...65535

int 4 bytes จ านวนเตม็ -2.1 x 109...2.1 x 109

uint 4 bytes จ านวนเตม็ไม่ติดลบ 0...4.3 x 109

long 8 bytes จ านวนเตม็ -9.2 x 1018...9.2 x 1018

ulong 8 bytes จ านวนเตม็ไม่ติดลบ 0...1.8 x 1019

float 4 bytes จ านวนจริง ±1.5x10-45...±3.4x1038

double 8 bytes จ านวนจริงความละเอยีดสองเท่า ±5.0x10-324...±1.7x10308

decimal 16 bytes จ านวนจริงความละเอยีดสงู ±1.0x10--28...±7.9x1028

string N/A สายอกัขระ N/A

Page 20: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

การก าหนดค่าให้ตัวแปรเราสามารถก าหนดค่าเริ่มต้นให้กับตัวแปรได้พร้อมกับการประกาศตัวแปรนั้น ๆ ได้ทันที

ตัวอย่างint width = 10, height = 20;

int area;

area = width * height;

Page 21: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

นิพจน์ (Expressions)นิพจน์เป็นส่วนหนึ่งของค าสั่งที่ถูกประเมินเป็นค่าได้◦ อาจเป็นค่าโดดหรือประกอบขึ้นมาจากนิพจน์ที่เล็กกว่า

ตัวอย่างของนิพจน์ที่เป็นค่าโดด◦ ตัวเลข 3212, 3.1415

◦ อักขระ 'C'

◦ ค่าความจริง true or false

◦ ข้อความ "Hello, World"

◦ ตัวแปรหรือค่าคงที่ x, myName

Page 22: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

นิพจน์ทางคณิตศาสตร์นิพจน์ทางคณิตศาสตร์ (arithmetic expression) คือนิพจน์ที่ถูกตีความเป็นค่าจ านวนนิพจน์ทางคณิตศาสตร์สามารถน ามาประกอบเป็นนิพจน์ที่ซับซ้อนขึ้นได้โดยอาศัยตัวด าเนินการทางคณิตศาสตร์ (arithmetic operators)◦ +, -, *, /◦ % (ให้ค่าเศษจากการหาร)

ตัวอย่าง◦ 11+5 16◦ 11/2 5◦ 11.0/2 5.5◦ 11%2 1◦ 5.0%2.2 0.6

Page 23: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

ล าดับการค านวณ (Precedence Rules)1. ( )

2. *, / , %

3. +, –

4. ซ้ายไปขวาint Width,Height;

Width = 10*5+(16 * 12)/5;

Height = (16+5)+20%2;

Page 24: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

การหารแบบจ านวนเต็มการหารนิพจน์ที่เป็นจ านวนเต็มสองจ านวนให้ผลลัพธ์เป็นจ านวนเต็มเสมอ◦ เศษจะถูกปัดทิ้ง

หากตัวตั้งและ/หรือตัวหารเป็นจ านวนทศนิยม (float หรือ double) การหารจะค านวณทศนิยมให้

int i1 = 10, i2 = 8;

double f1 = 10, f2 = 8;

double r1 = i1/i2; // r1 = 1

double r2 = f1/f2; // r2 = 1.25

double r3 = f1/i2; // r3 = 1.25

double r4 = i1/f2; // r4 = 1.25

double r1 = 10/8; // r1 = 1

double r2 = 10.0/8.0; // r2 = 1.25

double r3 = 10.0/8; // r3 = 1.25

double r4 = 10/8.0; // r4 = 1.25

Page 25: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

ค าสั่ง usingการใช้ค าสั่ง using <ชื่อเนมสเปส> ที่ต้นโปรแกรมเป็นการระบุว่าเราต้องการเรียกใช้งานคลาสในเนมสเปสนั้น ๆ

ตัวอย่าง: คลาส Console อยู่ในเนมสเปส System

using System;

class Hello {

static void Main () {

Console.WriteLine("Hello World!");

Console.ReadLine();

}

}

class Hello {

static void Main () {

System.Console.WriteLine("Hello World!");

System.Console.ReadLine();

}

}

Page 26: Weerayuth Khunrattanasiri - Introduction to Digital Logic · 2016-09-20 · ภาษา C# รองรับการโปรแกรมเชิงวัตถุ (Object-Oriented

ค าสั่งส าหรับแสดงผลทางหน้าจอใช้ค าสั่ง Write หรือ WriteLine ซึ่งอยู่ในคลาส Console(ซึ่งอยู่ในเนมสเปส System อีกทีหนึ่ง)

การใช้งานพื้นฐาน:

การใช้งานชั้นสูง:

Console.WriteLine(”Size {0}x{1}”, width, height);

double salary=12000;

Console.WriteLine("My salary is {0:f2}.", salary);

Console.WriteLine("Hello");

Console.WriteLine(area);