标准C语言中的计时器程序

3

我正在尝试使用标准C-Free 5.0创建一个秒表程序。以下是目前的代码:

#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'继续,并始终显示计时器。


C-Free是一个集成开发环境,而不是编译器。您能告诉我们您正在使用哪个编译器吗?因为在一些支持的编译器库中会有可用的kbhit()函数。 - fvu
4个回答

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");
    }
}

2
@JitzuZu 当你得到满意的答案时,对于 Stack Overflow 的运作来说,接受答案非常重要(点击答案左上角的勾号)。你还没有接受任何问题的答案,所以去检查一下,并在回答后接受它们。 - hyde

2
由于您使用了system("cls");,这可能是在dos / Windows命令提示符上。您可以尝试查看您的编译器是否支持conio.h
如果支持,kbhit()_kbhit()(链接到MSDN,请查看您的编译器库的文档以获取最准确的参考)似乎是您需要使用的内容。

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;
}

0

这是一个系统问题,而不是C语言的问题。一般来说,您的主机系统会为输入提供缓冲区,因此当您按下键时,它并不会立即传递给您的程序,而是在某些条件发生时进行缓冲(基本上是按下行结束符)。

在Windows下,您需要调用不同的函数来获取按键。

在Unix下,您应该将tty设置为非规范模式(有一组神奇的tcgetattrtcsetattr调用)。

请参见例如之一


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接