1 / 12

函式庫補充資料

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

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

  3. 6-4.1 數學函式 常用的數學函式(參考課本6-31頁)

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

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

  6. 6-4.2 亂數函式 例如: 01 //Program name:06-04-02A.c 02 #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 } 執行結果: 41 18467 6334 26500 19169

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

  8. 6-4.2 亂數函式 例如: 01 //Program name:06-04-02B.c 02 #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 } 執行結果: 365 1216 5415 16704 24504

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

  10. 6-4.2 亂數函式 例如: 01 //Program name:06-04-02C.c 02 #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 } 執行結果: 29921 18871 385 32432 808

  11. 範例06-04-02D 以程式模擬產生一個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 }

More Related