gpt4 book ai didi

C - 禁用用户已经使用的可能性

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:18:17 25 4
gpt4 key购买 nike

我在任何地方都找不到这个...

我正在我的大学做一个项目——非常简单。我有一些用户可以提出的问题/可能性......在使用它之后,我想禁用它并且永远不会显示,以便用户可以在“while”循环中继续进行到另一个。

例子:

while (something == true)
{
printf("Your possibilities are:\n 1 - Use a hammer\n 2 - Use a pipe\n 3 - Use a chair\n");

choice = getchar();

switch (choice)
{
case '1':
someCode();
break;
case '2':
someCode();
break;
case '3':
someCode();
break;
}}

这当然是一个例子。问题是,在用户使用锤子之后,会发生一些代码然后 - 我只想要 2 种可能性:“管道”和“椅子”。所以下一次机会只能是“双向”切换。在我的代码中,我有 6 种可能性,并为每个选择生成 bool 值,并在 if-else 中组合所有六个 bool 值,然后为每种可能性编写一个开关是不可想象的。

有什么解决办法吗?可能每种可能性都需要一个 bool 变量(如果使用的话),但我需要以某种方式定义该开关。

谢谢!

最佳答案

创建一个项目列表和一组平行的标志以跟踪已使用的内容。
下面是一些伪代码

#include <stdint.h>
#include <stdbool.h>

const char *item[] =
{ "a hammer", "a pipe", "a chair", "an awl", "an hour", 0 };
#define ITEM_N (sizeof item/sizeof item[0] - 1)

// return EOF on end-of-file/input error
// return 0 when no choices
// return 0 when non-numeric data entered
// return 0 when 0 or too high a value entered
// return 0 when a used value is selected
int Choice(bool *Used) {
int choice = -1;
puts("Your possibilities are:");
for (int i = 0; item[i]; i++) {
if (test_if_value_i_has_not_been_used) {
print_prompt;
choice = 0;
}
}
if (choice < 0) {
puts(" None");
return 0;
}
char buf[50];
read_user_input_into(buf); // return EOF if no input
if (!an_int_was_entered(&choice) ) return 0;
if (choice_in_range) return 0;
if (was_choice_used_before) return 0;
Used[choice - 1] = true;
return choice;
}

int main(void) {
bool Used[ITEM_N] = { 0 }; // clear all flags
while (Choice(Used))
;
return 0;
}

关于C - 禁用用户已经使用的可能性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40470119/

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