Presentation is loading. Please wait.

Presentation is loading. Please wait.

函式庫補充資料.

Similar presentations


Presentation on theme: "函式庫補充資料."— Presentation transcript:

1 函式庫補充資料

2 6-4 函式庫 C語言編譯軟體內部提供了很多常用的函式,它們集中在所謂的程式庫或函式庫
6-4 函式庫 C語言編譯軟體內部提供了很多常用的函式,它們集中在所謂的程式庫或函式庫 使用函式庫中的函式只需將此檔利用#include指令包含進來就可以了

3 6-4.1 數學函式 常用的數學函式(參考課本6-31頁) int abs(int n); double fabs(double x);
6-4.1 數學函式 常用的數學函式(參考課本6-31頁) 函 數 原 型 作    用 表頭檔 int abs(int n); 計算整數n的絕對值(取正值)。 <stdlib.h>或<math.h> double fabs(double x); 計算雙浮點數x的絕對值(取正值)。 <math.h> long labs(long n); 計算長整數n的絕對值(取正值)。 double sqrt(double x); 計算雙浮點數x的平方根值。 double pow(double x, double y); 計算x的y次方值,即xy。

4 6-4.1 數學函式 常用的數學函式 type __max( type a, type b );
6-4.1 數學函式 常用的數學函式 type __max( type a, type b ); 傳回a和b中數值較大者,可以是任何型態的數值,特別注意的是max前面的底線有兩個。 <stdlib.h> type __min( type a, type b ); 和__max相反,__min傳回a和b中數值較小者。 double floor( double x ); 傳回不大於x的最大整數,如floor(2.8)會傳回2.0;floor(-2.8)傳回-3。 <math.h> double ceil( double x ); 傳回不小於x的最小整數,如ceil(2.8)會傳回3.0;ceil(-2.8)傳回-2。

5 6-4.2 亂數函式 int rand( void ); 所謂亂數(Random number),是由電腦自動產生一個數字
6-4.2 亂數函式 所謂亂數(Random number),是由電腦自動產生一個數字 電腦的亂數其實是由一個亂數產生器產生的,函式名稱是rand,使用前記得將其表頭檔<stdlib.h>包含進來 格式: int rand( void ); 呼叫後會傳回0~32767之間的任意一數

6 6-4.2 亂數函式 例如: 01 //Program name:06-04-02A.c
6-4.2 亂數函式 例如: 01 //Program name: A.c 02 #include <stdio.h> 03 #include <stdlib.h> 04 int main() 05 { int i; for (i=0;i<5;i++) printf("%d\n",rand()); system("PAUSE"); return 0; 11 } 執行結果: 41 18467 6334 26500 19169

7 void srand(unsigned int seed);
6-4.2 亂數函式 每次執行程式,產生亂碼的順序卻都相同,這是因為亂數產生器的「種子」(seed)並沒有改變,所產生的亂數都是屬於同一組 改變種子,就會產生另一組亂數 語法: void srand(unsigned int seed);

8 6-4.2 亂數函式 例如: 01 //Program name:06-04-02B.c
6-4.2 亂數函式 例如: 01 //Program name: B.c 02 #include <stdio.h> 03 #include <stdlib.h> 04 int main() 05 { int i; srand(100); for (i=0;i<5;i++) printf("%d\n",rand()); system("PAUSE"); return 0; 12 } 執行結果: 365 1216 5415 16704 24504

9 6-4.2 亂數函式 srand((unsigned) time ( NULL ) );
6-4.2 亂數函式 改了亂數產生器的種子數,以後每次執行時,還是產生同樣的一組亂數 利用讀取系統時間的函式,產生一個種子數,讓亂數產生器每次執行都不一樣 時間函數表頭檔:time.h 使用方法如下: srand((unsigned) time ( NULL ) );

10 6-4.2 亂數函式 例如: 01 //Program name:06-04-02C.c
6-4.2 亂數函式 例如: 01 //Program name: C.c 02 #include <stdio.h> 03 #include <stdlib.h> 04 #include <time.h> 05 int main() 06 { int i; srand((unsigned)time(NULL)); for (i=0;i<5;i++) printf("%d\n",rand()); system("PAUSE"); return 0; 13 } 執行結果: 29921 18871 385 32432 808

11 範例 D 以程式模擬產生一個10以內加法的測驗,由使用者計算後輸入答案,如果答錯則重新出題,直到答對為止

12 範例06-04-02D 01 //Program name:06-04-02D.c 02 #include <stdio.h>
03 #include <stdlib.h> 04 #include <time.h> 05 int main() 06 { 07 int n1,n2,ans; 08 srand((unsigned)time(NULL)); //重新選擇亂數種子數 09 do { 10 n1=rand()%10; //亂數取10以內的數 11 n2=rand()%10; 12 printf("%d + %d = ",n1,n2); 13 scanf("%d",&ans); 14 if (ans==n1+n2) //如果答對了,跳離do...while迴圈 15 break; 16 printf("答錯了!再來一題...\n"); 17 }while( ans!=(n1+n2) ); 18 printf("答對了!好棒喔!"); 19 system("PAUSE"); 20 return 0; 21 }

