真值表的最佳实现

时间:2019-01-08 17:21:17

标签: c algorithm truthtable karnaugh-map

我已经确定了一个真值表,如下图所示

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

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

我是否想使用Karnaugh map,例如以下内容:

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

其中A代表什么都不是,B代表function1,C代表function2

根据我的观察,您有2个A的2种组合,而A总共3种 B的1 和2个C的2种组合

这是否意味着比较的最小次数为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)但占用内存空间的查找表的人。 我现在意识到,我希望有一种不使用额外内存的解决方案。您是否同意使用卡诺地图是推导最小比较量的有效方法?

4 个答案:

答案 0 :(得分:7)

  

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

零。使用查询表

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随后评论了looking for a solution that ... doesn't use further extra memory

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();
}

答案 1 :(得分:2)

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

  • 使用3维数组,其中每个值是夫妇下一个状态和动作,第一个维是prev_state 0/1,第二个输入1 D / E-> 0/1,第三个输入2关闭/打开-> 0/1

  • 但是由于您的输入非常有限,因此您也可以仅将3个索引编码为一个= prev_state * 4 + input1 * 2 + input2,并使用大小为8的简单向量。正如Schwern在评论中建议的那样,您也可以进行切换/ case prev_state * 4 + input1 * 2 + input2

  • 的结果

答案 2 :(得分:1)

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

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();
    }
}

答案 3 :(得分:0)

我会做下面的事情。

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();