ฟังก์ชัน ในภาษาซี มี 2 ชนิด
คือ ฟังก์ชันมาตรฐาน(standard function)
กับ ฟังก์ชันที่ผู้เขียนโปรแกรมเขียนขึ้นเอง (user
defined function)
ฟังก์ชันมาตรฐาน
เป็นฟังก์ชันที่ผู้ผลิตคอมไพล์เลอร์เขียนขึ้นเพื่อผู้ใช้นำไปใช้ในการเขียนโปรแกรมเพื่อให้เขียนโปรแกรมได้สะดวกและง่ายขึ้น
บางครั้งอาจเรียกว่า library functions
ปกติฟังก์ชันเหล่านี้จะจัดเก็บไว้ใน header files ดังนั้นผู้ใช้จะต้องรู้ว่าฟังก์ชันนั้นอยู่ใน header file ใด จึงจะนำไปเรียกใช้ในส่วนต้นของโปรแกรม ด้วย #include <header
file.h> ได้ เช่น #include <stdio.h> ฟังก์ชันมีมากมาย อาจจำแนกดังนี้
ฟังก์ชันทางคณิตศาสตร์
เป็นฟังก์ชันที่ใช้ทางการคำนวณ ทางคณิตศาสตร์ ปกติอยู่ใน math.h ผลลัพธ์ ที่ได้จากฟังก์ชันกลุ่มนี้เป็นข้อมูลประเภท double ดังนั้นตัวแปรที่ใช้จึงเป็นพวกที่มีชนิดเป็น double
ฟังก์ชัน sin(x) เป็นฟังก์ชันใช้คำนวณหาค่าของ sine
โดย x มีค่าของมุมในหน่วย เรเดียน
ฟังก์ชัน cos(x) ใช้หาค่า cosine
โดย x มีหน่วยเป็นเรเดียน(radian)
ฟังก์ชัน tan(x) ใช้หาค่า tangent
โดย x มีหน่วยเป็นเรเดียน(radian)
ตัวอย่าง /* math1.c */
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{ float deg , angle, pi = 3.141592654;
clrscr();
printf("Please
enter value of angle in degree that you want to find tan cos sin :");
scanf("%f",°);
angle = deg * pi / 180; /* เปลี่ยนค่า
องศา ให้เป็นเรเดียน */
printf("\nvalue of
tangent %4.0f degree is %4.2f
",deg,tan(angle));
printf("\nvalue of
sine %4.0f degree is %4.2f
",deg,sin(angle));
printf("\nvalue of
cosine %4.0f degree
is %4.2f ",deg,cos(angle));
}
ฟังก์ชัน sqrt(x) ใช้หาค่ารากที่สองของ x โดย x เป็นตัวเลขหรือตัวแปรที่ไม่ติดลบ
ฟังก์ชัน exp(x) ใช้หาค่า ex โดย e มีค่าประมาณ
2.718282
ฟังก์ชัน pow(x,y) ใช้หาค่า x y
ฟังก์ชัน log(x) ใช้หาค่า log ฐาน e เรียกว่า natural logarithm โดย x เป็นตัวเลขหรือตัวแปรที่ไม่ติดลบ
ฟังก์ชัน log10(x) ใช้หาค่า log ฐาน 10 โดย x เป็นตัวเลขหรือตัวแปรที่ไม่ติดลบ
ฟังก์ชัน ceil(x) ใช้ในการปัดเศษทศนิยมของ x
เมื่อ x เป็นเลขทศนิยม
ฟังก์ชัน floor(x) ใช้ในการตัดเศษทศนิยมของ x
ทิ้งเมื่อ x เป็นเลขทศนิยม
ฟังก์ชัน fabs(x) ใช้ในการหาค่าสัมบูรณ์ของค่าคงที่หรือตัวแปรที่มีทศนิยม
โดยเป็นบวกหรือลบก็ได้
ตัวอย่าง ให้นักเรียนศึกษาการทำงานของโปรแกรม /* math2.c */ แล้วป้อนโปรแกรมและตรวจสอบการทำงานว่าตรงกับที่คาดคะเนหรือไม่
/* math2.c */
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
double x = 10.0 , y = 2.0 ,z = 16.0,a
= 2.718282 , b = -2.718282 , m=1.0;
clrscr();
printf("\npow\(x,y\) = %4.2f when x=10.0 y=2.0", pow(x,y));
printf("\nsqrt\(z\)
= %4.2f when z=16.0", sqrt(z));
printf("\nexp\(m\)
= %4.6f when m=1.0",exp(m));
printf("\nlog\(a\)
= %4.2f when a=2.718282",log(a));
printf("\nlog10\(x\) = %4.2f when x=10.0",log10(x));
printf("\nceil\(a\)
= %4.2f when a=2.718282",ceil(a));
printf("\nceil\(b\)
= %4.2f when b=-2.718282",ceil(b));
printf("\nfloor\(a\) = %4.2f when a=2.718282",floor(a));
printf("\nfloor\(b\) = %4.2f when b=-2.718282",floor(b));
printf("\nfabs\(a\)
= %4.6f when a=2.718282" ,fabs(a));
printf("\nfabs\(b\)
= %4.6f when b=-2.718282" ,fabs(b));
}
ฟังก์ชันที่จัดการเกี่ยวกับตัวอักษร(character functions)
เป็นฟังก์ชันที่จัดการกับตัวอักษร single char เท่านั้น ตัวอักษรนี้ใช้หน่วยความจำเพียง 1 ไบต์ ฟังก์ชันเหล่านี้อยู่ใน header file ชื่อ ctype.h ก่อนจะทำการเขียนโปรแกรมจึงต้อง #include
<ctype.h> เข้ามาในส่วนต้นของโปรแกรม
ฟังก์ชัน isalnum(cha) เป็นฟังก์ชันที่ใช้ตรวจสอบว่าข้อมูลในตัวแปร(ซึ่งคือตัวแปรประเภท
char ) เป็นตัวอักขระหรือตัวเลขหรือไม่
ถ้าเป็นตัวอักษรหรือตัวเลข ฟังก์ชันจะส่งค่าที่ไม่ใช่ 0 มาให้ ถ้าข้อมูลในตัวแปร
เป็นอักขระพิเศษอื่นที่ไม่ตัวอักษรหรือตัวเลขจะส่งค่าออกมาเป็น 0
ฟังก์ชัน isalpha(cha) เป็นฟังก์ชันที่ใช้ตรวจสอบว่าข้อมูลในตัวแปร(ซึ่งคือตัวแปรประเภท
char ) เป็นตัวอักขระหรือไม่
ถ้าเป็นตัวอักษรฟังก์ชันจะให้ค่าที่ไม่ใช่ 0
ออกมาถ้าเป็นตัวเลขหรืออักขระพิเศษอื่นฟังก์ชันจะส่งค่า 0 ออกมา
ฟังก์ชัน
isdigit(cha) เป็นฟังก์ชันที่ใช้ตรวจสอบว่าข้อมูลในตัวแปร(ซึ่งคือตัวแปรประเภท
char ) ฟังก์ชัน
เป็นตัวเลขหรือไม่ ถ้าเป็นตัวเลขฟังก์ชันจะให้ค่าที่ไม่ใช่ 0 ออกมา ถ้าเป็นตัวอักษรหรืออักขระพิเศษอื่น
ฟังก์ชันจะส่ง 0 ออกมา
ตัวอย่าง /* isalnumPhadigit.c
*/
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
main()
{
clrscr();
char cha1
= 'B' ,cha2 ='3',cha3= '&';
printf("\n %d is
return value of isdigit\(cha1\) of %c",isdigit(cha1),cha1);
printf("\n %d is
return value of isdigit\(cha2\) of %c ",isdigit(cha2),cha2);
printf("\n %d is
return value of isalpha\(cha3\) of %c ",isalpha(cha3),cha3);
printf("\n %d is
return value of isalpha\(A\) of %c ",isalpha('A'),'A');
printf("\n %d is
return value of isalpha\('0'\) of %c ",isalpha('0'),'0');
printf("\n %d is
return value of isalpha\('$'\) of %c ",isalpha('$'),'$');
printf("\n %d is
return value of isalnum\(cha1\) of %c",isalnum(cha1),cha1);
printf("\n %d is
return value of isalnum\(cha2\) of %c ",isalnum(cha2),cha2);
printf("\n %d is
return value of isalnum\(cha3\) of %c ",isalnum(cha3),cha3);
printf("\n %d is
return value of isalnum\(A\) of %c ",isalnum('A'),'A');
printf("\n %d is
return value of isalnum\('0'\) of %c ",isalnum('0'),'0');
printf("\n %d is
return value of isalnum\('$'\) of %c ",isalnum('$'),'$');
}
ฟังก์ชัน islower(cha) ฟังก์ชันที่ใช้ตรวจสอบว่าตัวอักขระในตัวแปร
cha เป็นตัวพิมพ์เล็กหรือไม่
ถ้าเป็นตัวพิมพ์เล็กฟังก์ชันจะส่งค่ากลับเป็นจำนวนเต็มที่ไม่ใช่ 0
แต่ถ้าไม่ใช่ตัวพิมพ์เล็กจะส่งค่ากลับเป็น 0
ฟังก์ชัน isupper(cha) ฟังก์ชันที่ใช้ตรวจสอบว่าตัวอักขระในตัวแปร
cha เป็นตัวพิมพ์ใหญ่หรือไม่ ถ้าเป็นตัวพิมพ์ใหญ่
ฟังก์ชันจะส่งค่ากลับเป็นจำนวนเต็มที่ไม่ใช่ 0
แต่ถ้าไม่ใช่ตัวพิมพ์ใหญ่จะส่งค่ากลับเป็น 0
ฟังก์ชัน tolower(cha) ฟังก์ชันที่ใช้เปลี่ยนตัวพิมพ์ใหญ่ที่เก็บอยู่ในตัวแปรให้เป็นตัวพิมพ์เล็ก
ฟังก์ชัน toupper(cha) ฟังก์ชันที่ใช้เปลี่ยนตัวพิมพ์เล็กที่เก็บอยู่ในตัวแปรให้เป็นตัวพิมพ์ใหญ่
ตัวอย่าง /* upperlower.c */
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
main()
{
char cha1 =
'D' , cha2 = 'a' ,cha3 = 'f' , cha4 = 'N' ;
clrscr();
printf("\ncheck cha1 = 'D' is uppercase yes or no : %d ",isupper(cha1));
printf("\ncheck cha2 = 'a' is lower yes or no : %d ",islower(cha2));
printf("\ncheck cha2 = 'a' is upper yes or
no : %d ",isupper(cha2));
printf("\ncheck 'i' is lower
yes or no : %d ",islower('i'));
printf("\ncheck 'L' is
uppercase yes or no : %d ",isupper('L'));
printf("\nchange
cha3 = 'f' to
uppercase %c : ", toupper(cha3));
printf("\nchange
cha4 = 'N' to
lowercase %c : ", tolower(cha4));
}
ฟังก์ชัน
isspace(cha) ฟังก์ชันที่ใช้ตรวจสอบว่าข้อมูลในตัวแปร
cha เป็น whitespace หรือไม่ whitespace
ได้แก่ space ,tab ,vertical tab ,formfeed ,carriage retun
,newline ถ้ามี whitespace
จริง ฟังก์ชันจะส่งค่าไม่เท่ากับ 0 ถ้าไม่จริงจะส่งค่า 0
ฟังก์ชัน isxdigit(cha) ฟังก์ชันที่ใช้ตรวจสอบว่าข้อมูลในตัวแปร
cha เป็น เลขฐานสิบหก ( คือ 0-9 , A-F , a – f) หรือไม่ ถ้าจริงส่งค่าตัวเลขที่ไม่ใช่ 0 ถ้าไม่จริงส่งตัวเลข 0
ตัวอย่าง /*isspaceisxdigit.c */
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
main()
{
char cha1
='\r',cha2= '\n',cha3='\v'
,cha4 ='A';
clrscr();
printf("\n%d is
volue return from isspace %c ",isspace(cha1),cha1);
printf("\n%d is
volue return from isspace %c ",isspace(cha2),cha2);
printf("\n%d is
volue return from isspace %c ",isspace(cha3),cha3);
printf("\n%d is
volue return from isxdigit %c
",isxdigit(cha4),cha4);
printf("\n%d is
volue return from isxdigit %c
",isxdigit('0'),'0');
printf("\n%d is
volue return from isxdigit %c
",isxdigit('g'),'g');
}
ฟังก์ชัน gotoxy(x,y); เป็นฟังก์ชันอยู่ใน conio.h
ใช้สั่งให้เคอร์เซอร์เคลื่อนที่ไปตามตำแหน่งที่ระบุ โดย x คือ ตำแหน่งของสดมภ์บนจอภาพ (คล้ายค่า x ของกราฟ)ค่าเพิ่มจากซ้ายไปขวามีค่าตั้งแต่
1 ถึง 79 ตำแหน่งที่ 80 สงวนไว้ไม่ให้ใช้
ส่วน y คือตำแหน่งแถวบนจอภาพนับจากบนลงล่าง
มีค่าได้ตั้งแต่ 1 ถึง 24 ตำแหน่งที่25 สงวนไว้
ฟังก์ชัน clreol(); เป็นฟังก์ชันอยู่ใน conio.h
ใช้ลบข้อความตั้งแต่ตำแหน่งที่เคอร์เซอร์อยู่ไปจนจบบรรทัด
ฟังก์ชัน delline(); เป็นฟังก์ชันอยู่ในconio.h
ใช้ลบข้อความทั้งบรรทัดที่เคอร์เซอร์อยู่ไปจนจบบรรทัดและเลื่อนข้อความในบรรทัดล่างขึ้นมาแทน
ฟังก์ชัน insline(); เป็นฟังก์ชันอยู่ในconio.h
ใช้แทรกบรรทัดว่าง 1 บรรทัดใต้บรรทัดที่เคอร์เซอร์อยู่
ฟังก์ชัน system(“dos
command”); เป็นฟังก์ชันอยู่ในstdlib.h
ใช้เรียกคำสั่งของ dos ขึ้นมาทำงาน เช่นคำสั่ง
cls dir date time
ฟังก์ชัน
abort(); ฟังก์ชันที่อยู่ใน
<stdlib.h> ใช้
ยกเลิกการทำงานของโปรแกรมทันทีไม่ว่าจะทำงานสำเร็จหรือไม่ และมีข้อความ Abnomal
program termination แสดงทางจอภาพ
ฟังก์ชัน abs(x); ฟังก์ชันที่อยู่ใน <stdlib.h>
ใช้หาค่าสัมบูรณ์ของ x โดย x ต้องเป็นจำนวนเต็ม
ฟังก์ชัน labs(x); ฟังก์ชันที่อยู่ใน <stdlib.h>
ใช้หาค่าสัมบูรณ์ของ x โดย x ต้องเป็นlong integer
ฟังก์ชัน atoi(s); ฟังก์ชันที่อยู่ใน <stdlib.h>
ใช้เปลี่ยนข้อความให้เป็นเลขจำนวนเต็ม
ฟังก์ชัน atol(s); ฟังก์ชันที่อยู่ใน <stdlib.h>
ใช้เปลี่ยนข้อความให้เป็น long integer
ฟังก์ชัน atof(s); ฟังก์ชันที่อยู่ใน <stdlib.h>
ใช้เปลี่ยนข้อความให้เป็น floating point
ตัวอย่าง /* atoilf.c */
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{ char numstring1[10],numstring2[10],numstring3[10];
int in1; float flo1; long lon1; clrscr();
printf("\nEnter
number as string1 : ");
scanf("%s",numstring1);
printf("\nEnter
number as string2 : ");
scanf("%s",numstring2);
printf("\nEnter
number as string3 : ");
scanf("%s",numstring3);
in1 = atoi(numstring1); flo1 =
atof(numstring2); lon1=atol(numstring3);
printf("\nnumstring1 =%s change to integer %d ",numstring1,in1);
printf("\nnumstring2 =%s change to floating point %4.4f
",numstring2,flo1);
printf("\nnumstring3 =%s change to long integer %d ",numstring3,lon1);
printf("\nsummation
of in1,flo1,lon1
is %6.4f ",in1+flo1+lon1);
printf("\nsummation
of atoi(numstring1),atof(numstring2),atol(numstring2) is %6.4lf:",atoi(numstring1)+atof(numstring2)+atol(numstring3));
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น