งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

บทที่ ไลบรารีฟังก์ชัน

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "บทที่ ไลบรารีฟังก์ชัน"— ใบสำเนางานนำเสนอ:

1 บทที่ 5 ไลบรารีฟังก์ชัน

2 ไลบรารีฟังก์ชัน-ฟังก์ชันมาตรฐาน
การใช้ไลบรารีฟังก์ชันนั้นทำได้โดยการเขียนชื่อของฟังก์ชันตามด้วยวงเล็บซึ่งภายในเป็นพารามิเตอร์ คือข้อมูลที่ฟังก์ชั่นต้องการใช้ ต้องมีการ include ไลบรารีไฟล์ของมันไว้ตรงส่วน preprocessing ด้วย /* A program to show the use of abs() function */ #include <stdio.h> #include <math.h> void main(void) { int x, y; x = -5; y = abs(x); printf("The absolute value of %d is %d ", x, y); } P. 5.1 The absolute value of -5 is 5

3 รูปแบบไวยากรณ์ (Syntax) ของฟังก์ชัน
ชนิดของค่าส่งกลับ ชื่อฟังก์ชัน (พารามิเตอร์ต่างๆ) ตัวอย่างเช่น int abs( int ) การใช้งาน เช่น y = abs (10 ) มีค่าเป็น 10 y = abs (-10) ก็มีค่าเป็น 10 y = abs(-5.3) มีค่าเป็น 5

4 วิธีการใช้งานฟังก์ชัน
1. ฟังก์ชันบางตัวไม่มีการคืนค่าออกมา การใช้งานทำได้โดยการเรียกฟังก์ชันเท่านั้น void printf(format_string, expression_list); void scanf(format_string, variable_list); การใช้งานทำได้โดยการเขียนชื่อฟังก์ชันตามด้วยวงเล็บของพารามิเตอร์ เช่น printf("The absolute value of %d is %d ", x, y); 2. ฟังก์ชันบางตัวมีการคืนค่าผลลัพธ์ออกมาด้วย ดังตัวอย่างเช่น int abs ( int )

5 P. 5.2 P. 5.3 การใช้งานฟังก์ชันที่มีการคืนค่าผลลัพธ์ออกมานั้น
ค่าที่ได้นี้สามารถนำไปกำหนดค่าให้อีกตัวแปรหนึ่งได้ #include <stdio.h> #include <math.h> void main(void) { int x, y; x = -5; y = abs(x); printf("The absolute value of %d is %d ", x, y); } P. 5.2 หรืออาจนำค่านั้นไปใช้โดยตรงโดยไม่ต้องส่งให้กับตัวแปรใดก็ได้ #include <stdio.h> #include <math.h> void main(void) { int x; x = -5; printf("The absolute value of %d is %d ", x, abs(x)); } P. 5.3 The absolute value of -5 is 5

6 Standard Input/Output Library Functions <stdio.h>
ฟังก์ชัน การใช้งาน void printf(format_string, expression_list) พิมพ์ข้อความตามรูปแบบใน format_string และค่าของตัวแปรใน expression_list ออกทางหน้าจอ void scanf(format_string, variable_list) รับข้อมูลจากคีย์บอร์ดตามรูปแบบใน format-string เก็บในตัวแปรใน variable-list int getchar(void); รับตัวอักษรจากคีย์บอร์ดหนึ่งตัวแล้วคืนค่าเป็นรหัสแอสกี char *gets(char *buf); รับข้อความจากคีย์บอร์ดให้กับตัวแปร variable int putchar(int c); แสดงตัวอักษรของรหัสแอสกี C บนหน้าจอ int puts(char *str); แสดงข้อความของ expression บนหน้าจอ

7 P. 5.4 Please enter a character : a 
/* A program to show the use of getchar library function */ #include <stdio.h> main() { int ch; printf(“Please enter a character : “); ch = getchar(); printf("Ascii code and value of your character are : %d %c\n", ch, ch); } Please enter a character : a  Ascii code and value of your character are : 97 a

8 P 5.5 /* A program to show the use of putchar() function */
#include <stdio.h> main() { int ch; printf(“Please enter an ascii code : “); scanf(“%d”, &ch); printf("The character of your ascii is : "); putchar(ch); } Please enter an ascii code: 97  The character of your ascii is : a

9 P 5.6 /* A program to show the use of gets library function */
#include <stdio.h> main() { char name[30]; printf(“Please enter your name : “); gets(name); printf("Good morning %s\n", name); } Please enter your name : Suthep Madarasmi  Good morning Suthep Madarasmi

