Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stdlib.h & time.h. String to numbers #include double atof(char *str); // 把 string 轉為 double #include int atoi(char *str); // 把 string 轉為 integer #include.

Similar presentations


Presentation on theme: "Stdlib.h & time.h. String to numbers #include double atof(char *str); // 把 string 轉為 double #include int atoi(char *str); // 把 string 轉為 integer #include."— Presentation transcript:

1 Stdlib.h & time.h

2 String to numbers #include double atof(char *str); // 把 string 轉為 double #include int atoi(char *str); // 把 string 轉為 integer #include long int atol(char *str); // 把 string 轉為 long integer

3 #include double myexp(const double, const int); main(int argn, char *argv[]) { double x; int n; if (argn < 3) return(1); x = atof(argv[1]); n = atoi(argv[2]); printf("Sum up to %d terms\n", n); printf("val(%lf) = %.15lf\n", x, myexp(x, n)); } Example-1

4 Time and date display #include long *sys_time, run_time, run_second; run_time = clock(); // 回傳程式執行迄今時間 run_second = run_time / CLK_TCK;// 換算為秒數 //CLK_TCK=1000 time(sys_time); //sys_time 記錄系統時間 printf(“%s\n”, ctime(sys_time)); // 把系統時間表為年月日時分秒格式 // Wed Apr 07 02:26:12 2004

5 struct tm *localtime(long*sys_time) long *sys_time; struct tm *tt; time(sys_time); // 傳回 system time. tt = localtime(sys_time); // 轉為 struct tm 格式 printf("Month=%d day=%d\nhour=%d minute=%d second=%d\n", tt->tm_mon, tt->tm_mday, tt->tm_hour, tt->tm_min, tt->tm_sec);

6 struct tm { int tm_sec; // 秒, 0-59 int tm_min; // 分, 0-59 int tm_hour; // 時, 0-23 int tm_mday; // 日, 1-31 int tm_mon; // 月, 0-11 int tm_year; // 年, 1900- int tm_wday; // 星期, 0-6 int tm_yday; // 天, 0-365 int tm_isdst; // >0: 陽光節約時間 } struct tm *tp, tt; printf(“Month=%d day=%d\n”,tt.tm_mon, tt.tm_mday); printf(“Month=%d day=%d\n”,tp->tm_mon, tp->tm_mday);

7 Random number #include main() { unsigned int sd1=3911U, sd2=99873U, rn; int i; srand(sd1); // set initial seed. for (i=0; i<5; i++) { rn = rand(); printf("No %2d : %7u %f\n",(i+1),rn, (float)rn/RAND_MAX); }

8 Random walker 1. 設定 ensm_max, step_max, step_chk, no_chk. 2.Run loop ensm=0  ensm_max-1. 3. 開始一個 ensemble: nchk=1; x=y=0. 4.Run step = 1  step_max: 4-a. nd = rand()%4; switch(nd) case(0): x++; case(1): x--; case(2): y++; case(3): y--; 4-b. if step%step_chk == 0: r2[nchk] += (x*x+y*x)/ensm_max; 5. 重複 (4) 直到 step > step_max. 6. 重複 3-5 直到 ensm == ensm_max. 7. 輸出 r2[i], i=0 -- no_chk.

9 Dynamic allocation #include void *calloc(unsigned num, unsigned size); // 設定 num 個大小為 size 的 array. void *malloc(unsigned total_size); // 設定總大小為 total_size 的 array. void *realloc(void *ptr, unsigned total_size); // 將 prt 指向的記憶體重設為 total_size void free(void *ptr); // 釋放 ptr 指向的記譯憶體回 heap

10 #include main(int argn, char*argv[]) { unsigned int ndim, size, total_size, i; double rr, *dp1, *dp2; ndim = (unsigned)atoi(argv[1]); size = sizeof(double); if (dp1 = calloc(ndim, size), dp1!=NULL) { for (i=0; i<ndim; i++) dp1[i] = 0.01 * (double)i; for (i=0; i<ndim; i++) printf("No %3d : %lf\n", (i+1), dp1[i]); } total_size = ndim * size; if (dp2 = malloc(total_size), dp2!=NULL) { for (i=0; i<ndim; i++) dp2[i] = 0.02 * (double)i; for (i=0; i<ndim; i++) printf("No %3d : %lf\n", (i+1), dp2[i]); } Example-5


Download ppt "Stdlib.h & time.h. String to numbers #include double atof(char *str); // 把 string 轉為 double #include int atoi(char *str); // 把 string 轉為 integer #include."

Similar presentations


Ads by Google