includestdio h includeconio h includemath h int main

  • Slides: 55
Download presentation

ตวอยาง #include<stdio. h> #include<conio. h> #include<math. h> int main() { double rad = -1.

ตวอยาง #include<stdio. h> #include<conio. h> #include<math. h> int main() { double rad = -1. 0; do { printf ( "Sine of %f is %fn", rad, sin(rad)); rad += 0. 1; } while (rad <= 1. 0); return 0; } 10

ฟงกชนการคำนวณทางคณตศาสตร #include<math. h> sin(var); cos(var); tan(var); sqrt(var); pow(var 1, var 2); log(var); log 10(var);

ฟงกชนการคำนวณทางคณตศาสตร #include<math. h> sin(var); cos(var); tan(var); sqrt(var); pow(var 1, var 2); log(var); log 10(var); exp(var); fabs(var); 13

ตวอยางการใชงานฟงกชนการคำนวณทางคณ ตศาสตร #include<stdio. h> #include<conio. h> #include<math. h> #define PI 3. 14 int main()

ตวอยางการใชงานฟงกชนการคำนวณทางคณ ตศาสตร #include<stdio. h> #include<conio. h> #include<math. h> #define PI 3. 14 int main() { float deg, rad; printf ("Enter Degree : "); scanf ("%f", &deg); rad = deg * PI / 180; printf ("sin(%. 2 f) = %. 3 fn", deg, sin(rad)); printf ("cos(%. 2 f) = %. 3 fn", deg, cos(rad)); printf ("tan(%. 2 f) = %. 3 fn", deg, tan(rad)); return 0; } 14

ฟงกชนสำหรบขอความ #include<string. h> strcpy(str 1, str 2); strcat(dest 1, src 2); strcmpi(str 1, str

ฟงกชนสำหรบขอความ #include<string. h> strcpy(str 1, str 2); strcat(dest 1, src 2); strcmpi(str 1, str 2); strlen(str); #include<ctype. h> tolower(ch); toupper(ch); 15

