【问题标题】:Stopwatch program in standard C标准 C 中的秒表程序
【发布时间】:2016-06-14 21:27:13
【问题描述】:

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

【问题讨论】:

  • C-Free 是 IDE,而不是编译器。你能告诉我们你用的是什么编译器吗?因为在一些受支持的编译器库中,会有像kbhit() 这样的函数可用。

标签: c


【解决方案1】:

这应该对_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");
    }
}

【讨论】:

  • @JitzuZu 当您得到满意的答案时,对于 Stack Overflow 的工作方式,您必须接受答案(单击答案左上角的复选标记)。你还没有接受任何问题的答案,所以也去检查一下,如果有答案就接受。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    在 Windows 下,您应该通过不同的调用来获得按键。

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

    看到one for example

    【讨论】:

      【解决方案4】:
      #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;
      }
      

      【讨论】:

        猜你喜欢
        • 2018-10-31
        • 1970-01-01
        • 1970-01-01
        • 2011-05-22
        • 1970-01-01
        • 1970-01-01
        • 2011-05-05
        • 1970-01-01
        • 2013-06-12
        相关资源
        最近更新 更多