13 6-4.3 字串函式 在C語言的資料型態中,並沒有字串型態,而是用字元陣列或字元指標來表示
6-4.3 字串函式 在C語言的資料型態中,並沒有字串型態,而是用字元陣列或字元指標來表示 C語言有關字元或字串的處理函式大部分都宣告在string.h表頭檔 可以分成兩類: 具有破壞性的字串函式:當傳遞參數給字串函式,函式執行完畢返回後,該參數的內容可能已經改變其值 不具破壞性的字串函式:傳遞給函式的參數,在處理完畢後仍然是原來的值,沒有受到破壞

14 常用具破壞性的字串函式 strcat 【原型】char *strcat( char *strDest, const char *strSrc ); 【表 頭 檔】<string.h> 【功  能】將strSrc複製一份連接在strDest之後,傳回strDest。 【參數說明】strDest:連接的目的字串。 strSrc:被連接的來源字串。 【範  例】 char a[20]="Happy",b[20]="Birthday"; strcat(a,b); printf("%s",a); //a字串的內容已經結合了b字串 執行結果: HappyBirthday

15 常用具破壞性的字串函式 strcpy 【原型】char *strcpy( char *strDest, const char *strSrc ); 【表 頭 檔】<string.h> 【功  能】複製strSrc所有字元到strDest,並傳回strDest。 【參數說明】strDest:複製的目的字串。 strSrc:複製的來源字串。 【範  例】 char a[20]="Happy",b[20]="Birthday"; strcpy(a,b); printf("%s",a); //b字串的內容已經複製給a字串了 執行結果: Birthday

16 常用不具破壞性的字串函式 strlen 【函式原型】size_t strlen( const char *string );
【表 頭 檔】<string.h> 【功  能】傳回string的長度,不包括結束字元’\0’。 【參數說明】string:欲求長度的字串。 【範  例】 char a[20]="Happy",b[20]="Birthday"; printf("a字串的長度=%d\n",strlen(a)); printf("b字串的長度=%d\n",strlen(b)); 執行結果: a字串的長度=5 b字串的長度=8

17 常用不具破壞性的字串函式 strcmp 【函式原型】int strcmp( const char *str1, const char *str2 ); 【表 頭 檔】<string.h> 【功  能】比較str1和str2,傳回值為負數則表示str1比str2小,傳回0則表示str1等於str2,傳回正數表示str1比str2大。所謂大小,是指比較字串每一個字元的ASCII碼的大小。 【參數說明】str1:欲比較的第一個字串。 str2:欲比較的第二個字串。 【範  例】 char a[]="happy",b[]="happen"; if (strcmp(a,b)) //若a和b字串不相同,strcmp會傳回非0 printf("a字串和b字串不相同!"); else printf("a字串和b字串相同!"); 執行結果: a字串和b字串不相同!

18 常用不具破壞性的字串函式 strchr 【函式原型】char *strchr( const char *string, int c );
【表 頭 檔】<string.h> 【功  能】找出字元c在string中的位址,若尋找失敗,傳回NULL。 【參數說明】string:尋找的字串。 c:欲找尋的字元ASCII碼。 【範  例】 char a[]="This is a test string"; char sc='s'; //欲尋找的字元 char *sp=strchr(a,sc); int position=sp-a+1; printf("a字串=%s\n",a); if (sp!=NULL) printf("a字串中第一個's'字元出現在第%d個字元",position); else printf("找不到%c字元!",sc); 執行結果: a字串=This is a test string a字串中第一個's'字元出現在第4個字元

19 常用不具破壞性的字串函式 strstr 【函式原型】char *strstr( const char *string, const char *strCharSet ); 【表 頭 檔】<string.h> 【功  能】傳回string中第一個出現strCharSet的位址,若尋找失敗,傳回NULL。 【參數說明】string:尋找的字串。 strCharSet:欲找尋的字元集合。 【範  例】 char a[]="This is a test string."; char b[]="test"; char *sp=strstr(a,b); int position=sp-a+1; printf("a字串=%s\n",a); printf("b字串=%s\n",b); if (sp!=NULL) printf("b字串出現在a字串中第%d個字元",position); else printf("找不到相同字元!"); 執行結果: a字串=This is a test string. b字串=test b字串出現在a字串中第11個字元

20 範例 B 輸入任意三個字串a,b,c, 將a字串中含有b字串的部分, 全部換成c字串, 並列印出改過後之a字串。 (註:b和c字串的長度相同)

21 範例06-04-03B 01 //Program name:06-04-03B.c 05 int main()
02 #include <stdio.h> 03 #include <stdlib.h> 04 #include <string.h> 05 int main() 06 { char a[40],b[10],c[10]; char *p=a; //p記錄找到b字串的位置 09 printf("請輸入一個句子:"); scanf("%[a-z,A-Z,0-9, ]s",a); printf("請輸入找尋的字串:"); scanf("%s",b); printf("請輸入取代的字串:"); scanf("%s",c); while (p!=NULL) { p=strstr(a,b); //在a字串中找b字串 if (p==NULL) break; strncpy(p,c,strlen(c)); //找到後替換乘c字串 } printf("字串替換後 = %s",a); system("PAUSE"); return 0; 25 }


Download ppt "函式庫補充資料."

Similar presentations


Ads by Google