2013-03-23 85 views
3

我正嘗試使用此標準C-Free 5.0創建秒錶的程序。這是我到目前爲止:標準C中的秒錶程序C

#include <stdio.h> 
#include <time.h> 
#include <stdlib.h> 
#include <windows.h> 

char button; 
int minutes=0, seconds=0, millisec=0; 

int main(void) 
{ 
    while(1) 
    { 
     reset: 
     button = '\0'; 
     int minutes=0, seconds=0, millisec=0; 
     printf(" %d : %d : %d ", minutes, seconds, millisec); 
     system("cls"); 
     if(button == 'a') 
     { 
      while(1) 
      { 
       cont: 
       button = '\0'; 
       Sleep(10); 
       millisec++; 
       if(millisec == 100) 
       { 
        millisec = 0; 
        seconds++; 
        if(seconds == 60) 
        { 
         seconds = 0; 
         minutes++; 
        } 
       } 
       printf(" %d : %d : %d ", minutes, seconds, millisec); 
       system("cls"); 
       if(button == 's') 
       { 
        while(1) 
        { 
         button = '\0'; 
         printf(" %d : %d : %d ", minutes, seconds, millisec); 
         system("cls"); 
         if(button == 'a') 
         { 
          goto cont; 
         } 
         if(button == 'd') 
         { 
          goto reset; 
         } 
        } 
       } 
      } 
     } 
    } 
} 

我想按下按鈕'a'開始秒錶,但它不會工作。使用scanf()會暫停整個程序。有沒有辦法檢測到按下按鈕並繼續秒錶程序?我的意思是,在不停止程序的情況下,特別是按's'停止並再次按'a'以繼續,同時始終顯示定時器。

+0

不在標準C. – md5 2013-03-23 12:20:20

+0

[C-免](http://www.programarts.com/cfree_en/index.htm)是IDE,而不是一個編譯器。你能告訴我們你使用的是什麼編譯器嗎?因爲在一些支持的編譯器庫中會有像'kbhit()'這樣的函數可用。 – fvu 2013-03-23 12:24:17

回答

0

這是一個系統問題而無法C.一般情況下,您的主機系統提供緩衝,以輸入,所以當你按下一個鍵,它是不是在那個時候你的程序,直到某些條件發生(基本交付,則緩衝,結束行被按下)。

在Windows下,您應該進行不同的調用來獲得按鍵。

在Unix下,你應該把你的tty設置爲非規範模式(有一組神奇的調用tcgetattrtcsetattr)。

請參閱one for example

2

由於您使用system("cls");,這可能是在DOS/Windows命令提示符。您可以嘗試查看編譯器是否支持conio.h

如果是,kbhit() or _kbhit()(鏈接到MSDN,你應該檢查編譯器庫的文檔以獲得最準確的參考)似乎是你需要使用的。

3

這應該有助於_kbhit,在它之後使用_getch()很重要。

#include <conio.h> 

//... 

int key; 
while (1) 
{ 
    if (_kbhit()) 
    { 
     key = _getch(); 

     if (key == 'a') 
      printf("You pressed 'a'\n"); 
     else if (key == 'd') 
      printf("You pressed 'd'\n"); 
    } 
} 
+0

kbhit()完全工作...真棒..非常感謝你... ^^ – 2013-03-23 12:49:56

+2

@JitzuZu當你得到滿意的答案時,它是如何堆棧溢出工作,你接受答案是重要的(點擊複選標記在答案的左上角)。你沒有接受任何你的問題的答案,所以去檢查他們,並接受他們是否回答。 – hyde 2013-03-23 13:18:53

0
#include<stdio.h> 
#include<conio.h> 
#include<dos.h> 
#include<time.h> 
#include<windows.h> 
main() 
{ 
int choice, h,m,s; h=0; m=0; s=0; //--variable declaration--// 
char p= 'p'; 
printf("Press 1 to start the timer\nPress 2 to exit\n"); 
printf("\nEnter your choice\n"); 
scanf("%d",&choice); 
switch(choice) //--switch case --// 
{ 
case 1: 
{ 
while(1) //--while condition is true// 
{ 
if(s>59) //--if seconds(s) is > 59--// 
{ 
m=m+1; //--increment minute by 1--// 
s=0; 
} 
if(m>59) //--if minutes(s) is > 59--// 
{ 
h=h+1; //--increment hour by 1--// 
m=0; 
} 
if(h>11) //--if hour(h) is > 11--// 
{ 
h=0; //-Hour to 0--// 
m=0; 
s=0; 
} 
Sleep(1000); //--inbuilt function for 1sec delay--// 
s=s+1; 
system("cls"); //--Clear screen--// 
printf("DIGITAL CLOCK"); 
printf("\n\nHOUR:MINUTE:SECOND"); 
printf("\n\n%d:%d:%d",h,m,s); //--Print time--// 
printf("\n\nTo pause : press P\n"); 
if(kbhit()) //--Check if any button is pressed on keyboard--// 
{ 
if(p==getch()) //--Check if P is pressed--// 
{ 
system("pause"); //--Inbuilt function for pause and resume--// 
} 
} 
} 
break; 
} 
case 2: 
exit(0); //--Exit --// 
default: 
{ 
printf("Wrong Choice"); 
} 
} 
getch(); //--Holding the screen--// 
return 0; 
}