งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Computer Programming การเขียนโปรแกรมคอมพิวเตอร์"— ใบสำเนางานนำเสนอ:

1 Computer Programming การเขียนโปรแกรมคอมพิวเตอร์
สัปดาห์ที่ 3 เครื่องหมายและการดำเนินการในภาษาซี

2 objectives เข้าใจองค์ประกอบและโครงสร้างการทำงานของภาษาซี
ทบทวน ตัวแปร ชนิดของตัวแปร การตั้งชื่อตัวแปรและการประกาศตัวแปร เข้าใจเครื่องหมายและการดำเนินการของภาษาซี เครื่องหมายและการคำนวณทางคณิตศาสตร์ ลำดับความสำคัญของเครื่องหมายในการคำนวณ เครื่องหมายและการดำเนินการเปรียบเทียบ เครื่องหมายและการดำเนินการทางตรรกศาสตร์

3 Review Last Contents of the Class
Outline 1 Review Assignment 2 p Review Last Contents of the Class 3 Operator Types 4 Precedence of Operators 5 Assignment #2

4 Principal of Problem Analysis
การวิเคราะห์ข้อมูลเอาท์พุท หรือผลลัพธ์ (Output Analysis) วิเคราะห์ความต้องการของผู้ใช้ หรือผลลัพธ์จากโจทย์ปัญหา การวิเคราะห์ข้อมูลอินพุท (Input Analysis) วิเคราะห์ข้อมูลที่ผู้ใช้ป้อน หรือข้อมูลที่โจทย์ให้มา การวิเคราะห์กระบวนการทำงาน (Process Analysis) วิเคราะห์ขั้นตอนการทำงานที่ให้ได้มา ซึ่งผลลัพธ์ การกำหนดตัวแปร (Variable Define) กำหนดตัวแปรที่ใช้งานการเขียนโปรแกรมเพื่อความถูกต้อง

5 Example of Problem Analysis (1)
โจทย์ จงเขียนผังงาน และโปรแกรมคำนวณหาพื้นที่วงกลม Output Analysis ผลลัพธ์ที่ต้องการ คือ พื้นที่ของวงกลม Input Analysis การคำนวณหาพื้นที่วงกลม จำเป็นต้องทราบขนาดของรัศมี (หรือเส้นผ่านศูนย์กลาง)

6 Example of Problem Analysis (2)
Process Analysis 1. รอรับค่ารัศมี (หรือเส้นผ่านศูนย์กลาง) จากผู้ใช้งาน 2. คำนวณหาพื้นที่วงกลมจากสูตร 3. แสดงผลค่าพื้นที่วงกลมออกทางหน้าจอ Variable Define radius : เป็นตัวแปรชนิดจำนวนทศนิยมสำหรับรับค่ารัศมี pi : เป็นตัวแปรชนิดจำนวนทศนิยมสำหรับเก็บค่า area : เป็นตัวแปรชนิดจำนวนทศนิยมสำหรับเก็บค่าพื้นที่

7 Example of Problem Analysis (3)
float radius, pi, area; pi=22.0/7; // pi = 3.14; printf ("Enter Radius of Circular : "); scanf ("%f",&radius); area = pi * radius * radius; printf ("Area of Circular : %f",area);

8 Example of Problem Analysis (4)
#include<stdio.h> int main() { float radius, pi, area; pi = 22.0/7; // pi = 3.14; printf ("Enter Radius of Circular : "); scanf ("%f",&radius); area = pi * radius * radius; printf ("Area of Circular : %f",area); return 0; }

9 Review Last Contents of the Class
Outline 1 Review Assignment 2 p Review Last Contents of the Class 3 Operator Types 4 Precedence of Operators 5 Assignment #2

10 Compile and Link of C Program (1)
การสร้างโปรแกรมที่สามารถใช้งานได้ขึ้นมาโปรแกรมหนึ่ง ในภาษาซี มีขั้นตอนดังนี้ 1. สร้างตัวโปรแกรมที่เป็นตัวอักษร หรือเรียกว่า “ซอร์สไฟล์ (Source file)” โดยมีนามสกุลเป็น .c หรือ .cpp ขึ้นมาก่อน โดยใช้โปรแกรมที่สามารถเขียนไฟล์ที่เก็บอักขระ (Editor) ใดๆ ก็ได้ อักษรหรืออักขระใดๆ นั้น จะต้องอยู่ในรูปแบบของการโปรแกรมภาษา (ขั้นตอนนี้คือการสร้างโปรแกรมที่เป็นภาษามนุษย์นั่นเอง)

