chapter04
DESCRIPTION
หลักการเขียนโปรแกรมคอมพิวเตอร์เบื้องต้น โดยใช้ภาษา C# และใช้โปรแกรม RAPTOR ในการออกแบบ FlowchartTRANSCRIPT
![Page 1: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/1.jpg)
Company
LOGO
มหาวิ�ทยาลัยราชภัฏนครราชสี�มา
คณะวิ�ทยาการจัดการ
โปรแกรมคอมพิ�วิเตอร�ธุ รก�จั
บทท�" 4 Introduction to C#
Programming
![Page 2: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/2.jpg)
เน$%อหา
แนะน��ภ�ษ� C# ก�รร�บข้�อมู�ลเข้��และก�รแสดงผลล�พธ์� น�พจน�ท�งคณิ�ตศ�สตร� (Arithmetic
Expressions) พ"#นฐ�นก�รใช้� Raptor ส��หร�บออกแบบ
โปรแกรมู
![Page 3: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/3.jpg)
แนะน&าภัาษา C#
รองร�บก�รโปรแกรมูเช้�งวั�ตถุ, (Object-Oriented Programming – OOP)
พ�ฒน�โดยบร�ษ�ทไมูโครซอฟต� เพ"2อเป3นส4วันหน52งข้องสถุ�ป6ตยกรรมู .NET
มู7โครงสร��งคล��ยคล5งก�บภ�ษ� Java และ C++ ท��ง�นร4วัมูก�บภ�ษ�อ"2นในสถุ�ป6ตยกรรมู .NET ได�
ง4�ย
![Page 4: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/4.jpg)
สีถาป)ตยกรรม .NET
![Page 5: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/5.jpg)
การสีร*างแลัะคอมไพิลั�โปรแกรม
มู7หล�ยท�งเล"อก เข้7ยนโปรแกรมูด�วัย Text Editor และคอมูไพล�
ด�วัยโปรแกรมู csc.exe ใน C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319 (VS 2010 Express)
ใช้�โปรแกรมูประเภท Integrated Development Environment (IDE) ท72รวัมู Text Editor และจ�ดก�รเร"2องก�รคอมูไพล�ให� เช้4น Microsoft Visual C#
![Page 6: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/6.jpg)
แนะน&าภัาษา C#
ก�รพ�ฒน�แอพล�เคช้�2น (Application) เร7ยกวั4�ก�รสร��งโซล�ช้� 2น (solution)
โซล�ช้� 2น (solution) ประกอบด�วัยโปรเจค (project) ต�#งแต4หน52งโปรเจคหร"อมู�กกวั4�หน52งโปรเจค
VC# -> Console Application
![Page 7: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/7.jpg)
การสีร*าง New Project
![Page 8: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/8.jpg)
หน*าต-าง New Project
![Page 9: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/9.jpg)
โครงสีร*างโปรแกรมภัาษา C#
namespace Hello { class HelloClass { static void Main ()
{ System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } }}
namespace Hello { class HelloClass { static void Main ()
{ System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } }}
1Namespace2class
3start point
4code
![Page 10: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/10.jpg)
ค&าสี"งแสีดงผลัลัพิธุ�
Write และ WriteLine อย�4ภ�ยใน คล�สช้"2อ Console และเนมูสเปสช้"2อ
System ค��ส�2ง System.Console.Write( "Hello");
หร"อ Console.Write("Hello"); กรณิ7ค��ส�2งแบบไมู4เข้7ยนเนมูสเปส System ต�อง
ประก�ศค��ส�2ง using System;
![Page 11: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/11.jpg)
Formatting String
Console.WriteLine(" Two sample integers are {0} and {1}. ", 3, 9)
Index 0 จะใช้�ข้�อมู�ลต��แหน4งแรกเสมูอ Console.WriteLine("{0,-9}{0,9}",
12,45); ค4�ลบหมู�ยถุ5งช้�ดซ��ย ค4�บวักหมู�ยถุ5งช้�ดข้วั�
{index [,alignment] [:formatSpecifier]}
![Page 12: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/12.jpg)
Formatting String
Console.WriteLine("x = {0} y = {1}",123,456); Console.WriteLine("123456789"); Console.WriteLine("{0,9}", 123); Console.WriteLine("123456789"); Console.WriteLine("{0,-9}End", 123); Console.WriteLine("123456789123456789"); Console.WriteLine("{0,-9}{0,9}", 123,456);
![Page 13: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/13.jpg)
Formatting String
ร/ปแบบ ควิามหมาย
E หร"อ e Exponential (แสดงผลในร�ปแบบต�วัเลข้ท�งวั�ทย�ศ�สตร�)
F หร"อ f Fixed-point (แสดงผลในร�ปแบบทศน�ยมู 2 ต��แหน4ง)
G หร"อ g General (แสดงผลในร�ปแบบท�2วัไป เช้4นต�วัเลข้แสดงผลในร�ปแบบส�#นท72ส,ด)
N หร"อ n Number (แสดงผลในร�ปแบบต�วัเลข้เหมู"อนก�บ Fixed-
point แต4จะใส4เคร"2องหมู�ย comma ค�2นท,กๆ 3 หล�ก)
P หร"อ p Percentage (ต�วัเลข้จะถุ�กเปล72ยนอย�4ในร�ปแบบข้องเปอร�เซ9นต�)
X หร"อ x Hexadecimal (แสดงผลในร�ปแบบข้องเลข้ฐ�นส�บหก)
![Page 14: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/14.jpg)
Formatting String
int n = 123456789; Console.WriteLine("{0:E}", n); Console.WriteLine("{0:F}", n); Console.WriteLine("{0:G}", n); Console.WriteLine("{0:N}", n); Console.WriteLine("{0:P}", n); Console.WriteLine("{0:X}", n);
![Page 15: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/15.jpg)
Escape Sequence
Escape Sequence
ควิามหมาย
\n n ค"อก�รส�2งข้5#นบรรท�ดใหมู4\t t ค"อก�รส�2งแท9บ (tab)
\r r ค"อก�รส�2งเล"2อนเคอร�เซอร�ไปย�งซ��ยส,ดข้องบรรท�ดท72อย�4
\\ \ ค"อก�รส�2งพ�มูพ�ต�วัอ�กษร Backslash (\)
\" " ค"อก�รส�2งพ�มูพ�ต�วัอ�กษร double quote (")
![Page 16: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/16.jpg)
Escape Sequence
Console.WriteLine("Welcome \nto\nC#\nProgramming!");
![Page 17: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/17.jpg)
ค&าสี"งรบข้*อม/ลั
Read, ReadLine และ ReadKey อย�4ภ�ยใน คล�สช้"2อ Console และเนมูสเปสช้"2อ
System
![Page 18: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/18.jpg)
ค&าสี"งรบข้*อม/ลั
ค��ส�2ง Read จะร�บต�วัอ�กษรเข้��มู�จ�กน�#นจะเก9บเป3นต�วัเลข้แทน (ASCII Code)
int a;Console.Write("Input : ");a = Console.Read();Console.WriteLine(a);Console.ReadKey();
![Page 19: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/19.jpg)
ค&าสี"งรบข้*อม/ลั
ค��ส�2ง Read จะร�บต�วัอ�กษรเข้��มู�จ�กน�#นจะเก9บเป3นต�วัเลข้แทน (ASCII Code)
int a;Console.Write("Input : ");a = int.Parse(Console.ReadLine());Console.WriteLine(a);Console.ReadKey();
![Page 20: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/20.jpg)
ค&าสี"งรบข้*อม/ลั
ก�รกระท��ก�รร�บข้�อมู�ลส�มู�รถุสร,ปเป3นข้�#นตอนได�ด�งน7#1 .ต�องท��ก�รประก�ศต�วัแปรในช้น�ดข้�อมู�ลท72ต�องก�รร�บ
ก4อน2. ควัรมู7ข้�อควั�มูแจ�งให�ป:อนข้�อมู�ล เพ"2อให�ผ��ใช้�ง�นทร�บวั4�
จะต�องป:อนข้�อมู�ลน��เข้��อะไร3. ควัรใช้�ค��ส�2ง Console.ReadKey() ก4อนจบก�ร
ท��ง�นข้องโปรแกรมู เพ"2อให�หน��ต4�ง DOS หย,ดรอ ท��ให�เห9นก�รท��ง�นและผลล�พธ์�ต4�งๆ
4. เมู"2อต�องก�รข้�อมู�ลน��เข้��เป3นต�วัเลข้ ต�องร�บเข้��มู�เป3นข้�อควั�มูแล�วัจ5งแปลงข้�อควั�มูให�เป3นต�วัเลข้ก4อนน��ไปใช้�ง�น
![Page 21: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/21.jpg)
ค&าสี"งแปลังข้*อควิาม
int a;float b;string c;char d;Console.Write("Input integer : ");a = int.Parse(Console.ReadLine());Console.Write("Input float : ");b = float.Parse(Console.ReadLine());Console.Write("Input string: ");c = Console.ReadLine();Console.Write("Input character: ");d = char.Parse(Console.ReadLine());
![Page 22: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/22.jpg)
น�พิจัน�ทางคณ�ตศาสีตร� (Arithmetic
Expressions)
โอเปอเรเตอร� ค&าอธุ�บาย+ บวัก- ลบ* ค�ณิ/ ห�ร% ห�รเอ�เศษ (Modulas)
++ เพ�2มูค4�ข้5#นอ7ก 1-- ลดค4�ลงอ7ก 1
โอเปอเรเตอร�ส��หร�บก�รค��นวัณิ
![Page 23: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/23.jpg)
น�พิจัน�ทางคณ�ตศาสีตร� (Arithmetic
Expressions)
โอเปอเรเตอร� ค&าอธุ�บาย= เท4�ก�บ เช้4น A = 2;
+= บวักค4�เด�มูด�วัยค4�ท72ก��หนด เช้4น A +=1
หมู�ยถุ5ง A = A + 1;
-= ลบค4�เด�มูด�วัยค4�ท72ก��หนด เช้4น A -=2 หมู�ยถุ5ง A = A - 2;
*= ค�ณิค4�เด�มูด�วัยค4�ท72ก��หนด เช้4น A *=3 หมู�ยถุ5ง A = A * 3;
/= ห�รค4�เด�มูด�วัยค4�ท72ก��หนด เช้4น A /=2 หมู�ยถุ5ง A = A / 2;
%= ห�รค4�เด�มูด�วัยค4�ท72ก��หนด เช้4น A %=2
หมู�ยถุ5ง A = A % 2;
โอเปอเรเตอร�ส��หร�บก�รก��หนดค4�
![Page 24: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/24.jpg)
น�พิจัน�ทางคณ�ตศาสีตร� (Arithmetic
Expressions)
โอเปอเรเตอร� ค&าอธุ�บาย== เปร7ยบเท7ยบวั4�เท4�ก�นหร"อไมู4 เช้4น 2 == 3
!= เปร7ยบเท7ยบวั4�ไมู4เท4�ก�นหร"อไมู4 เช้4น 2 != 3
> เปร7ยบเท7ยบวั4�มู�กกวั4�หร"อไมู4 เช้4น 2 > 3
>= เปร7ยบเท7ยบวั4�มู�กกวั4�หร"อเท4�ก�นหร"อไมู4 เช้4น 2 >= 3
< เปร7ยบเท7ยบวั4�น�อยกวั4�หร"อไมู4 เช้4น 2 < 3
<= เปร7ยบเท7ยบวั4�น�อยกวั4�หร"อเท4�ก�นหร"อไมู4 เช้4น 2 <= 3
== เปร7ยบเท7ยบวั4�เท4�ก�นหร"อไมู4 เช้4น 2 == 3
โอเปอเรเตอร�ส��หร�บก�รเปร7ยบเท7ยบ
![Page 25: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/25.jpg)
น�พิจัน�ทางคณ�ตศาสีตร� (Arithmetic
Expressions)
โอเปอเรเตอร� ค&าอธุ�บาย&& ถุ��ท�#งสองน�พจน�เป3นจร�งท�#งค�4 ผลท72ได�เป3นจร�ง
นอกน�#นเป3นเท9จหมูด|| ถุ��ท�#งสองน�พจน�เป3นเท9จท�#งค�4 ผลท72ได�เป3นเท9จ
นอกน�#นเป3นจร�งหมูด! ถุ��น�พจน�เป3นจร�ง ผลเป3นเท9จ ถุ��น�พจน�เป3นเท9จ
ผลเป3นจร�ง (ตรงก�นข้��มู)
^ ถุ��น�พจน�เหมู"อนก�น ผลเป3นเท9จ ถุ��น�พจน�ต4�งก�น ผลเป3นจร�ง (เช้4น จร�ง-จร�ง / เท9จ-เท9จ ผลเป3นเท9จ และ จร�ง-เท9จ / เท9จ-จร�ง ผลเป3นจร�ง)
โอเปอเรเตอร�ท�งตรรกะ
![Page 26: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/26.jpg)
น�พิจัน�ทางคณ�ตศาสีตร� (Arithmetic
Expressions)
ลั&าดบควิามสี&าคญ เคร$"องหมายใน C#
1 ( ) parenthesis – วังเล9บ2 ++, -- (วั�งไวั�หน��ต�วัแปร)
3 *, /, %4 +, -5 ==, != 6 && 7 ||8 =, +=, -=, /=, %=9 ++, -- (วั�งไวั�หล�งต�วัแปร)
ล��ด�บควั�มูส��ค�ญข้องโอเปอเรเตอร�
![Page 27: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/27.jpg)
คลัาสี Math
เมท4อด ค-าท�"ค&านวิณMath.Abs(x) ค4�ส�มูบ�รณิ�ข้อง x (|x|)
Math.Ceiling(x)
จ��นวันเต9มูท72น�อยท72ส,ดท72มู�กกวั4�หร"อเท4�ก�บ x
Math.Floor(x)
จ��นวันเต9มูท72มู�กท72ส,ดท72น�อยกวั4�หร"อเท4�ก�บ x
Math.Log(x) ลอก�ร�ธ์5มูฐ�นธ์รรมูช้�ต�ข้อง xMath.Log10(x)
ลอก�ร�ธ์5มูฐ�นส�บข้อง x
Math.Round(x) จ��นวันเต9มูท72ใกล�ก�บค4� x มู�กท72ส,ดMath.Pow(x,y) x ยกก��ล�ง y ( )
Math.Sqrt(x) ร�กท72สองข้อง x ( )
Math.Max(x,y) ค4�ท72มู�กกวั4�ระหวั4�ง x และ y (2 ค4�เท4�น�#น)
Math.Min(x,y) ค4�ท72น�อยกวั4�ระหวั4�ง x และ y (2 ค4�เท4�น�#น)
Math.Sin(x) ค4�ไซน�ข้อง x (x มู7หน4วัยเป3นเรเด7ยน)
Math.Cos(x) ค4�โคไซน�ข้อง x (x มู7หน4วัยเป3นเรเด7ยน)
![Page 28: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/28.jpg)
คลัาสี Math
เมท4อด (ค-าคงท�") ควิามหมายMath.PI ค4�คงท72 ซ52งมู7ค4�ประมู�ณิ
3.14159265358979323846Math.E ค4�คงท72 e ซ52งมู7ค4�ประมู�ณิ
2.7182818284590452354
![Page 29: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/29.jpg)
เทคน�คเพิ�"มเต�มเก�"ยวิกบสีตร�ง
โอเปอเรเตอร�ท72กล4�วัมู�เป3นส4วันท72ใช้�ในก�รจ�ดก�รก�บข้�อมู�ลท72เป3นต�วัเลข้
ส��หร�บต�วัอ�กษร (string) ส�มู�รถุใช้�โอเปอเรเตอร� "+" หร"อ "+=" ซ52งเป3นก�รต�วัอ�กษรมู�วั�งเร7ยงต4อก�น
![Page 30: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/30.jpg)
RAPTOR to C#
int BirthYear, Age;
หน��ต4�ง Watch Window
![Page 31: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/31.jpg)
RAPTOR to C#
Console.WriteLine("Your Birth Year");BirthYear = int.Parse(Console.ReadLine());
Start
"Your Birth Year"GET BirthYear
Age ← 2556 - BirthYear
PUT "You are "+Age+" Years old"¶
End
ร�ป Input
![Page 32: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/32.jpg)
RAPTOR to C#
Age = 2556-BirthYear;
Start
"Your Birth Year"GET BirthYear
Age ← 2556 - BirthYear
PUT "You are "+Age+" Years old"¶
End
ร�ป Process
![Page 33: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/33.jpg)
RAPTOR to C#
Console.WriteLine("You are " + Age + " Years old");
Start
"Your Birth Year"GET BirthYear
Age ← 2556 - BirthYear
PUT "You are "+Age+" Years old"¶
End
ร�ป Output
![Page 34: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/34.jpg)
RAPTOR to C#
int BirthYear, Age;
Console.WriteLine("Your Birth Year");BirthYear = int.Parse(Console.ReadLine());
Age = 2556-BirthYear;
Console.WriteLine("You are " + Age + " Years old");Console.ReadKey();
รวัมูค��ส�2ง
![Page 35: Chapter04](https://reader035.vdocuments.us/reader035/viewer/2022070315/5553b49bb4c905d9448b4c97/html5/thumbnails/35.jpg)
ค&าถาม
ข้�อสงส�ย หร"อ ค��ถุ�มู ค��ถุ�มูท��ยบทท72 4 (ส4งในห�อง/ส�ปด�ห�หน��)