2013-03-23 92 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; 
}