10 2 1 Standard Library Function includestdio h
10. 2. 1 ฟงกชนไลบรารมาตรฐาน (Standard Library Function) ตวอยาง #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; } 9
ฟงกชนการคำนวณทางคณตศาสตร #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); 12
โปรแกรม 10. 1 โปรแกรมคำนวณ sin, cos, tan, sqrt, pow, log 10, exp และ fabs #include<stdio. h> #include<math. h> int main() { float a = 3. 14; float b = -123. 456; float c = 2. 7; int d = -2000; printf ("%. 3 f n", sin(a)); printf ("%. 3 f n", cos(a)); printf ("%. 3 f n", tan(a)); printf ("%. 3 f n", sqrt(a)); printf ("%. 3 f n", pow(a, c)); printf ("%. 3 f n", log(a)); printf ("%. 3 f n", log 10(a)); printf ("%. 3 f n", exp(a)); printf ("%. 3 f n", fabs(b)); printf ("%d n", abs(d)); return 0; } //������ 0. 002 -1. 000 -0. 002 1. 772 21. 964 1. 144 0. 497 23. 104 123. 456 2000 13
ฟงกชนสำหรบขอความ #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); 14
โปรแกรม 10. 2 โปรแกรมใช strcpy, strcat, strcmpi, strlen, tolower และ toupper //strlen printf("%dn", strlen(string)); #include <stdio. h> #include <string. h> #include <ctype. h> //tolower and toupper string[0] = tolower(string[0]); string[1] = toupper(string[1]); printf("%sn", string); return 0; int main(void) { char string[10]; char *str = "Abc"; } //strcpy(string, str); printf("%sn", string); //strcat(string, "Def"); printf("%sn", string); //strcmp and strcmpi printf("%dn", strcmp(str, "abc")) ; //����� Abc. Def -1 0 6 a. Bc. Def printf("%dn", strcmpi(str, "abc") ); 15
10. 2. 2 ฟงกชนทผใชงานสรางขนเอง (User-defined Function) type function-name (type); int main() {. . . y=function-name(x); . . . return 0; } type function-name (type) {. . . return ? ; } 19
สรางฟงกชนกอนฟงกชนหลก #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; } 22
สรางฟงกชนหลก #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); } 23
โปรแกรม 10. 3 โปรแกรมแสดง kmitl ในกรอบสเหลยม ***** * kmitl * ***** START show_star(n) num=9 i=1 show_star(num) i<=n kmitl F T * i++ show_star(num) end END 24
โปรแกรม 10. 3 โปรแกรมแสดง kmitl ในกรอบสเหลยม #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('*'); 25 }
10. 4. 1 ฟงกชนทไมมการรบสงคา //ตวอยางโปรแกรม #include <stdio. h> void show(void) { printf("hello"); } int main(void) { show(); return 0; } 28
10. 4. 4 ฟงกชนทไมมการรบคา แตมการสงคากลบ //ตวอยางโปรแกรม #include <stdio. h> float show(int i, float f) { printf("%f", i+f); return i*f; } int main(void) { float a; a = show(22, 3. 14); printf("%f", a); printf("%f", show(10, 12. 1)); return 0; } 32
10. 4. 3 ฟงกชนทมการรบคา และมการสงคากลบ //ตวอยางโปรแกรม #include <stdio. h> int getdata() { int k; printf("enter value"); scanf("%d", &k); return k; } int main(void) { int a; a = getdata(); printf("%d", a); printf("%d", getdata()); return 0; } 34
โปรแกรม 10. 4 โปรแกรมคำนวณ degree radian #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", °); 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); } 36
#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); } 38
#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); } 39
โปรแกรม 10. 5 โปรแกรมแสดง pass by value และ pass by reference void func(int va) { va = va+1; printf ("In function la = %dn", va); } void func 2(int *pa) { *pa = *pa +1; printf ("In function *pa = %dn", *pa); } 43
โปรแกรม 10. 5 โปรแกรมแสดง pass by value และ pass by reference 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; } 44
Q&A #include <stdio. h> #include <conio. h> int a; void func(int x) { x=10; printf("x = %dn", x); } OUTPUT x = 10 a = 3 b = 5 int main() { int b; a = 3; b = 5; func(b); printf ("a = %dn", a); printf ("b = %dn", b); return 0; } 47
Q&A #include <stdio. h> #include <conio. h> int a; void func(int *x) { *x=10; printf("x = %dn", *x); } OUTPUT x = 10 a = 3 b = 10 int main() { int b; a = 3; b = 5; func(&b); printf ("a = %dn", a); printf ("b = %dn", b); return 0; } 48
Q&A #include <stdio. h> #include <conio. h> int a; void func(int x) { a=10; printf("x = %dn", x); } OUTPUT x = 5 a = 10 b = 5 int main() { int b; a = 3; b = 5; func(b); printf ("a = %dn", a); printf ("b = %dn", b); return 0; } 49
10. 7 คำถามทายบท #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; } 51 (2)
10. 7 คำถามทายบท 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; } 52
10. 7 คำถามทายบท 3. โปรแกรมตอไปนแสดงผลเลขอะไรจอภาพ ใชเวลาคดไมเกน 10นาท เมอคดเสรจแลวใหลองพมพ code #include <stdio. h> ดงกลาวลงคอมพวเตอร และตรวจสอบคำตอบ fa(&i, j); 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) 53 (2)
10. 7 คำถามทายบท 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) 54
- Slides: 54