[英]Stuck with code using if statements
This might be a non-sense question, but i'm kind of stuck so I was wondering if someone can help. 这可能是一个无稽之谈,但我有点困惑,所以我想知道是否有人可以提供帮助。 I have the following code:
我有以下代码:
bool while_condition=false;
do{
if(/*condition*/){
//code
}
else if(/*condition*/){
//code
}
else if(/*condition*/){
//code
}
...//some more else if
else{
//code
}
check_for_do_while_loop(while_condition, /*other parameters*/);
}while(while_condition);
the various if
and else if
exclude with each other but each have other if
inside; 各种
if
和else if
互相排斥,但if
在内部则彼此排斥; if a certain condition is met (which can't be specified in a single if statement), then the code return a value and the do while
loop is ended. 如果满足特定条件(无法在单个if语句中指定),则代码返回一个值,并且
do while
循环结束。 But if, after entering a single else if
, the conditions inside aren't met the code exit without actually doing nothing, and the while loop restart the whole. 但是,如果在输入了一个
else if
,则不满足内部条件的条件而没有实际执行任何操作就退出了,而while循环将重新启动整个过程。
I want the program to remember where he entered and avoid that part of the code, ie to avoid that specific else if
he entered without any result, so he can try entering another else if
. 我希望程序记住他输入的位置,并避免代码的那部分,即
else if
输入时没有任何结果, else if
避免使用else if
特定的代码,因此, else if
,他可以尝试输入else if
。 I thought about associating a boolean to the statements but I'm not quite sure on how to do it. 我曾考虑过将布尔值与这些语句相关联,但是我不确定如何做到这一点。 Is there a way which allows me not to modify the code structure too much?
有没有一种方法可以让我不必过多修改代码结构?
To give an idea of one way of approaching this that avoid loads of variables, here is an outline of how you might data-drive a solution. 为了提供一种避免变量加载的解决方法,这里概述了如何以数据驱动解决方案。
class TestItem
{
public:
typedef bool (*TestFuncDef)(const state_type& state_to_test, std::shared_ptr<result_type>& result_ptr);
TestItem(TestFuncDef test_fn_parm)
{
test_fn = test_fn_parm;
already_invoked = false;
}
bool Invoke(const state_type& state_to_test, std::shared_ptr<result_type>& result_ptr)
{
already_invoked = true;
return test_fn(state_to_test, result_ptr);
}
bool AlreadyInvoked() const {return already_invoked; }
private:
TestFuncDef test_fn;
bool already_invoked;
};
std::shared_ptr<result_type> RunTest(std::list<TestItem>& test_item_list, state_type& state_to_test)
{
for(;;) {
bool made_a_test = false;
for (TestItem& item : test_item_list) {
std::shared_ptr<result_type> result_ptr;
if (!item.AlreadyInvoked()) {
made_a_test = true;
if (item.Invoke(state_to_test, result_ptr)) {
return result_ptr;
}
else
continue;
}
}
if (!made_a_test)
throw appropriate_exception("No conditions were matched");
}
}
This is not supposed to be a full solution to your problem but suggests another way of approaching it. 这不应该是您问题的完整解决方案,但可以提出另一种解决方案。
The important step not documented here is to build up the std::list of TestItems to be passed to RunTest. 此处未记录的重要步骤是建立要传递给RunTest的TestItems的std :: list。 Code to do so might look like this
这样做的代码可能看起来像这样
std::list<TestItem> test_item_list;
test_item_list.push_back(TestItem(ConditionFn1));
test_item_list.push_back(TestItem(ConditionFn2));
The definition of ConditionFn1 might look something like ConditionFn1的定义可能类似于
bool ConditionFn1(const state_type& state_to_test, std::shared_ptr<result_type>& result_ptr)
{
// Do some work
if (....)
return false;
else {
result_ptr.reset(new result_type(some_args));
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.