3

我正在尝试使用这个 Standard 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”继续,同时始终显示计时器。

4

4 回答 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");
    }
}
于 2013-03-23T12:26:41.300 回答
2

由于您使用system("cls");,这可能是在 dos / Windows 命令提示符下。您可以尝试查看您的编译器是否支持conio.h 。

如果是,kbhit()或者_kbhit()(链接到 MSDN,您应该检查编译器库的文档以获得最准确的参考)似乎是您需要使用的。

于 2013-03-23T12:25:54.870 回答
0

这是一个系统问题,而不是 C。通常,您的托管系统为输入提供缓冲,因此当您按下一个键时,它不会在那个时候传递给您的程序,它会被缓冲直到发生某种情况(基本上,一个 end-离线被按下)。

在 Windows 下,您应该拨打不同的电话来获得按键。

在 Unix 下,您应该将您的 tty 置于非规范模式(有一组对tcgetattrand的魔术调用tcsetattr)。

例如看那个

于 2013-03-23T12:24:47.223 回答
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;
}
于 2016-06-14T13:27:13.737 回答