10 P 5.7 #include <stdio.h> main() { char name[30];
printf("Please enter your name : "); gets(name); printf("Good morning "); puts(name); } Please enter your name : Suthep Madarasmi Good morning Suthep Madarasmi

11 Console Input/Output Functions <conio.h>
ฟังก์ชัน การใช้งาน clrscr( ) gotoxy( x, y) wherex( ) wherey( ) ล้างหน้าจอ ย้ายเคอร์เซอร์ไปยังคอลัมน์ x บรรทัด y คืนค่าคอลัมน์ปัจจุบัน คืนค่าบรรทัดปัจจุบัน

12 P 5.8 /* A program to show the use of clrscr library function */
#include <stdio.h> #include <conio.h> main() { char name[30]; printf("Please enter your name : "); gets(name); clrscr(); printf("Good morning "); puts(name); } P 5.8 Please enter your name : Suthep Madarasmi หลังจากป้อนข้อความและกด Enter โปรแกรมจะทำการล้างหน้าจอ ทำให้ข้อความทั้งหมดหายไป และจะทำการพิมพ์ข้อความใหม่บนบรรทัดแรกดังนี้ Good morning Suthep Madarasmi

13 P 5.9 /* A program to show the use of gotoxy function */
#include <stdio.h> #include <conio.h> main() { char name[30]; printf("Please enter your name : "); gets(name); gotoxy(20, 5); printf("Good morning "); puts(name); } P 5.9 Please enter your name : Suthep Madarasmi Good morning Suthep Madarasmi

14 P 5.10 #include <stdio.h> #include <conio.h > main() {
char name[30]; printf("Please enter your name : "); gets(name); printf("%d\n”, wherex()); printf("%d\n”, wherey()); } P 5.10 Please enter your name : Suthep Madarasmi 1 3

15 Math Library Functions <math.h>
ฟังก์ชัน การใช้งาน sqrt(int x) หาค่ารากที่สองของ x pow(double x, double y) หาค่า xy exp(x) หาค่าเอกซ์โปเนนเชียลของ x log(x) หาค่าล็อกธรรมชาติของ x log10(x) หาค่าล็อกฐานสิบของ x abs(x) หาค่าสัมประสิทธิ์ของ x (เมื่อ x เป็นเลขจำนวนเต็ม) fabs(x) หาค่าสัมประสิทธิ์ของ x (เมื่อ x เป็นเลขทศนิยม) ceil(x) คืนค่าเลขจำนวนเต็มของ x โดยปัดเศษขึ้น floor(x) คืนค่าเลขจำนวนเต็มของ x โดยปัดเศษลง fmod(x,y) หาค่าเศษของ x หาร y sin(x) หาค่า sin ของ x cos(x) หาค่า cos ของ x tan(x) หาค่า tan ของ x rand( ) สุ่มเลขจำนวนเต็มระหว่าง 0 ถึง RAND_MAX

16 P 5.11 /* A program to show the use of sqrt and pow functions */
#include <stdio.h> #include <math.h> main() { float a, b, c; float x1, x2; printf(“Please enter a, b, c : “); scanf(“%f %f %f”, &a, &b, &c); x1 = (-b + sqrt (pow( b, 2 ) - 4*a*c) ) / ( 2 * a ); x2 = (-b - sqrt (pow( b, 2 ) - 4*a*c) ) / ( 2 * a ); printf("The values of x are %0.2f and %0.2f\n“, x1, x2); } P 5.11 Please enter a, b, c :  The values of x are –0.22 and –2.28

17 P 5.12 Please enter a number : 1.0  Exponential of 1.0000 is : 2.7183
/* A program to show the use of exp, log, and log10 library functions */ #include <stdio.h> #include <math.h> main() { double x, ExpOfX, LnOfX, Log10OfX; printf("Please enter a number : "); scanf("%lf", &x); ExpOfX = exp(x); LnOfX = log(ExpOfX); Log10OfX = log10(ExpOfX); printf("Exponential of %0.4f is : %0.4f\n", x, ExpOfX); printf("Natural Log of %0.4f is : %0.4f\n", ExpOfX, LnOfX); printf("10 Base Log of %0.4f is : %0.4f\n", ExpOfX, Log10OfX); } P 5.12 Please enter a number : 1.0  Exponential of is : Natural Log of is : 10 Base Log of is :

