01006012 Computer Programming 4 1 01006012 Computer Programming

  • Slides: 64
Download presentation
01006012 Computer Programming ����� 4 ���������������������������� 1 01006012 Computer Programming

01006012 Computer Programming ����� 4 ���������������������������� 1 01006012 Computer Programming

01006012 Computer Programming Problem Algorithms Representation 6

01006012 Computer Programming Problem Algorithms Representation 6

Origami 01006012 Computer Programming 8

Origami 01006012 Computer Programming 8

ตวอยางผงงานโปรแกรม 01006012 Computer Programming #include<stdio. h> int main() { printf ("Hello World!"); 21

ตวอยางผงงานโปรแกรม 01006012 Computer Programming #include<stdio. h> int main() { printf ("Hello World!"); 21

แนวทางสรางโปรแกรมคำนวณหาพนทวงกลม (3) 01006012 Computer Programming float radius, pi, area; pi=22. 0/7; // pi =

แนวทางสรางโปรแกรมคำนวณหาพนทวงกลม (3) 01006012 Computer Programming float radius, pi, area; pi=22. 0/7; // pi = 3. 14; printf ("Enter Radius of Circular : "); scanf ("%f", &radius); area = pi * radius; printf ("Area of Circular : %f", area); 25

แนวทางสรางโปรแกรมคำนวณหาพนทวงกลม (4) 01006012 Computer Programming #include<stdio. h> int main() { float radius, pi, area;

แนวทางสรางโปรแกรมคำนวณหาพนทวงกลม (4) 01006012 Computer Programming #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; printf ("Area of Circular : %f", area); return 0; } 26

การใชงานเครองหมายทางตรรกศาสตร (2) 01006012 Computer Programming int num 1 = 10, num 2 = 20,

การใชงานเครองหมายทางตรรกศาสตร (2) 01006012 Computer Programming int num 1 = 10, num 2 = 20, num 3 = 30; num 1 == num 2 False num 1 > num 2 False (num 1<num 2) && (num 2<num 3) True (num 1>num 2) || (num 1>num 3) False (num 1>num 2) || (num 2<num 3) True 33

