gpt4 book ai didi

C编程,如何在等待用户输入时运行for循环

转载 作者:太空宇宙 更新时间:2023-11-04 00:00:23 25 4
gpt4 key购买 nike

我的编码作业遇到了麻烦。

我想要实现的要点是运行一个程序,该程序每 1 分钟在“A”和“B”之间交替一个变量 char OUTPUT。所以第一分钟,OUTPUT 应该是 A。第二分钟,它会是 B。依此类推。

但是,如果用户输入字符 P,则 OUTPUT 将在一分钟内为 P,然后返回到 A/B 循环。

我还没有完全写下代码,因为我的尝试都没有奏效。我试过使用 while 和 for 循环。但是每次我使用 scanf 或 getchar() 时,程序都会无限期地停止以等待用户输入。

void timer(){
int t, d, change;
char p;

for(t = 1; t <= 30000; t++)
for(d = 1; d <= 30000; d++){
if(t == 30000){
change = 1;
}
else if(p == 'p'){
change = 0;
}
else{
p = getchar();
}

有点像我现在拥有的。这是一个相当不完整的函数,但 change 应该返回 1 或 0 并且在 main 函数中,如果它是 1,则 OUTPUT 将更改为 A/B。

我也做了一些研究,大多数人推荐多线程,但我们在类里面没有学过,所以我们不能将其用于程序。我也无权使用 sleep() 或 delay()。基本上我仅限于标准 c 库。

最佳答案

如果您仍然卡住了,那么这里是一个使用 pselect 的简短示例(设置为不阻塞,例如 struct timespec 的两个字段都设置为 0)。它只是使用无限循环来检查 pselect 以确定输入是否正在等待在 stdin 上读取。如果是这样,它会读取输入,如果用户输入了 P(或任何以 P 开头的字符串),则输出 P,每秒一次在一段时间内,在使用 AB 恢复之前(在“P”之前最后未显示的内容)。

我将时间段当前设置为 10 秒。 用于测试,只需调整 #define PERIOD 10 常量以更改为您需要的任何秒数(例如60 在你的例子中)我不会看一分钟的 A 的B 的P 的 ...

pselect 的使用相当简单。它与 select 相同,除了 timeval 结构 select 用于时间和 select 没有 sigmask 参数。 (此处无关)关键是将timespec结构的两个字段都设置为0,这样pselect就不会阻塞等待输入。您只需调用它,如果返回值是 1,那么您就有输入等待。然后它只是读取输入的问题(我选择了 fgets 所以它会读取并使用尾随的 '\n')。检查用户是否输入了P(通过检查buf中的第一个字符),如果是,则开始一个新的周期输出P,否则只需继续缓慢地处理您的 AB 输出。下面还有其他内联评论:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/select.h>
#include <unistd.h>

#define MAXC 64 /* size of buffer for fgets */
#define PERIOD 10 /* no. of seconds for alternating period */

/* simple function calling pselect, returns 1 when input waiting */
int haveinput (int filedes)
{
fd_set set;
struct timespec timeout;

/* Initialize the file descriptor set. */
FD_ZERO (&set);
FD_SET (filedes, &set);

/* Initialize the timeout data structure. */
timeout.tv_sec = 0; /* timeout 0 - immediately return, */
timeout.tv_nsec = 0; /* if NULL, blocks indefinitely. */

return pselect (filedes + 1, &set, NULL, NULL, &timeout, NULL);
}

int main (void)
{
int c = 'A',
last = c; /* holds last 'A' or 'B' for pickup after 'P' */
unsigned long begin = time (NULL); /* begin seconds of period */

for (;;) { /* loop until 'q' entered or EOF */
unsigned long current = time (NULL); /* current seconds */
if (haveinput (STDIN_FILENO) == 1) { /* is input ready? */
char buf[MAXC] = ""; /* if so, get it! */
if (!fgets (buf, MAXC, stdin) || *buf == 'q')
break; /* if EOF or 'q' -- bail */
if (*buf == 'P') { /* if user entered 'P' */
c = 'P'; /* set c = 'P' */
begin = time (NULL); /* restart time period */
}
}
else {
putchar (c); /* if no imput read, output char */
fflush (stdout); /* output is buffered so fflush */
sleep (1); /* arbitrary second to wait */
}
if (current - begin >= PERIOD) { /* end of period reached */
if (last == 'A') /* if last was 'A' use 'B' */
c = last = 'B';
else /* otherwise use 'A' */
c = last = 'A';
begin = time (NULL); /* restart time period */
putchar ('\n');
}
}
putchar ('\n');

return 0;
}

(注意:这是一个'nix特定的解决方案。Windows不提供sys/select.h(MinGW也不提供)。虽然windows提供了select 通过 Winsock2.h 和 lib Ws2_32.lib 它的行为与此处的实现不一致。)

示例使用/输出

以 10 秒的交替周期。而不是 60:

$ ./bin/pselect_abp
AAAAAAAAAAA
BBBBBBBBBBB
AAAAAAP
PPPPPPPPPPP
BBBBBBBBBBB
AAAAAAzz
AAAAA
BBBBBBBBBBB
AAAqA

试一试,仔细检查一下,如果您还有其他问题,请告诉我。

关于C编程,如何在等待用户输入时运行for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47117260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com