18 /* A program to show the use of abs and fabs library functions */
#include <stdio.h> #include <math.h> main() { int i1, i2, di; float x1, x2, dx; printf("Please enter two integers for i1 and i2 : "); scanf("%d %d", &i1, &i2); printf("Please enter two float numbers x1 and x2 : "); scanf("%f %f", &x1, &x2); di = abs(i2-i1); dx = fabs(x2-x1); printf("The difference of i1 and i2 is %d\n", di); printf("The difference of x1 and x2 is %0.4f\n", dx); } P 5.13 Please enter two integers foe i1 and i2 :  Please enter two float numbers for x1 and x2 :  The difference of i1 and i2 is 85 The difference of x1 and x2 is

19 P 5.14 /* A program to show the use of ceil and floor functions */
#include <stdio.h> #include <math.h> main() { float x; printf("Please enter a float number : "); scanf("%f", &x); printf("Ceiled %0.4f is %0.4f\n", x, ceil(x)); printf("Floored %0.4f is %0.4f\n", x, floor(x)); } Please enter a float numbers :  Ceiled is Floored is Please enter a float numbers :  Ceiled is Floored is

20 P 5.15 /* A program to show the use of fmod library function */
#include <stdio.h> #include <math.h> main() { float x, y; printf("Please enter two float numbers : "); scanf("%f %f", &x, &y); printf("%0.4f mod %0.4f is %0.4f\n", x, y, fmod(x,y)); } Please enter two float numbers :  fmod is

21 P 5.16 #include <stdio.h> #include <math.h>
#define pi main() { float degree, radians; printf("Please enter a degree : "); scanf("%f", &degree); radians = degree*pi/180; printf("sin(%0.4f) is %0.4f\n", degree, sin(radians)); printf("cos(%0.4f) is %0.4f\n", degree, cos(radians)); printf("tan(%0.4f) is %0.4f\n", degree, tan(radians)); } Please enter a degree : 30  sin(30.000) is cos(30.000) is tan(30.000) is

22 Standard Library Definition <stdlib.h>
สุ่มตัวเลข int rand(void); rand()%100 +1 สุ่มตัวเลข 1 ถึง 100 int random(int MAX); สุ่มตัวเลข 1 ถึง 100 random(100)+1 สุ่มตามเวลา void srand(unsigned seed);

23 P 5.18 #include <stdio.h> #include <stdlib.h>
#include <time.h> main() { int i; time_t t; srand(time(NULL)); i = rand() % 2500; printf("The random number is : %d\n", i); } The random number is 1060 The random number is 1087 The random number is 1096

24 String Library <string.h>
ฟังก์ชั่น strcpy ใช้ในการกำหนดค่าให้กับสตริง เนื่องจากไม่สามารถจะใช้เครื่องหมาย = โดยตรงได้ #include <stdio.h> #include <string.h> void main(void) { char name[20]; strcpy( name, "Sam"); printf("Your name is %s.\n", name); } Your name is Sam.

25 String Library <string.h>
ฟังก์ชั่น strlen ใช้ในการหาความยาวของสตริง #include <stdio.h> #include <string.h> void main(void) { char name[20]; strcpy( name, "Sam"); printf("Your name is %s.\n", name); printf("Your name is %d characters long.\n", strlen(name)); } Your name is Sam. Your name is 3 characters long.

26 String Library <string.h>
ใช้ในการเปรียบเทียบสตริง เนื่องจากเรา ไม่สามารถเปรียบเทียบทั้งสตริงด้วย เครื่องหมายเปรียบเทียบธรรมดาได้ ฟังก์ชั่น strcmp strcmp(ตัวแปร/”ข้อความ”1, ตัวแปร/”ข้อความ”2) ถ้าเท่ากัน จะได้ค่า == 0 ถ้าไม่เท่ากัน จะได้ค่า !=0

27 ฟังก์ชั่น strcmp Your name is Sam.
#include <stdio.h> #include <string.h> void main(void) { char Name[20]; int A, B; strcpy( Name, "Sam"); printf("Your name is %s.\n", Name); A = strcmp(Name,”Sombut”); printf(“The difference of %s and %s is %d.\n", Name, “Sombut”, A); B = strcmp(name,”Sam”); printf(“The difference of %s and %s is %d.\n", Name, “Sam”, B); } Your name is Sam. The difference of Sam and Sombut is -14. The difference of Sam and Sam is 0.


ดาวน์โหลด ppt บทที่ ไลบรารีฟังก์ชัน

งานนำเสนอที่คล้ายกัน


Ads by Google