11 Compile and Link of C Program (2)
คอมไพล์เลอร์ของภาษาซี (C Compiler) จะทำการแปลงซอร์สไฟล์ จากอักขระใดๆ ให้เป็นรหัสที่เครื่องคอมพิวเตอร์สามารถเข้าใจได้เก็บไว้ในอีกไฟล์หนึ่งเรียกว่า “ไฟล์วัตถุประสงค์ (Object file)” ที่มีนามสกุล .obj (ขั้นตอนนี้ถือได้ว่าการคอมไพล์เป็นการแปลงภาษามนุษย์เป็นภาษาเครื่องนั่นเอง) 3. ตัวเชื่อม (Linker) จะทำการตรวจสอบว่าในโปรแกรมที่เขียนขึ้นนั้น มีการเรียกใช้งานฟังก์ชันมาตรฐานใด จากห้องสมุดของภาษาซี (C Library) บ้างหรือไม่ ถ้ามีตัวเชื่อมจะทำการรวมเอาฟังก์ชันเหล่านั้นเข้ากับไฟล์วัตถุประสงค์ แล้วจะได้ไฟล์ที่สามารถทำงานได้ โดยมีนามสกุลเป็น .exe (ขั้นตอนนี้เรียกว่า “การลิงค์” เป็นการรวมฟังก์ชันสำเร็จรูปเข้าไป แล้วสร้างไฟล์ที่ทำงานได้)

12 Compile and Link of C Program (3)
C Compiler Linker Library Source File Computer Object File Computer Object File

13 C Programming Structure (1)
void main(void) { } Statements ; Local Declarations Preprocessor directive Global Declarations main function User define functions int function ()

14 C Programming Structure (2)
พรีโปรเซสเซอร์ไดเร็คทีฟ #include<file.h> type function_name(type); type variable; int main() { statement-1; ... statement-n; return 0; } type function_name(type variable) return(var); ฟังก์ชันโพรโทรไทพ์ ส่วนหัวโปรแกรม ตัวแปรชนิดโกบอล ตัวแปรชนิดโลคอล ฟังก์ชันหลัก คำสั่ง ส่วนตัวโปรแกรม ฟังก์ชันย่อย

15 ส่วนหัวโปรแกรม (Header Program)
Header of Program ส่วนหัวโปรแกรม (Header Program) พรีโปรเซสเซอร์ไดเร็คทีฟ (Preprocessor Directive) #include #define ฟังก์ชันโพรโทรไทพ์ (Function Prototype Declaration) ตัวแปรชนิดโกบอล (Global Variable Declaration)

16 Preprocessor Directive (1)
ส่วนประมวลผลก่อน เป็นส่วนที่สั่งให้คอมไพเลอร์เตรียมการทำงานที่กำหนดไว้ ก่อนที่จะทำงานในฟังก์ชันหลัก ให้โปรแกรมดึงคำสั่งจาก file-name.h ใน Include Directory ให้โปรแกรมดึงคำสั่งจาก file-name.h ใน Current Directory file-name คือ ชื่อไฟล์นามสกุล h #include<file-name.h> #include"file-name.h"

17 Preprocessor Directive (2)
วิธีการใช้งาน #include <ชื่อไฟล์> หรือ #include “ชื่อไฟล์” ตัวอย่าง #include <stdio.h> (เป็นการเรียกใช้ไฟล์ stdio.h เข้ามาในโปรแกรม) #include <mypro.h> (เป็นการเรียกใช้ไฟล์ mypro.h เข้ามาในโปรแกรม) < > จะเรียกไฟล์ใน directory ที่กำหนดโดยตัวคอมไพล์เลอร์ “ ” จะเรียกไฟล์ใน directory ทีทำงานอยู่ในปัจจุบัน

18 Examples of Preprocessor Directive
#include<stdio.h> ทำการเรียกไฟล์ stdio.h เพื่อทำให้สามารถใช้คำสั่งทั่วไปได้ #include<conio.h> ทำการเรียกไฟล์ conio.h เพื่อทำให้สามารถใช้คำสั่งจัดการหน้าจอ และคำสั่งรับและแสดงผลเพิ่มเติม #include<math.h> ทำการเรียกไฟล์ math.h เพื่อทำให้สามารถใช้คำสั่งเกี่ยวกับคณิตศาสตร์ได้ #include<string.h> ทำการเรียกไฟล์ string.h เพื่อทำให้สามารถใช้คำสั่งจัดการเกี่ยวกับข้อความได้

19 Category of Preprocessor Directive
ส่วนประมวลผลก่อน เป็นส่วนที่สั่งให้คอมไพเลอร์เตรียมการทำงานที่กำหนดไว้ ก่อนที่จะทำงานในฟังก์ชันหลัก ให้โปรแกรมดึงคำสั่งจาก file-name.h ใน Include Directory ให้โปรแกรมดึงคำสั่งจาก file-name.h ใน Current Directory file-name คือ ชื่อไฟล์นามสกุล h #include<file-name.h> #include"file-name.h"

20 Example of Include File-name in Current Directory
#include"mine.h" #include"/u/example_prog/mine.h"

21 #define ชื่อ ค่าที่ต้องการ
วิธีการใช้งาน #define ชื่อ ค่าที่ต้องการ ตัวอย่าง #define START (กำหนดค่า START = 10) #define A 3*5/ (กำหนดค่า A=3*5/4) #define pi (กำหนดค่า pi = ) #define sum(a,b) a+b (กำหนดค่า sum(ตัวแปรที่1, ตัวแปรที่2) (= ตัวแปรที่1 + ตัวแปรที่2)

22 Function Prototype Declaration
พรีโปรเซสเซอร์ไดเร็คทีฟ #include<file.h> type function_name(type); type variable; int main() { statement-1; ... statement-n; return 0; } type function_name(type variable) return(var); ฟังก์ชันโพรโทรไทพ์ ส่วนหัวโปรแกรม ตัวแปรชนิดโกบอล ตัวแปรชนิดโลคอล ฟังก์ชันหลัก คำสั่ง ส่วนตัวโปรแกรม ฟังก์ชันย่อย

23 Return_Type Function_name(Type variable)
Process (ทำงาน) ส่งค่ากลับ รับค่ามา Function Return_Type Function_name(Type variable)

24 Function (2) Function การเขียนฟังก์ชันโดยที่ไม่ต้องมีการประกาศก่อน สามารถประกาศตัวแปรไว้ใช้งานเฉพาะในฟังก์ชันได้ ฟังก์ชันประกอบไปด้วย คำสั่ง (Statement) : การทำงานอย่างหนึ่ง เรียงกันไป Return : การคืนค่าที่เป็นผลลัพธ์การทำงานของฟังก์ชัน Function Main ฟังก์ชันที่ทุกโปรแกรมที่เรียกทำงานได้ต้องมี โปรแกรมจะเริ่มทำงานจากฟังก์ชัน main ก่อน ฟังก์ชัน main จะมีการเรียกการทำงานของฟังก์ชั่นอื่นๆ

25 ระหว่างปีกกาจะประกอบไปด้วยคำสั่ง(Statement) ต่างๆ ที่จะให้โปรแกรมทำงาน
Function (3) ส่วนนี้ทุกโปรแกรมจะต้องมี โดยโปรแกรมหลักจะเริ่มต้นด้วย main() และตามด้วยเครื่องหมายปีกกาเปิด ‘{’ และปีกกาปิด ‘}’ ระหว่างปีกกาจะประกอบไปด้วยคำสั่ง(Statement) ต่างๆ ที่จะให้โปรแกรมทำงาน แต่ละคำสั่งจะต้องจบด้วยเซมิโคลอน ‘;’ (Semicolon) #include <stdio.h> void main(void) { ... Statement ; }

26 Main Function ตัวอย่าง ผลการทำงาน Height in inches is 72
#include <stdio.h> int feet,inches; void main() { feet = 6; inches = feet * 12; printf("Height in inches is %d",inches); } ผลการทำงาน Height in inches is 72

27 สร้างฟังก์ชันหรือคำใหม่ขึ้นมาใช้งานตามที่เราต้องการ
User Define Function สร้างฟังก์ชันหรือคำใหม่ขึ้นมาใช้งานตามที่เราต้องการ ระหว่างปีกกาจะประกอบด้วยคำสั่ง(Statement) ต่างๆ ที่จะให้ฟังก์ชันทำงาน สามารถเรียกใช้ภายใน โปรแกรมได้ทุกที่ #include <stdio.h> int function() void main(void) { ... Statement ; } return (int value);

28 Example of User Define Function (1)
ตัวอย่าง #include <stdio.h> int Feet2Inch(int); int feet,inches; void main() { feet = 6; inches = Feet2Inch(feet); printf("Height in inches is %d",inches); } int Feet2Inch(int f) return f*12; ผลการทำงาน Height in inches is 72

29 Example of User Define Function (2)
#include <stdio.h> void adder(int , int ); int summ; int main() { adder(20, 30); // จุดที่เรียกใช้ฟังก์ชันโพรโตไทป์ printf(“summary = %d”,summ) return 0; } void adder(int a, int b) summ = a+b;

30 Global and Local Variable
พรีโปรเซสเซอร์ไดเร็คทีฟ #include<file.h> type function_name(type); type variable; int main() { statement-1; ... statement-n; return 0; } type function_name(type variable) return(var); ฟังก์ชันโพรโทรไทพ์ ส่วนหัวโปรแกรม ตัวแปรชนิดโกบอล ตัวแปรชนิดโลคอล ฟังก์ชันหลัก คำสั่ง ส่วนตัวโปรแกรม ฟังก์ชันย่อย

31 Global Declarations เป็นการประกาศตัวแปรเพื่อใช้งานในโปรแกรม โดยตัวแปรนั้นสามารถใช้ได้ในทุกที่ในโปรแกรม เป็นส่วนที่ใช้ในการประกาศ Function Prototype ของโปรแกรม ส่วนนี้ในบางโปรแกรมอาจจะมีหรือไม่มีก็ได้ ตัวอย่าง int summation(float x, float y) ; (ประกาศ function summation) int x,y ; (กำหนดตัวแปร x,y เป็นจำนวนเต็ม) float z=3; (กำหนดตัวแปร z เป็นจำนวนจริง)

32 Size of Variables ชนิดของตัวแปร ขนาด ค่าต่ำสุด ค่าสูงสุด unsigned char
8 bit 255 char -128 127 unsigned int 16 bit 32 bit 65,535 4,294,967,295 short int -32,768 32,767 int -2,147,483,648 2,147,483,647 unsigned long long float 3.410-38 3.4 1038 double 64 bit 1.7 1.710308 long double 80 bit 3.4 3.4104932

33 Size of Variables & Memory Size
char short int unsigned int long unsigned long float double long double Maximum Minimum

34 What is a statement? ส่วนหัวโปรแกรม ฟังก์ชันหลัก ส่วนตัวโปรแกรม
พรีโปรเซสเซอร์ไดเร็คทีฟ #include<file.h> type function_name(type); type variable; int main() { statement-1; ... statement-n; return 0; } type function_name(type variable) return(var); ฟังก์ชันโพรโทรไทพ์ ส่วนหัวโปรแกรม ตัวแปรชนิดโกบอล ตัวแปรชนิดโลคอล ฟังก์ชันหลัก คำสั่ง ส่วนตัวโปรแกรม ฟังก์ชันย่อย

35 Return Value ส่วนหัวโปรแกรม ฟังก์ชันหลัก ส่วนตัวโปรแกรม ฟังก์ชันย่อย
พรีโปรเซสเซอร์ไดเร็คทีฟ #include<file.h> type function_name(type); type variable; int main() { statement-1; ... statement-n; return 0; } type function_name(type variable) return(var); ฟังก์ชันโพรโทรไทพ์ ส่วนหัวโปรแกรม ตัวแปรชนิดโกบอล ตัวแปรชนิดโลคอล ฟังก์ชันหลัก คำสั่ง ส่วนตัวโปรแกรม ฟังก์ชันย่อย

36 การคอมเมนต์ที่นิยมใช้มีสองรูปแบบด้วยกัน
Comment เป็นเครื่องหมายที่เมื่ออยู่หน้าคำสั่งใด หรือบรรทัดใด คำสั่งที่บรรทัดนั้นจะถูกละหรือข้ามการทำคำสั่งนั้นไป โดยไม่มีการ compile บรรทัดนั้น การคอมเมนต์ที่นิยมใช้มีสองรูปแบบด้วยกัน // หมายเหตุ 1 บรรทัด /* หมายเหตุหลายบรรทัด */

37 ตัวอย่างการเขียนฟังก์ชันหลักที่ไม่มีการทำงานใดๆ
Main Function ฟังก์ชันหลักเป็นฟังก์ชันที่โปรแกรมภาษาซีต้องมีอยู่เสมอ เพราะคอมไพเลอร์ของภาษาซีจะเริ่มต้นการทำงานจากฟังก์ชันหลัก ตัวอย่างการเขียนฟังก์ชันหลักที่ไม่มีการทำงานใดๆ int main(void) { return(0); } void main() { }

38 Category of Type of Function
เป็นพารามิเตอร์ที่ใช้งานในภาษาซี เพื่อกำหนดลักษณะฟังก์ชันต้นแบบ และ ตัวแปรที่ใช้งานในโปรแกรม มี 4 ชนิด void ข้อมูลชนิดว่างเปล่า int ข้อมูลชนิดจำนวนเต็ม float ข้อมูลชนิดจำนวนทศนิยม char ข้อมูลชนิดอักขระ

39 Void void เป็นพารามิเตอร์ที่ใช้งานในส่วนของ ฟังก์ชันโพรโทรไทพ์ การสร้างและใช้งานฟังก์ชัน เพื่อแสดงให้รู้ว่าฟังก์ชันที่สร้างขึ้นมาไม่มีการส่งหรือรับค่าจากการเรียกใช้งานฟังก์ชัน

40 Integer int เป็นพารามิเตอร์หลักที่ใช้กับข้อมูลชนิดจำนวนเต็ม โดยมีการใช้งาน 5 รูปแบบดังนี้ unsigned int ข้อมูลชนิดจำนวนเต็มไม่คิดเครื่องหมายขนาด 2 bytes short int ข้อมูลชนิดจำนวนเต็มขนาด 2 bytes int ข้อมูลชนิดจำนวนเต็มขนาด 2 bytes หรือ 4 byte (ในคอมไพเลอร์ 32 bit ตัวแปร int มีขนาด 4 byte แต่ในคอมไพเลอร์ 16 bit ตัวแปร int มีขนาด 2 byte) unsigned long ข้อมูลชนิดจำนวนเต็มไม่คิดเครื่องหมายขนาด 4 bytes long ข้อมูลชนิดจำนวนเต็มขนาด 4 bytes

