[問題] 關於撰寫計時器的程式

看板C_and_CPP (C/C++)作者 (totoron)時間14年前 (2011/09/10 11:11), 編輯推噓1(104)
留言5則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) 想請問以下的程式碼: #include <stdio.h> #include <stdlib.h> #include <time.h> int main(int argc, char *argv[]) { struct tm *ptrnow; int i; time_t loc_now, std_now; time(&std_now); i=0; while(1){ time(&loc_now); if(loc_now-std_now == i){ system("cls"); ptrnow = localtime(&loc_now); printf("%s", asctime(ptrnow)); i++; } } system("PAUSE"); return 0; } 之後跑出的結果會是: Sat Sep 10 11:01:06 2011 07 08 ..... 之後按照秒數一直跑..... 書上是說,利用time()可得到從標準時(格林威治時間)1970/1/1 00:00:00起所經過的秒數 那我想問的是,既然time()函數的參數是loc_now與std_now. 那為何在if的條件式中loc_now-std_now == i,loc_now-std_now會先差0,接著差1,再接著 差2,此條件式想表達的究竟是什麼? 謝謝~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.230.124.116

09/10 11:23, , 1F
你知道 time(&std_now); 做了什麼事嗎?
09/10 11:23, 1F

09/10 11:25, , 2F
如果知道那你應該不難理解 loc_now 和 std_now 的差別在哪
09/10 11:25, 2F

09/10 11:25, , 3F
這樣你就會了解它們差 0 等等等是什麼意思了
09/10 11:25, 3F

09/10 12:30, , 4F

09/10 12:31, , 5F
DRY, DIE, 寫程式看最多的是字典跟文件
09/10 12:31, 5F
文章代碼(AID): #1EQjLOUq (C_and_CPP)
文章代碼(AID): #1EQjLOUq (C_and_CPP)