1 / 32

第 十 章

第 十 章. 內建函數. 大綱. 數學函數 (math.h) 亂數函數 (stdlib.h) 字元函數 (ctype.h) 時間函數 (time.h). 數學函數 (math.h). 絕對值 / 取整數 三角函數 對數函數 次方及方根 轉換函數. 數學函數 (math.h). 絕對值 — 數學之用法為: |x| int abs(int x) ; double fabs(double x) ; long double fabsl(long double x) ; 取整數

seamus
Download Presentation

第 十 章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第十章 內建函數

  2. 大綱 • 數學函數(math.h) • 亂數函數(stdlib.h) • 字元函數(ctype.h) • 時間函數(time.h)

  3. 數學函數(math.h) • 絕對值/取整數 • 三角函數 • 對數函數 • 次方及方根 • 轉換函數

  4. 數學函數(math.h) • 絕對值—數學之用法為:|x| • int abs(int x) ; • double fabs(double x) ; • long double fabsl(long double x) ; • 取整數 • 將倍精度x取比該數為大之最小整數,如123.45取124,-123.45取-123。 • double ceil(double x) ; • long double ceill(long double x) ; • 將倍精度x取比該數為小之最大整數,如123.45取123,-123.45取-124。 • double floor(double x) ; • long double floorl(long double x) ;

  5. 數學函數(math.h) • 整數與小數分離 • 將參數x分為整數部份與小數部份,整數部份以ipart傳回,小數部份以函數傳回 • double modf(double x, double *ipart); • long double modfl(long double x, long double *ipart); • 求x/y之小數餘數,以函數名稱傳回,如5.0/2.2之餘數為0.6 • double fmod(double x, double y); • long double fmodl(long double x, long double y);

  6. #include<iostream> • #include <math.h> • using namespace std; • int main( ) • { • int value = -1234; • double x = 5.1, y = 2.2; • double fraction, integer, number=1034.56; • cout<<"數值 "<< value<<" 之絕對值為 " << abs(value) << endl; • cout<< x << "除以" << y <<"之餘數=" << fmod(x,y) << endl; • fraction = modf(number, &integer) ; • cout<< number <<"整數部份為:" <<integer • << ",小數部份為:"<<fraction<< endl; • return 0; • }

  7. 三角函數 • 180度等於3.14159弳,即等於π,故一度等於π/180弳=3.14159/180弳= 0.01745327777778弳 • 正弦函數 • double sin(double x); • long double sinl(long double x); • 餘弦函數 • double cos(double x); • long double cosl(long double x); • 正切函數 • double tan(double x); • long double tanl(long double x);

  8. 反三角函數 • 反正弦函數 • double asin(double x); • long double asinl(long double x); • 反餘弦函數 • double acos(double x); • long double acosl(long double x); • 反正切函數 • double atan(double x); • long double atanl(long double x);

  9. 三角函數 • #include<iostream> • #include<math.h> • #define PI 3.14159 • using namespace std; • int main() • { • double x, degree=60.0; • cout << degree <<"度=" << degree*PI/180 << "弳"<< endl; • cout << degree <<"度之sin="<< sin(degree*PI/180) << endl; • x=sin(degree*PI/180); • cout<<"The arcsin of "<< x <<" is "<< asin(x) << "弳"<<endl; • return 0; • }

  10. 對數函數 • 傳回自然對數e之x值,即ln(x) • double log(double x); • long double logl(long double x); • 傳回以10為底之對數x值,即log10(x)之值 • double log10(double x); • long double log10l(long double x); • 傳回自然對數e之x次方值,即y=ex • double exp(double x); • long double expl(long double x);

  11. #include<iostream> • #include<math.h> • using namespace std; • int main() • { • double x=8.687; • cout <<"ln(" <<x <<")=" << log(x) << endl; • cout <<"log(" << x << ")=" << log10(x) << endl; • cout <<"exp(" << log(x) << ")=" << exp(log(x)) << endl; • return 0; • }

  12. 次方及方根 • 傳回 之值,相當於求直角三角形之斜邊長 • double hypot(double x, double y); • long double hypotl(long double x, long double y); • 傳回x之y次方值,兩數均可帶小數,即z=xy • double pow(double x, double y); • long double powl(long double x, long double y); • 傳回10之p次方值,p為整數,即z=10p • double pow10(int p); • long double pow10l(int p);

  13. 求x之平方根值 • x之平方根值,x需為正,使用上常冠上abs,如sqrt(abs(x)),即 • double sqrt(double x); • long double sqrtl(long double x);

  14. #include<iostream> • #include<math.h> • using namespace std; • int main() • { • double x=8.687; • cout <<"ln(" <<x <<")=" << log(x) << endl; • cout <<"log(" << x << ")=" << log10(x) << endl; • cout <<"exp(" << log(x) << ")=" << exp(log(x)) << endl; • return 0; • }

  15. 10-2亂數函數

  16. 亂數函數 • int rand(void); • 產生亂數值介於0到RAND_MAX之間,RAND_MAX之常數值為32767 • void randomize(void); • 以任一數值當亂數啟動之種子(seed),可使每次產生之亂數值都不同,最好再加入另一包含檔<time.h>,以時間當種子會更有亂數的結果

  17. 亂數函數 • int random(int num); • 產生介於0與num-1間之亂數,若num=100則產生介於0-99間之亂數,若產生亂數之前沒有先使用randomize()則每次產生之亂數都一樣 • void srand(unsigned seed); • 重新以seed當亂數種子來起動亂數,每次以該種子啟動之亂數值都是一樣的,若配合time_t的話,其功能就如使用randomize()相同,time_t為一長整數值,用於儲存傳回時間之秒數

  18. 10-3字元函數(ctype.h) • ASCII字元集可分類為數字字元、控制字元、英文字母大小寫字元、特殊符號字元、16進位字元、空白字元等。 • 測試函數 • 測試函數之目的是用來檢測該字元是屬於那一類之字元。 • 轉換函數 • 將內碼轉為字元或大小寫之互轉而已。

  19. 字元測試函數 • int isalnum(int c); • 測試c是否為alphabet(英文字母)或number(數字字母),若兩者之一就傳回1,否則傳回0 , 如: • cout << isalnum(‘A’);結果為1 • cout << isalnum(‘!’);結果為0,因!是特殊字元 • int isalpha(int c); • 測試c是否為alphabet(英文字母),若是就傳回1,否則傳回0 , 如: • cout << isalpha(‘H’);結果為1 • cout << isalpha(‘9’);結果為0,因9不是字母

  20. 字元測試函數 • int isascii(int c); • 測試c是否為ASCII標準字元,若是就傳回1,否則傳回0,在目前IBM PC之ASCII內以此函數來測試只有內碼在0-127才是屬於ASCII標準字元,128-255之字元為擴充ASCII字元 • cout << isascii(‘!’);結果為1 • cout << isascii(129);結果為0,因字元內碼129超過127非標準 ASCII • int iscntrl(int c); • 測試c是否為控制(contril)字元,若是就傳回1,否則傳回0,所謂控制字元其內碼在0-31及127等共33個,像Enter、tab、space等都是屬於此字元之範圍 • cout << iscntrl(‘\n’);結果為1 • cout << iscntrl(‘a’);結果為0

  21. 字元測試函數 • int isdigit(int c); • 測試c是否為阿拉伯數字字元’0’-‘9’,若是就傳回1,否則傳回0 • cout << isdigit(‘0’) ;結果為1 • cout << isdigit(‘A’) ;結果為0 • int ispunct(int c); • 測試c是否為特殊符號字元(punctuation character),若是就傳回1,否則傳回0,但不包括文數字與空白字元 • cout << ispunc(‘!’) ;結果為1 • cout << ispunct(‘A’) ;結果為0

  22. 字元測試函數 • int isgraph(int c); • 測試c是否為可顯示字元,若是就傳回1,否則傳回0,一共有94個,包括英文字母52、數字10及特殊符號32個 。 • int isprint(int c); • 測試c是否為可列印字元,若是就傳回1,否則傳回0,一共有95個,包括空白字元1、英文字母52、數字10及特殊符號32個。 • int isspace(int c); • 測試c是否歸納為空白字元,若是就傳回1,否則傳回0,一共有6個,內碼分別為9、10、11、12、13、32 。

  23. 字元測試函數 • int islower(int c); • 測試c是否英文字母小寫字元(lowercase character),若是就傳回1,否則傳回0,一共有26個 • int isupper(int c); • 測試c是否英文字母大寫字元(uppercase character),若是就傳回1,否則傳回0,一共有26個 • int isxdigit(int c); • 測試c是否16進位字元(hexadecimal character),若是就傳回1,否則傳回0,一共有22個,即0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,A,B,C,D,E,F

  24. 轉換函數 • 將內碼轉為字元或大小寫互轉 • int toascii(int c); • int tolower(int ch); • int toupper(int ch); • int _tolower(int ch); • int _toupper(int ch);

  25. 整數轉為ASCII字元 • int toascii(int c); • 將整數c之最低7位元轉為相對應之ASCII字元,其內碼範圍在0到127之間,若要轉為字元需作型態轉換(Type casting),即在函數前加上char #include <iostream> //cout #include <ctype.h> using namespace std; int main( ){ int c; cout << "輸入任一數字:";cin >> c; cout << "Hex="<<hex<<c<<dec<<endl; cout << "ASCII:"<<toascii(c)<<"="<<char(toascii(c))<<endl; return 0; } 輸入任一數字:166 Hex=a6 ASCII:38=&

  26. 大小寫互轉 • int tolower(int ch); • 將字母ch之字元轉為小寫字母,若非小寫字母則保留原狀 • int toupper(int ch); • 將字母ch之字元轉為大寫字母,若非大寫字母則保留原狀 • int _tolower(int ch); • 將字母ch之字元轉為小寫字母,但必需確認ch是在‘a’-’z’範圍內 • int _toupper(int ch); • 將字母ch之字元轉為大寫字母,但必需確認ch是在‘A’-’Z’範圍內

  27. 10-4時間函數(time.h) • 表時間內各部分如年、月、日、時、分、秒之內建時間結構tm,表時間總經過計秒單位之長整數time_t,表電腦之滴答(tick)之clock,又有當地時間、GMT時間…等 • 包含檔 #include <time.h>

  28. 時間結構(tm) • struct tm { • int tm_sec; /* Seconds */ • int tm_min; /* Minutes */ • int tm_hour; /* Hour (0 - 23) */ • int tm_mday; /* Day of month (1 - 31) */ • int tm_mon; /* Month (0 - 11) 0:元月*/ • int tm_year; /* Year (減去1900後之值) */ • int tm_wday; /* Weekday (0 - 6; Sunday is 0) */ • int tm_yday; /* Day of year (0 -365) */ • int tm_isdst; /* Nonzero if daylight saving time is in effect. */ • };

  29. 時間函數(time.h) • ASCII時間 • char *asctime(const struct tm *tblock); • 將時間結構內資料轉為26個字元長之字串 • 26字元,包括換列「\n」及字串結束符號「\0」如 : • Sun Sep 16 01:03:52 1973\n\0 • 將時間轉為日曆式 • time_t mktime(struct tm *t); • 將時間轉為日曆式,有效範為為自Jan 1 1970 00:00:00 至Jan 19 2038 03:14:07,若轉換不成功則傳回-1

  30. 輸入年月日並求星期幾? • char *wday[ ] = {"星期日", "星期一", "星期二", "星期三","星期四", "星期五", "星期六", "不知道"}; • void main(void){ • struct tm time_check; • int year, month, day; • time_t t; • cout<<"輸入西元年,月,日:"; • cin>>year>>month>>day; • time_check.tm_year = year - 1900; //填入結構 • time_check.tm_mon = month - 1; • time_check.tm_mday = day; • time_check.tm_hour = 0;

  31. 輸入年月日並求星期幾? • time_check.tm_min = 0; • time_check.tm_sec = 1; • time_check.tm_isdst = -1; • t = mktime(&time_check); //設定tm結構 • if (t == -1) time_check.tm_wday = 7; • else cout <<"設定時間為:"<< ctime(&t); • cout<<"該日為:"<<wday[time_check.tm_wday]; • //輸出星期 輸入西元年,月,日:2000 6 18設定時間為:Sun Jun 00:00:01 2000該日為:星期日

  32. 長整數時間(time_t) • 型態time_t在01/19/2038之3:14:07就不能正常運作, 需特別注意,它是一長整數long int, 用於儲存時間之總秒數 • time_t time(time_t *timer); • 計算自GMT 1970年一月一日00:00:00起歷經之秒數

More Related