01006012 Computer Programming includestdio h includeconio h includemath

  • Slides: 40
Download presentation

ตวอยาง 01006012 Computer Programming #include<stdio. h> #include<conio. h> #include<math. h> int main() { double

ตวอยาง 01006012 Computer Programming #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

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

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

ตวอยางการใชงานฟงกชนการคำนวณทางคณ 01006012 Computer Programming ตศาสตร #include<stdio. h> #include<conio. h> #include<math. h> #define PI 3.

ตวอยางการใชงานฟงกชนการคำนวณทางคณ 01006012 Computer Programming ตศาสตร #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

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

ฟงกชนสำหรบขอความ 01006012 Computer Programming #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 01006012 Computer Programming type function-name (type) {. . . return ? ; } 20

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

สรางฟงกชนกอนฟงกชนหลก 01006012 Computer Programming #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); } 01006012 Computer Programming 24

ตวอยาง ***** * kmitl * ***** | ผงงาน & ผลลพธ 01006012 Computer Programming START

ตวอยาง ***** * kmitl * ***** | ผงงาน & ผลลพธ 01006012 Computer Programming 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; } 01006012 Computer Programming #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 }

ฟงกชนทไมมการรบสงคา 01006012 Computer Programming • เปนฟงกชนท ไมม การรบคาเขามาในฟงกช น และไมม การสงคากลบออกไปจากฟงกชน #include<stdio. h> #include<conio.

ฟงกชนทไมมการรบสงคา 01006012 Computer Programming • เปนฟงกชนท ไมม การรบคาเขามาในฟงกช น และไมม การสงคากลบออกไปจากฟงกชน #include<stdio. h> #include<conio. h> void function_name(void); int main() {. . . function_name(); . . . return 0; } void function_name() { statement-1; statement-2; . . . statement-n; } 29

ตวอยาง ฟงกชนทไมมการรบสงคา 01006012 Computer Programming #include<stdio. h> void Print. Hello(void); int main() { Print.

ตวอยาง ฟงกชนทไมมการรบสงคา 01006012 Computer Programming #include<stdio. h> void Print. Hello(void); int main() { Print. Hello(); printf("Hello, in function main. n"); Print. Hello(); return 0; } void Print. Hello(void) { printf("Hello, in function Print. Hello. . nn"); } 30

ตวอยาง ฟงกชนทมการรบคา แตไมสงคากลบ #include<stdio. h> void Print. Hello( int ); int main() { int

ตวอยาง ฟงกชนทมการรบคา แตไมสงคากลบ #include<stdio. h> void Print. Hello( int ); int main() { int n; printf("input number of hello : "); scanf("%d", &n); Print. Hello( n ); return 0; } void Print. Hello( int i ) { int count; for ( count=1; count<=i ; count++ ) printf("%d HELLOn", count); } 01006012 Computer Programming 32

ฟงกชนทมการรบคา และมการสงคากลบ 01006012 Computer Programming • เปน ฟงกชนทมการรบคาเขามาในฟงกชน และมการสงคากลบออกไปจากฟงกชน #include<stdio. h> #include<conio. h> type

ฟงกชนทมการรบคา และมการสงคากลบ 01006012 Computer Programming • เปน ฟงกชนทมการรบคาเขามาในฟงกชน และมการสงคากลบออกไปจากฟงกชน #include<stdio. h> #include<conio. h> type func_name(type); int main() {. . . var = func_name(var. X); . . . return 0; } type func_name(type var. Y) { statement-1; statement-2; . . . statement-n; return(var. Z); } 33

ตวอยาง ฟงชนทมรบคา มสงคา #include<stdio. h> 01006012 Computer Programming float Circle. Area( int ); int

ตวอยาง ฟงชนทมรบคา มสงคา #include<stdio. h> 01006012 Computer Programming float Circle. Area( int ); int main() { int radius; printf("input radius : "); scanf("%d", &radius); printf(" Circle area = %fn", Circle. Area(radius)); return 0; } float Circle. Area( int rad ) { float answer=0; answer = 22. 0/7. 0*rad ; return answer; } 34

ตวอยาง ฟงกชนทไมมการรบคา แตมการสงคากลบ #include<stdio. h> 01006012 Computer Programming int Round. Input( void ); int

ตวอยาง ฟงกชนทไมมการรบคา แตมการสงคากลบ #include<stdio. h> 01006012 Computer Programming int Round. Input( void ); int main() { int i, round; round = Round. Input(); for( i=1; i<=round; i++) printf("hello #%dn", i); return 0; } int Round. Input(void) { int answer; printf( "Please input number of hello : "); scanf( "%d", &answer ); return answer; } 36

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

#include<stdio. h> #include<math. h> #define PI 3. 14 01006012 Computer Programming 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); } 38

01006012 Computer Programming #include<stdio. h> int main() { int N; printf("Input N: "); scanf("%d",

01006012 Computer Programming #include<stdio. h> int main() { int N; printf("Input N: "); scanf("%d", &N); if ( Is. Prime(N) ) printf("%d is Prime number ", N ); else printf("%d is not Prime number ", N ); return 0; } 40