- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的编码作业遇到了麻烦。
我想要实现的要点是运行一个程序,该程序每 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
,每秒一次在一段时间内,在使用 A
或 B
恢复之前(在“P”之前最后未显示的内容)。
我将时间段当前设置为 10 秒。
用于测试,只需调整 #define PERIOD 10
常量以更改为您需要的任何秒数(例如60
在你的例子中)我不会看一分钟的 A 的
,B 的
或 P 的
...
pselect
的使用相当简单。它与 select
相同,除了 timeval
结构 select
用于时间和 select
没有 sigmask
参数。 (此处无关)关键是将timespec
结构的两个字段都设置为0
,这样pselect
就不会阻塞等待输入。您只需调用它,如果返回值是 1
,那么您就有输入等待。然后它只是读取输入的问题(我选择了 fgets
所以它会读取并使用尾随的 '\n')。检查用户是否输入了P
(通过检查buf
中的第一个字符),如果是,则开始一个新的周期输出P
,否则只需继续缓慢地处理您的 A
或 B
输出。下面还有其他内联评论:
#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/
我有一个测试即将进行,我想澄清两个有关参数的问题。 在我的笔记中指出,将参数传递给函数的推荐方法是使用“按引用传递” const type& x; // for "in" parameters
当我通过 OMG 2.5(Beta)推广的 UML 规范阅读以下概念时: in: Indicates that Parameter values are passed in by the caller
我试图在用户按下 Enter 时触发一个函数。我将此输入设置为只读的原因是限制用户在填充值后修改输入中的值。 该值来自将在点击属性中触发的弹出窗口。问题是 keyup.enter 没有触发该输入。 代
我在jQuery中使用模式弹出窗口控件,该弹出窗口具有由jQuery Tokenize输入插件提供动力的输入文本。问题是,当我在模式弹出文本框中键入内容时, token 化插件的搜索结果显示为隐藏在弹
我有一个问题。当我选中复选框时,系统工作正常,总值发生变化,但一旦我修改文本输入,它就会变为 0。我需要将此文本框输入排除在更改值之外。 这是 html: $15000 $
我正在努力让它发挥作用,但还是有些不对劲。 我想用 CSS 设置提交按钮的样式以匹配我已有的按钮。 风格: input[type="button"], input[type="submit"], b
import java.util.*;; public class selection { Scanner in=new Scanner(System.in); private
这可能是一个非常菜鸟的问题。假设我有一个带宽限制为 100MB/s 的网卡,那么输入/输出带宽是否有可能达到该限制 同时 ?或者我会在任何时候遇到这个不等式:in bandwidth + out ba
看着这个问题,Fill immutable map with for loop upon creation ,我很好奇是什么this表示在 Map(1 -> this) . scala> Map(1
我有这样的东西 一个 乙 问? 是或否 数字 数字或零 我想做的是: 如果 B1 = “Y”,则让用户在 B2 中输入一个数字。 如果 B1 = “N”,则将 B2 中的值更改为零,并且不允许用户在
我有一个包含许多列的表,我想添加 input标题单元格内的字段,但我希望输入适合根据正文内容的宽度。 这是没有 input 的样子领域: 这就是 input 的样子领域: 可以看出,像“index”和
关于为 FTP 客户端设置传出和传入文件夹,您遵循哪些最佳实践(如果有)?我们通常使用“outgoing”和“incoming”,但无论你如何表述方向,它都可以有两种解释方式,具体取决于名称相对于哪一
我正在尝试“求解”给定 d 的 Pell 方程:x^2 - d * y^2 = 1,或者至少我想得到最小的 x > 0 来求解方程。到目前为止,一切都很好。这是我的 Haskell 代码 minX :
我是VS Code的新手,可以使用Ctrl + Enter将代码运行到python交互式窗口中。我希望光标自动移动到下一行,因此我可以逐行浏览代码。 能做到吗? 最佳答案 如this blog pos
我正在创建一个 bool 值矩阵/二维数组,并且我想为 dategrid 推断一种不仅仅是“ANY”的类型。 let yearRange = [2000,2001,2002,2003,2004]; l
我有两个排序的列表,例如 a = [1, 4, 7, 8] b = [1, 2, 3, 4, 5, 6] 我想知道a中的每个项目是否在b中。对于上面的示例,我想找到 a_in_b = [True, T
菜鸟警报 这很奇怪 - 当我编写以下代码时,尝试在 AngularJS 中创建自定义指令: myModule.directive('myTab', function(){ console.lo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
假设我正在使用 gdscript 静态类型,并且对于一个函数参数,我事先不知道我会得到什么。这就是 python 中 typing.Any 的作用。如何使用 gdscript 做到这一点? 似乎 Va
我使用 dropzone 上传多个图像,并且工作正常,直到我想为每个图像插入品牌和网址。 我遇到的唯一问题是,当我要从输入字段获取值时,我会从服务器获取来自字段(品牌、网址)的未定义值,但如果我使用静
我是一名优秀的程序员,十分优秀!