Presentation is loading. Please wait.

Presentation is loading. Please wait.

函式庫補充資料 1.

Similar presentations


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

1 函式庫補充資料 1

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

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。 3

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。 4

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

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 6

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

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 8

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

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 10

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

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 } 12


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

Similar presentations


Ads by Google