【问题标题】:Data model for logic gates simulation逻辑门模拟的数据模型
【发布时间】:2012-05-26 23:58:00
【问题描述】:

我正在尝试为逻辑门开发一个模拟器。模拟器需要计算给定电路的真值表。

这是一个示例电路。 a,b,c,d,e 是输入,z 是输出。

我对编程很陌生。我找不到为大门建模的方法。你能给我一个方法吗?

【问题讨论】:

  • 这对于 Stack Overflow 问题来说太宽泛了。但您可能对directed graphs 很感兴趣。
  • @OliCharlesworth:在没有反馈链/循环的简单情况下,常规树可以很好地工作。

标签: data-structures logic


【解决方案1】:

对于每个输入,您需要为状态 (true,false) 生成真值表。因此,如果您有 5 个输入 = 总组合 = 2^5。

你没有指定你需要什么语言。所以,我会给你一个流畅的Java 方法。

我假设您已经为不同的门定义了所有特定功能,例如XNORANDOR 等。例如,您可以将XNOR 门的功能设置为:boolean XNOR(boolean ip1, boolean ip2)

现在该过程简化为为输入生成所有组合 (2^5)。这简化为一个简单的排列问题 - 你可以这样做:(想法是将值从数组的末尾更改为开头。因为它只需要两个值,所以很容易实现)

//inputs  - all initialized to FALSE; - ready for 1st case of (2^5)
//Let the inputs a,b,c,d,e correspond to values of this array
boolean inp[]=new boolean[5]; 

//need a pointer variable for the array
//first pointing to the last-1 element of the array
int main_col=inp.length-2; 

//Generate the combinations for input from all FALSE to until you reach all inputs to TRUE values
boolean looptf=true;
while(looptf){
   call_Appropriate_gates_from_inputs(inp); 
   inp[inp.length-1]=!inp[inp.length-1]; //last array element value changed 
   call_Appropriate_gates_from_inputs(inp);
   inp[inp.length-1]=!inp[inp.length-1]; //reset
   for(int i=inp.length-2;i>=0;i--){
        if (inp[i]){
          inp[i]=false; 
          if (main_col==i){
             main_col--;
             if (main_col<0){
               looptf=false;
               break;
             }
          }           
        }else{
          inp[i]=true;
          break;
        }
    }//for        
}//while

现在你可以定义方法call_Appropriate_gates_from_inputs(boolean[])并执行门逻辑并得到结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    相关资源
    最近更新 更多