41 Float float เป็นพารามิเตอร์หลักที่ใช้กับข้อมูลชนิดจำนวนทศนิยมโดยมีการใช้งาน 3 รูปแบบดังนี้ float ข้อมูลชนิดจำนวนทศนิยมขนาด 4 byte double ข้อมูลชนิดจำนวนทศนิยมขนาด 8 byte long double ข้อมูลชนิดจำนวนทศนิยมขนาด 10 byte

42 unsigned char ข้อมูลชนิดอักขระไม่คิดเครื่องหมาย
Character char เป็นพารามิเตอร์ที่ใช้งานเกี่ยวกับตัวอักษร และข้อความในภาษาซี โดยมีการกำหนดค่าอักขระโดยให้อยู่ในเครื่องหมาย single quote ('...') เช่น 'C', 'o', 'm', '1' อักขระพิเศษบางตัวไม่สามารถกำหนดค่าให้ได้โดยตรง แต่ใช้ค่ารหัส ASCII เช่นอักขระควบคุมการแสดงผลการขึ้นต้นบรรทัดใหม่ใช้ '\n' เป็นต้น โดยมีรูปแบบการใช้งาน 2 รูปแบบ unsigned char ข้อมูลชนิดอักขระไม่คิดเครื่องหมาย char ข้อมูลชนิดอักขระปกติ

43 Special Character for printf( )
ค่ารหัส ASCII การใช้งาน \t เว้นช่องว่างทุก 1 แท็ป (8 ช่องตัวอักษร) \n ขึ้นบรรทัดใหม่ \0 เป็นอักขระว่าง \' แสดง single quote ออกทางจอภาพ \" แสดง double quote ออกทางจอภาพ \\ แสดง backslash ออกทางจอภาพ

44 Format of Variables ส่วนแสดงชนิดข้อมูล การใช้งาน %d
แสดงผลข้อมูลชนิดจำนวนเต็ม %u แสดงผลข้อมูลชนิดจำนวนเต็มบวก (ไม่คิดเครื่องหมาย) %o แสดงผลข้อมูลเป็นเลขฐานแปด %x แสดงผลข้อมูลเป็นเลขฐานสิบหก %f แสดงผลข้อมูลชนิดจำนวนทศนิยม (6 ตำแหน่ง) %e แสดงผลข้อมูลเป็นจำนวนทศนิยมและอยู่ในรูปยกกำลัง %c แสดงผลข้อมูลชนิดอักขระ %s แสดงผลข้อมูลชนิดข้อความ %p แสดงผลข้อมูลชนิดตัวชี้ตำแหน่ง

45 Rule of Statement ใช้เครื่องหมาย semi colon ( ; ) เป็นจุดสิ้นสุดคำสั่ง ใช้อักษรตัวเล็กสำหรับเรียกใช้คำสั่ง (statement) ใช้เครื่องหมาย comma , สำหรับคั่นตัวแปร และพารามิเตอร์ หากคำสั่งใดมีคำสั่งส่วนย่อยภายในหลายๆ คำสั่ง ให้ใช้เครื่องหมายปีกกา { } สำหรับกำหนดขอบเขต

46 การประกาศตัวแปรแบบไม่กำหนดค่าเริ่มต้น
Variable Declaration การประกาศตัวแปรแบบไม่กำหนดค่าเริ่มต้น type var1; type คือ ชนิดของข้อมูลที่จะกำหนดให้กับตัวแปร varX คือ ชื่อของตัวแปร type var1, var2, … , varN;

47 Example of Variable Declaration
int number; int a, b, c; float real; float point1, point2; char choice; char ch1,ch2;

48 Declaration & Assign Value
type var1 = value1; type คือ ชนิดของข้อมูลที่จะกำหนดให้กับตัวแปร varX คือ ชื่อของตัวแปร valueX คือ ค่าของตัวแปรที่ต้องการกำหนดให้ type var1=value1, varN=valueN;

49 Example of Declaration & Assign Value
int number = 25; int a = 1, b = 2, c = 3; float real = 99.99; float point1 = 45.2, point2 = 30; char choice = 'a'; char ch1 = 'o', ch2 = 'z';

50 Char for String Variable
char var1[M1]; varX คือ ชื่อตัวแปร MX คือ จำนวนของอักขระที่จะใช้เก็บบวกด้วย 1 การใช้ตัวแปรแถวลำดับชนิดอักขระเป็นตัวแปรข้อความ ในภาษาซีกำหนดไว้ว่าตัวสุดท้ายของตัวแปรแถวลำดับคือ \0 \0  Null character char var1[M1], var2[M2];

51 Reserve Array for Char to String Variable
char str[15]; char first[20],last[20]; str[15] first[20] last[20]

52 Declaration & Assign Value
char var[M] = "??...?"; var คือ ชื่อตัวแปร M คือ จำนวนของอักขระที่จะใช้เก็บบวกด้วย 1 ? คือ อักษรที่จะกำหนดค่าให้ข้อความ จำนวน m-1 ตัว char var[M] = {'?','?',...,'?'}; char var[] = "??...? ";

