Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore c_language

c_language

Published by aunchaleechanpraphai, 2021-01-11 05:16:45

Description: c_language

Search

Read the Text Version

ตวั อย่างการประมวลผล โดยพิจารณาลาดบั ความสาคญั i = 8; j = 4.5; k = ‘z’; // ascii ของ z เท่ากบั 122 Expression ผลลัพธท์ ่ไี ด้ ค่าท่ไี ด้ i + j <= 10 False 0 1 i >= 8 && k == ‘z’ True && True == True 1 0 k != ‘a’ || i + j <= 10 True || False == True 1 k != ‘a’ || i < j && k < i True || False && False (ตวั อย่างท่ไี ม่ถูกต้อง) == True && False 51 == False k != ‘a’ || i < j && k < i True || False && False (ตวั อย่างท่ถี ูกต้อง) == True || False == True

การควบคุมการไหลของโปรแกรม • คาส่งั กาหนดเง่ือนไข  โครงสร้าง if  โครงสร้าง if…else  โครงสร้าง if แบบหลายเง่อื นไข  โครงสร้าง switch-case • คาส่งั วนซา้  โครงสร้าง while loop  โครงสร้าง do…while loop  โครงสร้าง for loop • โปรแกรมย่อย (ฟังกช์ ัน) 52

โครงสรา้ ง if C Syntax Flowchart if (condition) START { true statement1; condition : false Statement statementN; } • ส่วนของ condition ตีความเป็น Statement ข้อมลู แบบ int END • ทาคาส่งั ใน {} หาก condition เป็นจริง (ไม่เป็นศูนย์) • หากมีคาส่งั เดยี วไม่จาเป็นต้องใช้ วงเลบ็ ปี กกา 53

ตวั อย่าง if if1.c #include <stdio.h> I>J int main() { int i=101,j=100; if(i>j) printf(\"I > J\"); getch(); return 0; } 54

โครงสรา้ ง if…else Flowchart C Syntax START if (condition) { true false condition statementt1; statementt2; Statementt1 Statementf1 } Statementt2 Statementf2 else { END statementf1; statementf2; } 55

#include <stdio.h>ตวั อยา่ ง if…else int main() ifelse1.c { int i=101,j=102; I <= J if(i>j) printf(\"I > J\"); else printf(\"I <= J\"); getch(); return 0; } 56

โครงสรา้ ง if แบบหลายเงือ่ นไข x==1 true Action1; if (x==1) Action2; Action1; false true Action3; true Action4; else if (x==2) x==2 Action2; false else if (x==3) Action3; x==3 else if (x==4) false true Action4; x==4 else Default_Action; false Default_Action; 57

ตวั อยา่ ง if แบบหลายเงือ่ นไข #include <stdio.h> ifelse2.c int main() { int i=7; >6 if(i>7) printf(\"> 7\"); else if(i>6) printf(\"> 6\"); else if(i>5) printf(\"i> 5\"); else printf(\"1 , 2 , 3\"); getch(); return 0; } 58

โครงสรา้ ง switch-case x==1 true Action1; switch (x) Action2; { false true Action3; true Action4; case 1: Action1; x==2 break; false case 2: Action2; break; x==3 case 3: Action3; false true break; x==4 case 4: Action4; break; false default: Default_Action; Default_Action; break; } 59

#include <stdio.h> ตวั อย่าง switch-case int main() switch1.c { int i=2; switch(i) 2 { case 2 : printf(\"2\"); break; case 1 : printf(\"1\"); break; default : printf(\"NO MATCH\"); break; } getch(); return 0; } 60

ทาใบงานท่ี 3.1-3.5 61

การกาหนดค่าตวั นบั ตวั อย่าง i++ = i = i+1 i-- = i = i-1 i+=5 = i = i+5 i-=5 = i = i-5 62

โครงสรา้ ง while ลูป while (condition) START { false stmt1; condition stmt2; true : Statement stmtN; Statement } • วนทาคาส่งั stmt1 ถงึ stmtN ตราบเทา่ ท่ี condition เป็น จริง END 63

