无需等待即可从用户那里获得输入 - C 语言..!
Getting an input from the user without having to wait - C language..!
我目前正在编写一个简单的计时器,它从 00 秒运行到 55 秒,然后再次从 00 开始并一直计数,直到用户停止它。为此,我为用户提供了两个选项:1. 开始和 2. 重置。选择第一个运行程序,然后选择第二个,如我所料,会将计时器变为 00s 并保持在那里。
现在我面临的问题是我想在不停止计时器的情况下获得用户的输入(即允许用户在程序运行时随时输入 2 运行 以便他可以停止正在进行的计数)。我尝试使用像 getch() 和 scanf() 这样的函数,但它们停止了计时器,这完全破坏了程序。
感谢大家的帮助..!!
您可以使用 ncurses 来完成此操作。在 Windows 上,可以用 kbhit()
和 getch()
做类似的事情
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <ncurses.h>
int main()
{
char ch = 0;
int row = 0;
int line = 0;
int col = 0;
int counting = 1;
char text[80] = {""};
long int elapsed = 0;
struct timeval start;
struct timeval stop;
initscr ( );
noecho ( );
timeout(100);
getmaxyx ( stdscr, row, col);
line = row / 2;
snprintf ( text, sizeof ( text), "press 1 to exit or 2 to reset");
mvprintw ( line - 1, ( col - strlen ( text)) / 2,"%s", text);
gettimeofday ( &start, NULL);
while ( 1) {
if ( counting) {
gettimeofday ( &stop, NULL);
elapsed = (stop.tv_sec - start.tv_sec);
snprintf ( text, sizeof ( text), " %ld ", elapsed);
mvprintw ( line, ( col - strlen ( text)) / 2,"%s", text);
if ( elapsed > 54) {
gettimeofday ( &start, NULL);
}
}
else {
snprintf ( text, sizeof ( text), "paused press 1 or 2");
mvprintw ( line, ( col - strlen ( text)) / 2,"%s", text);
}
//refresh ( );
if ( ( ch = getch ( )) == '1' || ch == '2') {
if ( ch == '2') {
counting = !counting;
if ( counting) {
gettimeofday ( &start, NULL);
}
}
if ( ch == '1') {
break;
}
}
}
endwin ( );
return 0;
}
我目前正在编写一个简单的计时器,它从 00 秒运行到 55 秒,然后再次从 00 开始并一直计数,直到用户停止它。为此,我为用户提供了两个选项:1. 开始和 2. 重置。选择第一个运行程序,然后选择第二个,如我所料,会将计时器变为 00s 并保持在那里。
现在我面临的问题是我想在不停止计时器的情况下获得用户的输入(即允许用户在程序运行时随时输入 2 运行 以便他可以停止正在进行的计数)。我尝试使用像 getch() 和 scanf() 这样的函数,但它们停止了计时器,这完全破坏了程序。
感谢大家的帮助..!!
您可以使用 ncurses 来完成此操作。在 Windows 上,可以用 kbhit()
和 getch()
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <ncurses.h>
int main()
{
char ch = 0;
int row = 0;
int line = 0;
int col = 0;
int counting = 1;
char text[80] = {""};
long int elapsed = 0;
struct timeval start;
struct timeval stop;
initscr ( );
noecho ( );
timeout(100);
getmaxyx ( stdscr, row, col);
line = row / 2;
snprintf ( text, sizeof ( text), "press 1 to exit or 2 to reset");
mvprintw ( line - 1, ( col - strlen ( text)) / 2,"%s", text);
gettimeofday ( &start, NULL);
while ( 1) {
if ( counting) {
gettimeofday ( &stop, NULL);
elapsed = (stop.tv_sec - start.tv_sec);
snprintf ( text, sizeof ( text), " %ld ", elapsed);
mvprintw ( line, ( col - strlen ( text)) / 2,"%s", text);
if ( elapsed > 54) {
gettimeofday ( &start, NULL);
}
}
else {
snprintf ( text, sizeof ( text), "paused press 1 or 2");
mvprintw ( line, ( col - strlen ( text)) / 2,"%s", text);
}
//refresh ( );
if ( ( ch = getch ( )) == '1' || ch == '2') {
if ( ch == '2') {
counting = !counting;
if ( counting) {
gettimeofday ( &start, NULL);
}
}
if ( ch == '1') {
break;
}
}
}
endwin ( );
return 0;
}