4. 3 การใชคำสง if 01006012 Computer Programming if (expression) statement; if (expression) { statement-1.

4. 3 การใชคำสง if 01006012 Computer Programming if (expression) statement; if (expression) { statement-1. 1; statement-1. 2; . . . statement-1. n; } 34

การใชคำสง if กบนพจน int a = ; int b = ; if (a>b) {

การใชคำสง if กบนพจน int a = ; int b = ; if (a>b) { statement 1; statement 2; statement n; } printf(); 01006012 Computer Programming a = b = ; ; a > b False True statement 1; statement 2; statement 3; 35

โปรแกรมตรวจสอบคะแนน START first, last, id, point, full first, last, id | if (4) 1

โปรแกรมตรวจสอบคะแนน START first, last, id, point, full first, last, id | if (4) 1 point/full>=0. 6 False True "passed" point, full 1 END 39

โปรแกรมตรวจสอบคะแนน | if (5) 01006012 Computer Programming #include<stdio. h> int main() { char first[20],

โปรแกรมตรวจสอบคะแนน | if (5) 01006012 Computer Programming #include<stdio. h> int main() { char first[20], last[20], id[9]; float point, full; printf scanf ("Enter your Name : "); ("%s", first); ("Enter your Surname : "); ("%s", last); ("Enter your ID : "); ("%s", id); 40

โปรแกรมตรวจสอบคะแนน | if (6) 01006012 Computer Programming printf ("Enter your examination points : ");

โปรแกรมตรวจสอบคะแนน | if (6) 01006012 Computer Programming printf ("Enter your examination points : "); scanf ("%f", &point); printf ("Enter your total points : "); scanf ("%f", &full); if ((point/full) >= 0. 6) { printf ("Name : %s %sn", first, last); printf ("ID : %sn", id); printf ("Examination points : %f / %fn", point, full); printf ("You passed, Congratulationn"); } return 0; } 41

4. 4 การใชคำสง if -else | ภายในเงอนไขมคำสงเดยว 01006012 Computer Programming if (expression) statement-1; else

4. 4 การใชคำสง if -else | ภายในเงอนไขมคำสงเดยว 01006012 Computer Programming if (expression) statement-1; else statement-2; 42

4. 4 การใชคำสง if -else | ภายในเงอนไขมหลายคำสง 01006012 Computer Programming if (expression) { statement-1.

4. 4 การใชคำสง if -else | ภายในเงอนไขมหลายคำสง 01006012 Computer Programming if (expression) { statement-1. 1; statement-1. 2; … statement-1. n; } else { statement-2. 1; statement-2. 2; … statement-2. n; } 43

โปรแกรมหารเลข START | if-else (2) 1 01006012 Computer Programming num 1, num 2 !=

โปรแกรมหารเลข START | if-else (2) 1 01006012 Computer Programming num 1, num 2 != 0 num 1 True False "Error" num 1/num 2 1 END 45

โปรแกรมหารเลข | if -else #include<stdio. h> int main() { float num 1, num 2;

โปรแกรมหารเลข | if -else #include<stdio. h> int main() { float num 1, num 2; printf ("Enter number 1 : scanf ("%f", &num 1); printf ("Enter number 2 : scanf ("%f", &num 2); if (num 2 != 0) printf ("%. 2 f / %. 2 f = else printf ("Error divided return 0; } (3) 01006012 Computer Programming "); %. 2 f", num 1, num 2, num 1/num 2); by zeron"); 46

4. 4 การใชคำสง if -else if | ภายในเงอนไขมคำสงเดยว ลกษณะผกกน 01006012 Computer Programming if (expression-1(

4. 4 การใชคำสง if -else if | ภายในเงอนไขมคำสงเดยว ลกษณะผกกน 01006012 Computer Programming if (expression-1( statement-1; else if (expression-2) statement-2; . . . else if (expression-m) statement-m; else statement-m+1; 47

การใชคำสง if -else if | ภายในเงอนไขมหลายคำสง 01006012 Computer Programming if (expression-1) { statement-1. 1;

การใชคำสง if -else if | ภายในเงอนไขมหลายคำสง 01006012 Computer Programming if (expression-1) { statement-1. 1; . . . statement-1. n; } else if (expression-2) { statement-2. 1; . . . statement-2. n; }. . else if (expression-m) { statement-m. 1; . . . statement-m. n; } else { statement-m+1. 1; . . . statement-m+1. n; } 48

START โปรแกรมตรวจสอบเกรด | if-else if first, last, id, point (5) 01006012 Computer Programming first,

START โปรแกรมตรวจสอบเกรด | if-else if first, last, id, point (5) 01006012 Computer Programming first, last, id, point>=90 True "Grade A" False a b 53

a โปรแกรมตรวจสอบเกรด point>=80 | if-else if True b (6) 01006012 Computer Programming "Grade B"

a โปรแกรมตรวจสอบเกรด point>=80 | if-else if True b (6) 01006012 Computer Programming "Grade B" False point>=70 True "Grade C" False point>=60 True "Grade D" False "Grade F" END 54

4. 4 การใชคำสง switch (expression-1) { case constant-expr-1: statement-1. 1; statement-1. 2; . .

4. 4 การใชคำสง switch (expression-1) { case constant-expr-1: statement-1. 1; statement-1. 2; . . . statement-1. n; break; case constant-expr-2: statement-2. 1; statement-2. 2; . . . statement-2. n; break; . . . 01006012 Computer Programming. . . case constant-expr-m: statement-m. 1; statement-m. 2; . . . statement-m. n; break; default: statement-d. 1; statement-d. 2; . . . statement-d. n; } 55

การใชคำสง switch | Flowchart 01006012 Computer Programming 56

การใชคำสง switch | Flowchart 01006012 Computer Programming 56

ตวอยางการใชคำสง switch (1) 01006012 Computer Programming #include<stdio. h> int main () } int d;

ตวอยางการใชคำสง switch (1) 01006012 Computer Programming #include<stdio. h> int main () } int d; printf("Enter a number from 1 to 9: "); scanf("%d& , "d ; ( switch (d ( } case 1: puts("A stitch in time saves nine ; (". break ; case 2: case 6: case 9: puts("Handsome is as handsome does ; (". break ; default: puts("Very clever. Try again. "); { return 0; { 57

ตวอยางการใชคำสง switch (2) 01006012 Computer Programming #include<stdio. h> int main () } int d

ตวอยางการใชคำสง switch (2) 01006012 Computer Programming #include<stdio. h> int main () } int d ; printf("Enter a number from 1 to 9 ; (" : scanf("%d& , "d ; ( switch (d ( } case 1: puts("A stitch in time saves nine ; (". break ; case 2: break; case 6: break; case 9: puts("Handsome is as handsome does ; (". break ; default: puts("Very clever. Try again. "); { return 0; { 58

โจทย : โปรแกรมตรวจสอบสถานะการเรยน – ตวอยางการรนโปรแกรม Enter GPA (past) : 0. 74 Your Status :

โจทย : โปรแกรมตรวจสอบสถานะการเรยน – ตวอยางการรนโปรแกรม Enter GPA (past) : 0. 74 Your Status : Retired Sorry, Try again. Enter GPA (past) : 1. 50 Your Status : Probation Enter GPS (present) : 2. 00 Enter GPA (present) : 1. 75 Your Status : Probation Enter GPA (past) : 1. 50 Your Status : Probation Enter GPS (present) : 3. 0 Enter GPA (present) : 2. 25 Your Status : Normal (3) 01006012 Computer Programming Enter GPA (past) : 2. 50 Your Status : Normal Enter GPS (present) : 1. 00 Enter GPA (present) : 1. 75 Your Status : Probation Enter GPA (past) : 1. 50 Your Status : Probation Enter GPS (present) : 1. 00 Enter GPA (present) : 1. 25 Your Status : Retired Sorry, Try agin. 61