includestdio h includeconio h includemath h int main
- Slides: 55
ตวอยาง #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); exp(var); fabs(var); 13
ตวอยางการใชงานฟงกชนการคำนวณทางคณ ตศาสตร #include<stdio. h> #include<conio. h> #include<math. h> #define PI 3. 14 int main() { float deg, rad; printf ("Enter Degree : "); scanf ("%f", °); 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 2); strlen(str); #include<ctype. h> tolower(ch); toupper(ch); 15
ฟงกชนทสรางขนเอง 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; 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; . . . 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 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; 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*/ 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); 34 }
#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; 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 la = %dn", va); } 43
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 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
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; 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; 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 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. 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); ดงกลาวลงคอมพวเตอร และตรวจสอบคำตอบ 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; } 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
- Sum0
- 1 num
- Divideint
- Int max(int x int y)
- Interface calculator public int add class test
- Public void drawsquare(int x, int y, int len)
- Int main() int num=4
- Include
- Int main(int argc, char** argv)
- Nnxn com
- #includeconio.h
- Void main int main
- Int f(int n)
- Interface myinterface int foo(int x)
- Int b
- Const int vs int arduino
- 7팩토리얼
- Voidswap
- #includestdio.h
- #includestdio.h
- #include iostream using namespace std int main()
- 윤년 계산
- Include stdio.h
- Main() int x y m n
- Void main() int i j charval='j'
- Include stdlib h
- Int main
- Void main() int i j charval='j'
- Mainstring
- 저장 유형 지정자
- #include stdio.h int main()
- Dabe main daba dabe main cake
- A.1 determine the main idea of a passage
- Byte short long
- Vert opp angle
- Pointer and one dimensional array
- Inputstream to string
- Int grade
- Include
- String value of int
- Int x = 0; for (x=1; x<4; x++); printf("x=%d ", x);
- G int
- Class a void foo() throws exception
- Unsigned short int
- Unsigned short int
- Typedef struct tree int info struct *left
- Node leaf
- Typedef struct tree int info
- Int 32