ลูปวนนบั (Counting Loop) • หากพิจารณาโครงสร้างของลูปท่ใี ช้ในโปรแกรมส่วนใหญ่ มกั จะเป็นลูปแบบวนนับ • ลูปวนนับจะมีส่วนประกอบดังตัวอย่างต่อไปน้ีเสมอ ตัวแปรท่ใี ช้นับ คาส่งั ท่ถี ูกทาซา้ int i, sum = 0; ส่วนกาหนดค่า การปรับค่าตวั นับ i = 1; เร่ิมต้น while (i <= 10) { เง่อื นไขของตัวนับ sum = sum + i; i = i + 1; } printf(\"Sum = %d\\n\", sum); 64

ตวั อยา่ งโครงสรา้ ง while ลูป #include <stdio.h> while1.c int main() { Hello 1 Hello 2 int i=1; Hello 3 while(i<=10) Hello 4 { Hello 5 printf(\"Hello %d\\n\",i); Hello 6 Hello 7 i++; Hello 8 } Hello 9 getch(); Hello 10 return 0; } 65

โครงสรา้ ง while ลูป(INFINITY LOOP) #include <stdio.h> int main() while2.c { Hello 1 int i=1; Hello 2 while(1) Hello 3 { Hello 4 printf(\"Hello %d\\n\",i); Hello 5 Hello 6 if(i==10) Hello 7 break; Hello 8 i++; Hello 9 } Hello 10 getch(); return 0; } 66

โครงสรา้ ง do…while ลูป do START { Statement1 stmt1; StatementN stmt2; true : condition stmtN; } while (condition); false END • ทาคาส่งั stmt1...stmtN และวนทาซา้ อกี ตราบเท่าท่ี condition ยงั คงเป็ นจริง • น่นั คอื stmt1...stmtN จะถูก กระทาอย่างน้อยหน่งึ คร้ัง 67

ตวั อย่าง do…while ลูป #include <stdio.h> dowhile1.c int main() Hello 1 { Hello 2 int i=1; Hello 3 do Hello 4 { Hello 5 printf(\"Hello %d\\n\",i); Hello 6 i++; Hello 7 } Hello 8 while(i<=10); Hello 9 getch(); Hello 10 return 0; } 68

โครงสรา้ ง for ลูป • เป็นโครงสร้างท่ใี ห้ความสะดวกในการเขยี นลูปวนนบั for (init_stmt; condition; update_stmt) { statement1; statement2; : statementN; } • การทางาน 1. ทาคาส่งั init_stmt หน่ึงคร้ัง 2. ถ้า condition เป็ นจริง ทาคาส่งั statement1...statementN 3. ทาคาส่งั update_stmt จากน้ันกลับไปทาข้อ 2 69

การทางานของ for ลูป START for (init_stmt; condition; update_stmt) init_stmt { false statement1; condition statement2; true : Statement1 statementN; } StatementN update_stmt END 70

ตวั อย่าง for ลูป #include <stdio.h> for1.c int main() { Hello 1 int i; Hello 2 Hello 3 for(i=1;i<=10;i++) Hello 4 { Hello 5 printf(\"Hello %d\",i); Hello 6 Hello 7 printf(\"\\n\"); Hello 8 } Hello 9 getch(); Hello 10 return 0; } 71

โปรแกรมย่อย (Subroutine) • ในภาษาซีเรียกว่า \"ฟังกช์ นั \" (Function) • เป็นส่วนของโปรแกรมท่มี หี น้าท่กี ารทางานชัดเจนในตัวเอง ซ่งึ ถูกเรียกใช้ในโปรแกรมหลกั อกี ทหี น่งึ • การเขยี นโปรแกรมโดยแยกเป็นฟังกช์ ันมีข้อดหี ลายประการ  ช่วยแบ่งงานท่ซี ับซ้อนเป็นงานย่อยหลายงาน  ลดการเขยี นโค้ดท่ซี า้ ซ้อน  ซ่อนรายละเอยี ดไว้ในส่วนอ่นื ทาให้โปรแกรมเข้าใจได้ง่ายข้นึ  ฟังกช์ ันท่เี ขยี นข้นึ มาสามารถนาไปใช้ในโปรแกรมอ่นื ได้ 72

ชนดิ ของฟังกช์ นั • ฟังกช์ ันมาตรฐาน (Standard Functions)  เป็นฟังกช์ ันท่อี ยู่ในชุดไลบรารีของภาษาซี เรียกใช้ได้ทนั ที  เช่น printf(), scanf(), ... • ฟังกช์ ันท่ผี ู้ใช้กาหนด (User Defined Functions)  เป็นฟังกช์ ันท่ผี ู้ใช้สร้างข้นึ เอง  เรียกใช้ใน main() หรือจากฟังกช์ ันอ่นื ๆ ได้เหมอื นฟังกช์ ัน มาตรฐาน 73

ตวั อย่างฟังกช์ นั มาตรฐาน กล่มุ ฟังกช์ นั เฮดเดอรไ์ ฟล์ ตวั อย่างฟังกช์ นั จดั การอนิ พทุ /เอาทพ์ ุท stdio.h scanf, printf, gets, คณติ ศาสตร์ math.h puts แยกประเภทข้อมูลอกั ขระ ctype.h sin, cos, exp, pow isalpha, isdigit, จดั การกบั สตริง string.h islower, isupper strlen, strcpy, อ่นื ๆ stdlib.h strcmp rand, atoi, atof 74

ฟังกช์ นั ทีผ่ ูใ้ ชก้ าหนดเอง • แบบไม่ส่งค่ากลับ void say_hi(char *name) {  ระบุชนดิ ข้อมูล void  ไม่ต้องมีคาส่งั return printf(\"Hi, %s\\n\", name); } • แบบส่งค่ากลับ int max(int a, int b) {  ระบชุ นิดข้อมูลท่ตี ้องการ  ใช้คาส่งั return ส่งค่าคืน if (a > b) return a; ตามชนดิ ท่รี ะบุ else return b; } 75

การไหลของโปรแกรมเมอื่ ใชฟ้ ังกช์ นั #include <stdio.h> function1.c int incr(int i) { int j; j = i + 1; return j; } int main() { int k, m = 4; k = incr(m); printf (\"k = %d, m = %d\\n\", k, m); getch(); return 0; } 76

โปรแกรมแสดงขอ้ ความ (Function) แบบไม่ส่งค่ากลบั #include <stdio.h> function2.c void print1() { 77 printf(“Hello”); printf(“\\n”); } int main() { print1(); print1(); getch(); return 0; }

โปรแกรมคานวณภาษี (Function) แบบส่งค่ากลบั #include <stdio.h> function3.c float cal_tax(float i) { float ctax; ctax = i*0.07; return ctax; } int main() { float money=7290,ff; ff = cal_tax(money); printf(“%f”,ff); getch(); return 0; } 78

ข้นั ตอนวิธีกบั การโปรแกรม • การออกแบบข้นั ตอนวิธเี ป็นส่วนสาคัญในการพัฒนา โปรแกรม • การ \"เขียนโปรแกรม\" เป็นเพียงการแปลงข้นั ตอนวิธีให้ อยู่ในรปู ท่ยี อมรับได้โดยตวั ภาษา 79

ตวั อย่าง • เขียนโปรแกรมเพ่ือรับตัวเลขระบุขนาด และพิมพ์รูป สามเหล่ียมท่มี ีขนาดตามท่กี าหนด Enter N: 3 Enter N: 5 * * ** ** *** *** **** ***** 80

ขอ้ ผดิ พลาดทีพ่ บบ่อย #include <stdio.h> int main () ส่งค่า i ให้ scanf แทนท่จี ะส่ง { ตาแหน่ง int i; ใช้คาส่งั กาหนดค่า (=) แทน การเปรียบเทยี บ (==) scanf(\"%d\", i); if (i = 0) puts(\"false\"); else puts(\"true\"); return 0; } 81

THANK YOU QUESTION ? 82


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook