More Related Content
Similar to โปรแกรมย่อยและฟังก์ชันมาตรฐาน
Similar to โปรแกรมย่อยและฟังก์ชันมาตรฐาน (20)
โปรแกรมย่อยและฟังก์ชันมาตรฐาน
- 2. ก (standard functions)
ก C !" # $ ก% &
' header file C ()* ก% &' # + ,ก- *.h 0 1 )* * ก ' ก
' 2$ * 3&0 ก " * 30' header file ' 2 ก " 2! (0* '
(4 , #include<header file.h> ' ,0& * * 6 #ก 2! 2$, 7'
ก *ก 8! ก ก 3 C compiler !"
9)*': 3' 4 0& ' ก 6 #ก 4 ':ก 6 #ก ,$ &ก# $ 0 !" ("
* 22$ ก ก &0 ; ก ; (library functions)
- 3. ก ก pow(x,y) ก
กก ! xy " # x $ # y % #& double '(
ก pow(x,y) )$*+ก ก,-. header file math.h / )(
#include<math.h> ก + ! "# ก
ก main )( )$ * ก ก pow(x,y) 1 "# ก
/.
- 4. ก (mathematic functions)
ก ' ,4 : ก (4 &< (< = , # $ก0* 2$' ก $ " 2$ *
' (4 , #include <math.h> # ก* 30 * * 6 #ก # $ &# 2$'
ก $ "2$ * (type) double )* 2 ก 9> 2 ก ก
$ "2$ (0 ,0 ก * * 3 double 0 ก
ก ! " #
acos(x) asin(x) atan(x)
sin(x) cos(x) tan(x)
sqrt(x) exp(x) pow(x,y)
log(x) log10(x) ceil(x)
floor(x) fabs(x)
- 5. 1) ก acos(x) ก ' (4 &<: (0 arc cosine * x 6 x (0
- ' : 0& (radian) acos(x);
2) ก asin(x) #& ก 2 arc sine ! x " x #&
3 asin(x);
3) ก atan(x) #& ก 2 arc tan ! x " x #&
3 atan(x);
4) ก sin(x) #& ก 2 sine ! x " x #& 3
sin(x);
5) ก cos(x) #& ก 2 cosine ! x "
x #& 3 cos(x);
6) ก tan(x) #& ก 2 tan ! x " x #& 3
tan(x);
- 6. ก 7.1 #, ก ' ก acos(x), asin(x), atan(x), sin(x),
cos(x) # $ tan(x)
/* math1.c */
#include<stdio.h> /* 1 */
#include<math.h> /* 2 */
#include<conio.h> /* 3 */
void main(void) /* 4 */
{ /* 5 */
double r, pi = 3.141592654; /* 6 */
r = pi/180; /* 7 */
clrscr(); /* 8 */
printf("%fn",asin(r)); /* 9 */
printf("%fn",acos(r)); /* 10 */
printf("%fn",atan(r)); /* 11 */
printf("%fn",sin(r)); /* 12 */
printf("%fn",cos(r)); /* 13 */
printf("%fn",tan(r)); /* 14 */
printf("nPress any key back to program ..."); /* 15 */
getch(); /* 16 */
} /* 17 */
- 8. #$ % & '! ก"
! " 9 (4 , printf("%fn",asin(r)); ก (4 &<: (0 arc
sin * &# r 6 r - ' : 0& # $#, 9> **ก2* 9
10 (4 , printf("%fn",acos(r)); ก (4 &<: (0 arc
cosine * &# r 6 r - ' : 0& # $#, 9> **ก
2* 9
! " 11 (4 , printf("%fn",atan(r)); ก (4 &<: (0 arc
tan * &# r 6 r - ' : 0& # $#, 9> **ก2* 9
! " 12 (4 , printf("%fn",sin(r)); ก (4 &<: (0 sine * &
# r6 r - ' : 0& # $#, 9> **ก2* 9
- 9. ! " 13 (4 , printf("%fn",cos(r)); ก (4 &<:
(0 cosine * &# r 6 r - ' : 0& # $#, 9>
**ก2* 9
! " 14 (4 , printf("%fn",tan(r)); ก (4 &<:
(0 tan * &# r 6 r - ' : 0& # $#, 9> **ก
2* 9
! " 15 16 9 9 *(& ':ก ( ' 1 9)*ก ,306 #ก # $: - *
(0 ' 1 0 ก enter 2$ก ,306 #ก
- 10. 7) ก sqrt(x)
ก ' : (0 ก 2 (square root) * (0 ( : )* &# x 6
x 2$ * (0 ( & : )* &# (0 0
3 # sqrt(x);
8) ก exp(x)
#& ก ex " x #& # )$ #& ก
ก ! e " e # $ 2 2.718282
+# -- exp(x);
9) ก pow(x,y)
#& ก xy "
+# -- pow(x, y);
- 11. ! ก "*+%', 7.2
ก ก sqrt(x), exp(x) $ pow(x, y)
/* math2.c */
#include<stdio.h> /*
1 */
#include<math.h> /*
2 */
#include<conio.h> /*
3 */
void main(void) /*
4 */
{ /* 5 */
double x = 2.5, y = 7.0, z = 21.5; /* 6 */
clrscr( ); /* 7 */
printf("%.4fn",pow(x,y)); /* 8 */
printf("%.4fn",sqrt(z)); /* 9 */
printf("%.4fn",exp(y)); /*
10 */
printf("nPress any key back to program ..."); /* 11 */
getch(); /* 12 */
} /* 13 */
- 13. #$ % & '! ก"
) ก"# ก 7.2 * 9%- ก ! "# ก :; . /
8 printf("%.4fn",pow(x,y)); ก 2
xy " x #& # '( )$ กก <+ $
y #& # #& กก $ = . ก) 1 >
9 printf("%.4fn",sqrt(z)); ก 2
ก (square root) ! # z" z )$
#& % ! # . % - $ = . )1>
10 printf("%.4fn",exp(y)); ก 2
ey " y #& # )$ #& กก ! e "
e # $ 2 2.718282 $ = . ก) 1 >
11 1 12 >% >! ก ;>ก - +
"# ก $ 3 - ; ก enter )$ก - ! +"# ก
- 14. 10) ก log(x)
#& ก log n (natural logarithm) !
# x " x #& # #& -. .
log(x);
11) ก log10(x)
ก log 10 ! # x " x #&
# #& -. .
log10(x);
12) กceil(x)
#& ก ก # <A < % !(/ ! # x * x #& !
) < % * x #& !) , )$. ก # <A < %
ceil(x);
- 15. 13) ก floor(x)
#& ก ก <A < % %/ ! # x * x #& !)
< % * x #& !) , )$. ก <A < % %/
floor(x);
14. ก fabs(x)
#& ก -+ 2 (absolute value) !
# x " x #& # ก,- !)3 < % -ก
-ก,. fabs(x);
- 16. ก + 2 ,%'3
1) ก clrscr( )
#& ก ก -! + ก) ก) 1 > -- text mode
+# -- clrscr( );
2) ก gotoxy(x,y)
#& ก / (cursor) .# $-3. - ) 1 >
+# -- gotoxy(x,y );
3) ก clreol( )
#& ก -! - cursor +" -! * )ก
! cursor .#) ก $ )--
+# -- clreol( );
- 17. 4) กdeline( )
#& ก -! /- cursor + ) ก / ก, !
- +! !(/
+# -- deline( );
5) ก insline( )
#& ก ก- 1- " ก + - cursor +
+# -- insline( );
6) ก sizeof(x)
#& ก ) -! ! # x ! ก Byte
+# -- sizeof(x); sizeof(type);
7) ก system( )
#& ก * ก + MS-DOS .
+# -- system(“dos-command”);
- 18. 8) ก abort( )
#& ก ก %กก ! "# ก " . )$ ,)
. $)$ ! - ก BAbnormal program
termination” ก )1>
+# -- abort( );
9) ก abs(x)
#& ก -+ 2! x " x #& # ก,- !) , /
+# -- abs(x);
*+%', ก ก
atoi( ) char numstr[20] = “1000”; int d;d =atoi(numstr);
printf (“%d”, d); = >9 . ! 1000
- 19. ! ก "*+%', ก ก atoi( ), atof( ) $ atol( )
/* atoifl.c */
#include<stdio.h> /* 1 */
#include<stdlib.h> /* 2 */
#include<conio.h> /* 3 */
void main(void) /* 4 */
{ /* 5 */
char s1[5] = "123"; /* 6 */
char s2[10] = "14.573"; /* 7 */
char s3[15] = "1234567891"; /* 8 */
int as1; float fs2; long ls3; /* 9 */
clrscr( ); /* 10 */
as1 = 2 * atoi(s1); /* 11 */
fs2 = 2 * atof(s2); /* 12 */
ls3 = atol(s3); /* 13 */
printf("AS1 = %dn",as1); /* 14 */
printf("FS2 = %fn",fs2); /* 15 */
printf("LS3 = %ldn",ls3); /* 16 */
printf("nPress any key back to program ..."); /* 17 */
getch(); /* 18 */
} /* 19 */
- 21. #$ % & '! ก"
11 ก atoi( ) # % s1 #& !) ,
* .# 2. '( /)$ +2ก- ! 2 ก,-. # as1
12 ก atof( ) # % s2 #& ! < %
* .# 2. '( /)$ +2ก- ! 2 ก,-. # fs2
13 ก atol( ) # % s3 #& !) ,
% long integer ก,-. # ls3
14 45 16 ! # as1, fs2 $ as3 -
= . ก) 1 >
17 1 18 >% >! ก ; > ก - +"# ก $ 3
- ; ก enter )$ก - ! +"# ก
- 22. 6 #ก 0*
# $" ! ก "# ก
- * #& 3 ก,-. Library
- "# ก > ก- '/ ;ก > $: &
' ก 6 #ก
- 0 0*ก #ก (Debug) $ Maintain6 # #ก
- 0 0*ก **ก# 6 #ก # Top-Down Design
- 23. Top-Down Design
ก ก --"# ก ก : $ -- $ ก
ก $ - ก ) )
"# ก *-ก . 3" +
1. " + 2 )
2. " + 21 A
3. " + 2 ) 3 9%
- 24. ! ก "',%'
"# ก
# $ก <
PROCEDURE or 6,+ 7% ! ก "',%'
FUNCTION
BEGIN
statement (s);
end;
- 25. ! ก "',%'
-6 #ก 0* & : ,0& $ก = &# ก0* BEGIN *
6 #ก : ก
-6 #ก 0* ,0& :& ,0& $ก = # $ ,0& (4 , 6 #ก : ก
# ก 0
-,0& :& !" & PROCEDURE : )* FUNCTION
2 & END; # END.
-6 #ก 0* 4 * 0 * 73ก ก6 6 #ก : ก
- 26. ! 89 % 1 ก
"# ก 1A# 2 --
-"> '
- ก
! ก $ "> ' $ ก
ก ! 89 %
: )* ก &# : )* 92 -
2! , 7' ( ,) )(
-ก4 : (0 ': &# ก -
- (0
-9 9(0 9> * ก
- 27. ก $ 96 %
6" &ก
6 + :* & ,+" 9 7 15
6 + &* "; 6+6 &< 9 7 18
6 + 6=+ 9">% ก 9 7 19
6 + &*' #= 9#' 9 7 31
6 + ก " ? ? *, '7$ 9 7 37
6 + & +@ 6 = %', 9 7 38
6 + +&6 =9 AB%? 9 7 39
ก9 ' C " '"?5กA D 6/2