53 Example of Declaration & Assign Value
char subject[12] = "Programming"; char nick[4] = "Com"; char nick_1[4] = {'C','o','m','\0'}; char name[] = "Jirasak"; subject[12] nick[4] P r o g a m i n \0 C o m \0 name[] nick_1[4] J i r a s k \0 C o m \0

54 Review Last Contents of the Class
Outline 1 Review Assignment 2 p Review Last Contents of the Class 3 Operator Types 4 Precedence of Operators 5 Assignment #2

55 Variable & Expression ตัวแปร (Variable) คือ การจองพื้นที่ในหน่วยความจำของคอมพิวเตอร์สำหรับเก็บข้อมูลที่ต้องใช้ในการทำงานของโปรแกรม โดยมีการตั้งชื่อเรียกหน่วยความจำในตำแหน่งนั้นด้วย เช่น a, B, Name นิพจน์  (Expression) คือ การนำข้อมูลและตัวแปรมาดำเนินการด้วยตัวดำเนินการ เช่น เครื่องหมายทางคณิตศาสตร์   ตรรกศาสตร์ หรือเครื่องหมายเปรียบเทียบ ดังตัวอย่างเช่น 15a, 4ab+5c

56 Operator & Operand N - 1 5 * ( a * b – 3 ) นิพจน์

57 Type of Operators ตัวดำเนินการเลขคณิต (arithmetic operators)
ตัวดำเนินการเชิงสัมพันธ์ (relational operators) ตัวดำเนินการเชิงตรรกะ (logical operators) ตัวดำเนินการระดับบิต (bitwise operators) ตัวดำเนินการกำหนดค่า (assignment operators) ตัวดำเนินการกำหนดค่าเชิงประกอบ (compound assignment operators) ตัวดำเนินการเพิ่มและลดค่า (increment and decrement operators) ตัวดำเนินการแปลงชนิดข้อมูล (type cast operators)

58 Arithmetic Operators เครื่องหมาย การทำงาน ตัวอย่าง + บวก ans = a + b;
- ลบ ans = a - b; * คูณ ans = a * b; / หาร ans = a / b; % โมดูลัส (modulo) ans = a % b;

59 Results of Arithmetic Operators
+, -, *, / ใช้แทนการดำเนินการ บวก ลบ คูณ หาร ตามลำดับ ผลลัพธ์ที่ได้จะขึ้นอยู่กับ operand ที่มากระทำกัน โดยจะถูกแปลงเป็น operand ที่มีขนาดใหญ่กว่าก่อน จำนวนเต็ม +, -, *,/ จำนวนเต็ม => จำนวนเต็ม จำนวนเต็ม +, -, *, / จำนวนจริง => จำนวนจริง จำนวนจริง +, -, *, / จำนวนเต็ม => จำนวนจริง จำนวนจริง +, -, *, / จำนวนจริง => จำนวนจริง

60 Examples of Arithmetic Operators
Type 1 Operator Type 2 Result Example int +,-,*,/ 3*3 9 19/2 float 3*3.0 19/2.0 3.0*3 19.0/2 3.0*3.0 19.0/2.0 % 7%4 3 8%4

61 The Difference of Modulus & Divide
ผลลัพธ์ที่ได้จะเป็นเศษของการหาร เช่น 0 % 3 => ผลหารได้ 0 ครั้ง เหลือเศษ 0 1 / 3 => ผลหารได้ 0 ครั้ง เหลือเศษ 1 2 % 3 => ผลหารได้ 0 ครั้ง เหลือเศษ 2 3 / 3 => ผลหารได้ 1 ครั้ง เหลือเศษ 0 4 % 3 => ผลหารได้ 1 ครั้ง เหลือเศษ 1

62 Test of Arithmetic Operators
#include<stdio.h> void main( ) { int x, y, z; printf(“Enter x : ”); scanf(“%d”, &x); printf(“Enter y : ”); scanf(“%d”, &y); printf(“x + y = %d\n”, x + y); printf(“x - y = %d\n”, x - y); printf(“x * y = %d\n”, x * y); printf(“x / y = %d\n”, x / y); printf(“x mod y = %d\n”, x % y); }

63 Relational Operators เครื่องหมาย การเปรียบเทียบ ตัวอย่าง == เท่ากับ
x == y != ไม่เท่ากับ x != y > มากกว่า x > y >= มากกว่าหรือเท่ากับ x >= y < น้อยกว่า x < y <= น้อยกว่าหรือเท่ากับ x <= y

64 Examples of Relational Operators
การเปรียบเทียบ ผลที่ได้ 7 == 9 False 7 != 9 True 8 > 8 8 >= 8 (10+9)<7 4 <= 3 การเปรียบเทียบ ผลที่ได้ 22 == 22 True (3+5)!=8 False 9 > 7 7 >= 9 7<(10+9) 3 <= 4 ไม่ควรใช้เครื่องหมายเท่ากับ == หรือไม่เท่ากับ != สำหรับข้อมูลทศนิยม

65 Test of Relational Operators
#include<stdio.h> main( ) { int A, B; printf(“Enter A : ”); scanf(“%d”, &A); printf(“Enter B : ”); scanf(“%d”, &B); printf(“Output expressions %d == %d is %d\n”, A, B, A == B); printf(“Output expressions %d != %d is %d\n”, A, B, A != B); printf(“Output expressions %d > %d is %d\n”, A, B, A > B); printf(“Output expressions %d >= %d is %d\n”, A, B, A >= B); printf(“Output expressions %d < %d is %d\n”, A, B, A < B); printf(“Output expressions %d <= %d is %d\n”, A, B, A <= B); }

66 Logical Operators (1) เครื่องหมาย ความหมาย ตัวอย่าง && และ (and)
x && y || หรือ (or) x || y ! ไม่ หรือ ตรงกันข้าม (not) !x

67 Logical Operators (2) การดำเนินการ ผลที่ได้ การดำเนินการ ผลที่ได้
T && T T T && F F F && T F && F การดำเนินการ ผลที่ได้ T || T T T || F F || T F || F F การดำเนินการ ผลที่ได้ !T F !F T

68 Test of Logical Operators
#include<stdio.h> main( ) { int p, q; printf(“***Results of logical operators***\n”); printf(“ p | q | p && q | p || q | !p | !q \n”); printf(“******************************\n”); p = 2; q = 3; printf(“ %d | %d | %d | %d | %d | %d \n”, p, q, p&&q, p||q, !p, !q); p = 1; q = 0; p = 0; q = 5; p = 0; q = 0; }

69 Example of Using the Operators
int num1 = 10, num2 = 20, num3 = 30; num1 == num2 False num1 > num2 False (num1<num2) && (num2<num3) True (num1>num2) || (num1>num3) False (num1>num2) || (num2<num3) True

70 Bitwise Operators

71 Example of Bitwise Operators (1)
Left shift a<< 1 (a = 12 = ) 7 6 5 4 3 2 1 12 24 ข้อสังเกต การเลื่อนบิตทางซ้ายมีค่าเท่ากับการคูณด้วย 2

72 Example of Bitwise Operators (2)
Right shift 127 >> 1 (a = 127 = ) 7 6 5 4 3 2 1 127 63 ข้อสังเกต การเลื่อนบิตทางขวามีค่าเท่ากับการหารด้วย 2 (เศษทิ้ง)

73 Example of Bitwise Operators (3)

74 Logical & Bitwise Operator
ตัวอย่าง 10 = ; 1 = Logical Bitwise 7474

75 Assignment Operators คือ ตัวดำเนินการเพื่อใช้ในการกำหนดค่าข้อมูล รูปแบบ assignment expression คือ variable = expression กำหนดค่าให้ตัวแปรหนึ่งตัว เช่น x = 2, x = y+2; รูปแบบ Multiple assignment expression คือ variable#1 = variable#2 =… = variable#k = expression กำหนดค่าให้ตัวแปรหลายตัวในคราวเดียวกัน เช่น x = z = w = y + 2

76 Example of Assignment Operators
ตัวอย่าง assignment expression และ multiple assignment expression int i; float x,y; x=i=y=24.3; ค่าที่ได้ x = , i = 24 , y = 24.3 หมายเหตุ: ค่าของการ assign ขึ้นอยู่กับชนิดของตัวแปรที่รับค่าและตัวแปรที่อยู่ทางด้านขวา (ทำจากด้านขวามาซ้าย)

77 Test of Assignment Operators
#include<stdio.h> main( ) { int output, intValue; intValue = 3; output = (inValue < 10) ? 15 : 5; printf(“Print output data %d \n”, output); intValue = 10; }

78 Composition Operator (1)
คือตัวดำเนินการที่ประกอบไปด้วยตัวดำเนินการประเภทอื่นประกอบกับระหว่างตัวดำเนินการกำหนดค่า

79 Composition Operator (2)
เครื่องหมาย ตัวอย่างการใช้งาน ตัวอย่างรูปแบบเต็ม += y += x; y = y + x; -= y -= x; y = y - x; *= y *= x; y = y * x; /= y /= x; y = y / x; %= y %= x; y = y % x;

80 Increment-Decrement Operator
คือตัวดำเนินการเพื่อใช้เพิ่มค่าหรือลดค่าข้อมูล จำนวนเต็ม หรือ Char มี 2 ตัวคือ ++ และ –- (คล้าย X = X +/- 1) รูปแบบอยู่หลังตัวถูกกระทำ (X++, X--) นำค่าไปใช้ก่อนแล้วเพิ่มค่าตัวถูกกระทำ รูปแบบอยู่หน้าตัวถูกกระทำ (++X, --X) เพิ่มค่าก่อนแล้วนำตัวแปรไปใช้ มีใช้เฉพาะภาษาซี