ฟงกชนทสรางขนเอง Function) type function-name (type); int main() {. . . y=function-name(x); . . .

ฟงกชนทสรางขนเอง Function) type function-name (type); int main() {. . . y=function-name(x); . . . return 0; } (User-defined type function-name (type) {. . . return ? ; } 20

สรางฟงกชนกอนฟงกชนหลก #include<file. h> type variable type function_name(type variable) { statement-1; . . . statement-n;

สรางฟงกชนกอนฟงกชนหลก #include<file. h> type variable type function_name(type variable) { statement-1; . . . statement-n; return(var); } int main() { type variable; statement-1; . . . statement-n; return 0; } 23

สรางฟงกชนหลก #include<file. h> type function_name(type); type variable int main() { type variable; statement-1; .

สรางฟงกชนหลก #include<file. h> type function_name(type); type variable int main() { type variable; statement-1; . . . statement-n; return 0; } type function_name(type variable) { statement-1; . . . statement-n; return(var); } 24

ตวอยาง ***** * kmitl * ***** | ผงงาน & ผลลพธ START show_star(n) num=9 i=1

ตวอยาง ***** * kmitl * ***** | ผงงาน & ผลลพธ START show_star(n) num=9 i=1 show_star(num) i<=n kmitl F T * i++ show_star(num) end END 25

ตวอยาง | โปรแกรม #include<stdio. h> #include<conio. h> void show_star (int n) { int i;

ตวอยาง | โปรแกรม #include<stdio. h> #include<conio. h> void show_star (int n) { int i; for (i=1; i<=n; i++) putchar('*'); } int main() { int num=9; show_star(num); printf ("n* kmitl *n"); show_star(num); return 0; } #include<stdio. h> #include<conio. h> void show_star (int); int main() { int num=9; show_star(num); printf ("n* kmitl *n"); show_star(num); return 0; } void show_star (int n) { int i; for (i=1; i<=n; i++) putchar('*'); 26 }

#include<stdio. h> #include<conio. h> #include<math. h> #define PI 3. 14 float deg_rad(float); /*Function Prototype*/

#include<stdio. h> #include<conio. h> #include<math. h> #define PI 3. 14 float deg_rad(float); /*Function Prototype*/ int main() { float deg, rad; printf ("Enter Degree : "); scanf ("%f", &deg); rad = deg_rad(deg); //printf ("%f->%fn", deg, rad); printf ("sin(%. 2 f) = %. 3 fn", deg, sin(rad)); printf ("cos(%. 2 f) = %. 3 fn", deg, cos(rad)); printf ("tan(%. 2 f) = %. 3 fn", deg, tan(rad)); return 0; } float deg_rad(float num) { float ans; ans = num * PI / 180; return(ans); 34 }

#include<stdio. h> #include<conio. h> int num 1; void test(void); /*Function Prototype*/ int main() {

#include<stdio. h> #include<conio. h> int num 1; void test(void); /*Function Prototype*/ int main() { num 1 = 19; printf ("line 1 (main) : num 1 = %dn", num 1); test(); printf ("line 2 (main) : num 1 = %dn", num 1); return 0; } void test() { num 1 = 26; printf ("line 1 (test) : num 1 = %dn", num 1); } 37

#include<stdio. h> #include<conio. h> void test(void); /*Function Prototype*/ int main() { int num 1;

#include<stdio. h> #include<conio. h> void test(void); /*Function Prototype*/ int main() { int num 1; num 1 = 19; printf ("line 1 (main) : num 1 = %dn", num 1); test(); printf ("line 2 (main) : num 1 = %dn", num 1); return 0; } void test() { int num 1; num 1 = 26; printf ("line 1 (test) : num 1 = %dn", num 1); } 38

Pass by Value Function void func(int va) { va = va+1; printf ("In function

Pass by Value Function void func(int va) { va = va+1; printf ("In function la = %dn", va); } 43

Pass by Reference Function void func 2(int *pa) { *pa = *pa +1; printf

Pass by Reference Function void func 2(int *pa) { *pa = *pa +1; printf ("In function *pa = %dn", *pa); } 44

Main Function int main() { int x; x = 10; printf ("Before call function

Main Function int main() { int x; x = 10; printf ("Before call function x = %dn", x); func(x); printf ("After call function x = %dn", x); printf ("nn"); x = 10; printf ("Before call function x = %dn", x); func 2(&x); printf ("After call function x = %dn", x); return 0; } 45

Output 46

Output 46

Q&A #include <stdio. h> #include <conio. h> int a; void func(int x) { x=10;

Q&A #include <stdio. h> #include <conio. h> int a; void func(int x) { x=10; printf("x = %dn", x); } int main() { int b; a = 3; b = 5; func(b); printf ("a = %dn", a); printf ("b = %dn", b); return 0; } OUTPUT x = 10 a = 3 b = 5 48

Q&A #include <stdio. h> #include <conio. h> int a; void func(int *x) { *x=10;

Q&A #include <stdio. h> #include <conio. h> int a; void func(int *x) { *x=10; printf("x = %dn", *x); } int main() { int b; a = 3; b = 5; func(&b); printf ("a = %dn", a); printf ("b = %dn", b); return 0; } OUTPUT x = 10 a = 3 b = 10 49

Q&A #include <stdio. h> #include <conio. h> int a; void func(int x) { a=10;

Q&A #include <stdio. h> #include <conio. h> int a; void func(int x) { a=10; printf("x = %dn", x); } int main() { int b; a = 3; b = 5; func(b); printf ("a = %dn", a); printf ("b = %dn", b); return 0; } OUTPUT x = 5 a = 10 b = 5 50

คำถามทายบท (2) #include <stdio. h> #include <string. h> struct product { char name[15]; int

คำถามทายบท (2) #include <stdio. h> #include <string. h> struct product { char name[15]; int number; float price; }; struct product getdata(void) { struct product x; strcpy(x. name, "Chewing gum"); x. number = 100; x. price = 25. 5; return x; } ……… showdata(………………) { …………………. . } void sub 10(struct product &x) { if (x. number > 10) x. number -=10; } (1) …………add 10(………………. . ) { x. number +=10; } int main(void) { int i; struct product y; y = getdata(); showdata(y); add 10(y); showdata(y); sub 10(y); showdata(y); return 0; } 52 (2)

คำถามทายบท (3) 2. ใหนกศกษาอธบายการทำงานของโปรแกรมน Hint: มทผด 1 จด พรอมกบชใหเหนวาจดผดของโปรแกรมคอจด #include <stdio. h> #include <string.

คำถามทายบท (3) 2. ใหนกศกษาอธบายการทำงานของโปรแกรมน Hint: มทผด 1 จด พรอมกบชใหเหนวาจดผดของโปรแกรมคอจด #include <stdio. h> #include <string. h> void callnum(int i, char y) { char data [10][8] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; strcpy(y, data[i]); } int main(void) { char ch[10]; int i; int telno[] = {0, 2, 7, 3, 0, 0, 0}; for (i=0; i<9; i++) { callnum(telno[i], ch); printf("%s ", ch); } return 0; 53 }

คำถามทายบท (4) 3. โปรแกรมตอไปนแสดงผลเลขอะไรจอภาพ ใชเวลาคดไมเกน เมอคดเสรจแลวใหลองพมพ code #include <stdio. h> fa(&i, j); ดงกลาวลงคอมพวเตอร และตรวจสอบคำตอบ

คำถามทายบท (4) 3. โปรแกรมตอไปนแสดงผลเลขอะไรจอภาพ ใชเวลาคดไมเกน เมอคดเสรจแลวใหลองพมพ code #include <stdio. h> fa(&i, j); ดงกลาวลงคอมพวเตอร และตรวจสอบคำตอบ 10นาท int i; printf("n i=%d, j=%d", i, j); struct data { fb(i, &j); int i; printf("n i=%d, j=%d", i, j); int j; fc(X, &Y, Z); }W; printf("n W. i=%d, W. j=%d", W. i, W. j); printf("n X. i=%d, X. j=%d", X. i, X. j); void fa(int *a, int b); printf("n Y. i=%d, Y. j=%d", Y. i, Y. j); void fb(int c, int *d); printf("n Z. i=%d, Z. j=%d", Z. i, Z. j); void fc(struct data x, struct data *y, return 0; struct data z); } int main(void) { void fa(int *a, int b) { int j=0; i = 4; int i = 15; *a = *a+3; struct data X, Y, Z; b = i+2; X. i = 10; } X. j = 20; (1) 54 (2)

คำถามทายบท (5) void fb(int c, int *d) { c += i; *d += i;

คำถามทายบท (5) void fb(int c, int *d) { c += i; *d += i; } void fc(struct data x, struct data *y, struct data z) { y->i = x. j++; y->j = y->i + (++x. i); z. i = x. j; z. j = y->j; W. i = y->i + (++x. j); W. j = y->j + z. i; } (3) 55