函式庫補充資料

12
函函函函函函函

description

函式庫補充資料. 6-4  函式庫. C 語言編譯軟體內部提供了很多常用的函式,它們集中在所謂的程式庫或 函式庫 使用函式庫中的函式只需將此檔利用 #include 指令包含進來就可以了. 6-4.1  數學函式. 常用的數學函式(參考課本 6-31 頁). 6-4.1  數學函式. 常用的數學函式. 6-4.2 亂數函式. 所謂 亂數 ( Random number ),是由電腦自動產生一個數字 電腦的亂數其實是由一個 亂數產生器 產生的,函式名稱是 rand ,使用前記得將其表頭檔 包含進來 格式:. - PowerPoint PPT Presentation

Transcript of 函式庫補充資料

Page 1: 函式庫補充資料

函式庫補充資料

Page 2: 函式庫補充資料

6-4  函式庫 C 語言編譯軟體內部提供了很多常用的函式,

它們集中在所謂的程式庫或函式庫 使用函式庫中的函式只需將此檔利用 #include

指令包含進來就可以了

主程式引用數學函式引用字串函式引用輸出入函式…………呼叫自訂函式1…

自訂函式1

自訂函式2

. . . . . .

函式庫

數學函式

字串函式

輸出入函式

繪圖函式

時間函式

轉換函式

C程式

#include <math.h>

#include <stdio.h>

#include <string.h>

Page 3: 函式庫補充資料

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 的絕對值(取正值)。<stdlib.h>或<math.h>

double sqrt(double x); 計算雙浮點數 x 的平方根值。 <math.h>

double pow(double x, double y);計算 x 的 y 次方值,即 xy 。 <math.h>

Page 4: 函式庫補充資料

6-4.1  數學函式 常用的數學函式

type __max( type a, type b );

傳回 a 和 b 中數值較大者,可以是任何型態的數值,特別注意的是 max 前面的底線有兩個。

<stdlib.h>

type __min( type a, type b );

和 __max 相反, __min 傳回 a和 b 中數值較小者。 <stdlib.h>

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 。

<math.h>

Page 5: 函式庫補充資料

6-4.2  亂數函式 所謂亂數( Random number ),是由電腦

自動產生一個數字 電腦的亂數其實是由一個亂數產生器產生

的,函式名稱是 rand ,使用前記得將其表頭檔 <stdlib.h> 包含進來

格式:int rand( void );

呼叫後會傳回 0~32767 之間的任意一數

Page 6: 函式庫補充資料

6-4.2  亂數函式 例如:

01 //Program name:06-04-02A.c02 #include <stdio.h>03 #include <stdlib.h>04 int main()05 {06 int i;07 for (i=0;i<5;i++)08 printf("%d\n",rand());09 system("PAUSE");10 return 0;11 }

執行結果:411846763342650019169

Page 7: 函式庫補充資料

6-4.2  亂數函式

每次執行程式,產生亂碼的順序卻都相同,這是因為亂數產生器的「種子」( seed)並沒有改變,所產生的亂數都是屬於同一組

改變種子,就會產生另一組亂數 語法:

void srand(unsigned int seed);

Page 8: 函式庫補充資料

6-4.2  亂數函式 例如:01 //Program name:06-04-02B.c02 #include <stdio.h>03 #include <stdlib.h>04 int main()05 {06 int i;07 srand(100);08 for (i=0;i<5;i++)09 printf("%d\n",rand());10 system("PAUSE");11 return 0;12 }

執行結果:365121654151670424504

Page 9: 函式庫補充資料

6-4.2  亂數函式 改了亂數產生器的種子數,以後每次執行時,還

是產生同樣的一組亂數 利用讀取系統時間的函式,產生一個種子數,讓

亂數產生器每次執行都不一樣 時間函數表頭檔: time.h 使用方法如下:

srand((unsigned) time ( NULL ) );

Page 10: 函式庫補充資料

6-4.2  亂數函式 例如:01 //Program name:06-04-02C.c02 #include <stdio.h>03 #include <stdlib.h>04 #include <time.h>05 int main()06 {07 int i;08 srand((unsigned)time(NULL));09 for (i=0;i<5;i++)10 printf("%d\n",rand());11 system("PAUSE");12 return 0;13 }

執行結果:299211887138532432808

Page 11: 函式庫補充資料

範例 06-04-02D

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

Page 12: 函式庫補充資料

範例 06-04-02D 01 //Program name:06-04-02D.c02 #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 }