81 Meaning of Increment & Decrement
เครื่องหมาย การทำงาน ตัวอย่าง ขั้นตอนการทำงาน ++ เพิ่มค่าทีละ 1 (Increment) x++; ++x; เพิ่มค่า x ขึ้น 1 y = ++x; กำหนดค่าให้ y y = x++; -- ลดค่าทีละ 1 (Decrement) x--; --x; ลดค่า x ลง 1 y = --x; y = x--;

82 Example of Increment-Decrement Operator
ตัวอย่าง

83 Test of Increment-Decrement Operator
#include<stdio.h> main( ) { int a = 9, b = 6, c = 0; c = (a++) + (++b); printf(“%d %d %d\n”, a, b, c); c = (--a) + (--b); c = ++a; c = c – b--; c = c + --a; c = c - ++b; }

84 Type Cast Operators คือ ตัวดำเนินการเพื่อใช้ในการแปลงชนิดค่าข้อมูล มีรูปแบบเป็น (type_specifier) expression type_specifier คือ ชนิดของข้อมูลที่ต้องการแปลง expression คือ นิพจน์ใดๆ ในภาษาซี การแปลงชนิดข้อมูลแบบนี้เราเรียกว่า Explicit type conversion หรือ Explicit type casting หรือ Casting

85 Example of Type Cast Operators
ตัวอย่าง a = , b = 5.3 มี float c; c = (int) a + b; แปลงชนิดข้อมูลของตัวแปร a เป็นชนิด int แล้วจึงนำไปบวกกับตัวแปร b : ได้ค่า c = 17.3 c = (int) (a + b); แปลงผลลัพธ์ที่ได้จากการบวกตัวแปร a + b เป็นชนิด int : ได้ค่า c = 18.0

86 Implicit Type Conversion(1)
เป็นการแปลงข้อมูลโดยอัตโนมัติ operator ที่เป็นตัวกระทำระหว่าง operand สองตัวจำเป็นจะต้องเป็น operand ที่เป็นชนิดข้อมูลเดียวกัน ถ้าไม่ใช่แปลงชนิดข้อมูลชนิดใช้เนื้อที่การเก็บน้อยกว่าเป็นใหญ่กว่า char -> short -> int -> long -> float -> double

87 Implicit Type Conversion(2)
ตัวอย่าง กำหนด char ch; int i; float f; double d;

88 The Other Operators ( ) parentheses operator คือ เครื่องหมายวงเล็บเพื่อแยกการทำงานของ operator อื่นๆ ออกจากกัน เช่น 2*(3+4) [ ] array subscript operator คือ เครื่องหมายเพื่อกำหนดขนาดของตัวแปรชนิด array เช่น int i[4]; . -> selection (structure-access) operator คือเครื่องหมายเพื่ออ้างถึงข้อมูลในตัวแปรแบบ structure เช่น s.m , s->m *(dereference) &(address) คือ เครื่องหมายที่ใช้ในการอ้างถึง address และข้อมูล (ส่วนใหญ่ใช้กับตัวแปรประเภท pointer) Operator อื่น ๆ เช่น , # ## sizeof

89 Precedence of the Operator(1)
ลำดับการดำเนินการของตัวดำเนินการต่างๆ หากมีการเขียนต่อกัน คล้ายกันกับการกำหนดความสำคัญของ operator ทางคณิตศาสตร์ a + b * c – d / e (a + (b * c)) – (d / e) 8989

90 Review Last Contents of the Class
Outline 1 Review Assignment 2 p Review Last Contents of the Class 3 Operator Types 4 Precedence of Operators 5 Assignment #2

91 Precedence of the Operator(2)

92 Precedence of the Operator(3)

93 Precedence of the Operator(4)
ใส่ลำดับก่อน avg = min + max / 2; ลำดับไม่เป็นไปตามต้องการ avg = (min + (max / 2) ); avg = (min + max) / 2;

94 Precedence of the Operator(5)
ตัวอย่าง x = * 5 – 4 / 2 % 3; x = – 4 / 2 % 3; x = – 2 % 3; x = – 2; x = 31 – 2; x = 29; result = 29

95 Test of Precedence of the Operator
#include<stdio.h> main( ) { int X; X = * 3; printf(“4 + 2 * 3 = %d\n”, X); X = ; printf(“ = %d\n”, X); X = % 7; printf(“ % 7 = %d\n”, X); X = % 27 – 32 / 4; printf(“ % 27 – 32 / 4 = %d\n”, X); }

96 Review Last Contents of the Class
Outline 1 Review Assignment 2 p Review Last Contents of the Class 3 Operator Types 4 Precedence of Operators 5 Assignment #2


ดาวน์โหลด ppt Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

งานนำเสนอที่คล้ายกัน


Ads by Google