繁体   English   中英

真值表的最佳实现

[英]Optimal implementation of truth table

我已经确定了一个真值表,例如下面的一个

prev_state| input1         | input2 |next_state| Action
(def/new) |(Disable/Enable)|(Off/On)|          |
def       | D              | Off    | def      | Nothing
def       | D              | On     | def      | Nothing
def       | E              | Off    | def      | Nothing
def       | E              | On     | new      | call function1
new       | D              | Off    | def      | call function2
new       | D              | On     | def      | call function2
new       | E              | Off    | def      | call function2
new       | E              | On     | new      | Nothing

我想知道实现此目标所需的最少检查数量是多少。

我是否想使用卡诺地图,例如以下地图

    00| 01| 11| 10 
  -----------------
0 | A | A | B | A |  
  -----------------
1 | C | C | A | C |  
  -----------------

其中A对应于无,B代表调用function1,C代表调用function2

根据我所看到的,您有2个A的2个组合和一个A总共3个A的B和2个2 C的组合

这是否意味着比较的最小次数为3 + 1 + 2 = 6? 但是由于A不执行任何操作,因此最小实现只需要B和C的3种组合?

测试实施

if (prev_state == new && input1 == disable) {
    function2();
}
else if (prev_state == new && input2 == Off) {
    function2();
}
else if (input1 == enable && input2 == On) {
    function1();
}

同样,现在我看到上面或这个更好的一个:

if ((prev_state == new && input1 == disable) || (prev_state == new && input2 == Off)) {
    function2();
}
else if (input1 == enable && input2 == On) {
    function1();
}

感谢您提出的查找表O(1)但占用内存空间的建议。 我现在意识到,我希望有一种不使用额外内存的解决方案。 您是否同意使用Karnaugh映射是得出最小比较量的有效方法?

我想知道您需要达到的最低检查数量是多少...

零。 使用查询表

void f_Nothing(void) {
  ; // do nothing
}

void f_function1(void) {
  ;  // something interesting
}

void f_function2(void) {
  ;  // something interesting
}

int main(void) {

  typedef void (*fun_type)(void);

  fun_type f[2][2][2] = { //
      {{f_Nothing, f_Nothing}, {f_Nothing, f_function1}}, 
      {{f_function2, f_function2}, {f_function2, f_Nothing}}};
  bool prev_state, input1, input2;
  //...
  f[prev_state][input1][input2]();

OP后来评论寻找一种解决方案... ...不再使用额外的内存

if ( (input1 == E && input2 == ON) && (prev_state == def)) function1();
if (!(input1 == E && input2 == ON) && (prev_state == new)) function2();

// or

if (input1 == E && input2 == ON) {
  if (prev_state == def) function1();
} else {
  if (prev_state == new) function2();
}

如果行为是静态的,则无法进行测试,您可以

  • 使用3维数组,其中每个值是一对下一个状态和动作,第一个维是prev_state 0/1,第二个输入1 D / E-> 0/1,第三个输入2 off / on-> 0/1

  • 但是由于您的输入非常有限,因此您也可以只对3个索引进行编码= prev_state * 4 + input1 * 2 + input2并使用大小为8的简单向量。根据prev_state * 4 + input1 * 2 + input2的结果prev_state * 4 + input1 * 2 + input2

您可以删除一些重复的测试,但是在实践中是否有很大不同取决于编译器的优化。

if (prev_state == new) {
    if (input1 == disable || input2 == Off) {
        function2();
    }
} else {
    if (input1 == enable && input2 == On) {
        function1();
    }
}

要么:

if (input1 == disable || input2 == Off) {
    if (prev_state == new) {
        function2();
    }
} else {
    if (prev_state == def) {
        function1();
    }
}

我会做下面的事情。

int check = (int)((prev_state == new) << 2 | (input1 == E)<<1 | (input2 == on));

/*def       | E              | On     | new      | call function1 == 3
  new       | D              | Off    | def      | call function2 == 4
  new       | D              | On     | def      | call function2 == 5
  new       | E              | Off    | def      | call function2 == 6 */

if (check == 4 || check == 5 || check == 6)
  function2();
else if (check == 3)
  function1();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM