JAVA中的门NAND

时间:2015-04-11 00:55:05

标签: java

这次我有NAND GateNOR Gate,其中我每次只有一次测试失败。在输入信号为NAND时,假设0X (LOW and UNKNOWN SIGNAL)输出应为1,但我得X。在NOR中,当我混合使用1(HI信号)和X(未知信号)时,答案应为0,但我得到X我只能失败一个同时涉及0X的测试用例,以及通过NAN输出的测试用例应为1。在我检查Signal.X的方法中,boolean变为true,但它是否会从循环中断开?

  Table NAND for reference.(NOTE:table does not contain X)

         Input A    Input B     Output Q
              0       0           1
              0       1           1
              1       0           1
              1       1           0

    Table NOR for reference.(NOTE:table does not contain X)

Input A     Input B     Output Q
    0          0           1
    0          1           0
    1          0           0
    1          1           0




//in NAND propagate when the inputs are 0X or 000X0X the output should be 1, Im //getting X
 public boolean propagate() //NAND method
        {
            Signal inputSignal;
            Signal temp = getOutput().getSignal();
            List<Wire> inputs = getInputs();
            boolean hasX = false;
            int countLO = 0;
            int countHI = 0;

            for(Wire wire: inputs)
            {
                inputSignal = wire.getSignal();

                if(inputSignal == Signal.X)
                    hasX = true;

                else if(inputSignal == Signal.LO)
                    countLO++;

                else if(inputSignal == Signal.HI)
                    countHI++;
            }

            if(hasX)
                getOutput().setSignal(Signal.X);

            else if(countLO == inputs.size())
                getOutput().setSignal(Signal.HI);

            else if(countHI == inputs.size())
                getOutput().setSignal(Signal.LO);

            else
                getOutput().setSignal(Signal.HI);

.................................


//in NOR propagate when the inputs are 1X1X or 1X the output should be X, Im //getting X

public boolean propagate() //NOR method
    {
        Signal inputSignal;
        int countLO = 0;
        boolean hasX = false;
        List<Wire> inputs = getInputs();
        Signal temp = getOutput().getSignal();

        for(Wire wire: inputs)
        {
            inputSignal = wire.getSignal();

            if(inputSignal == Signal.X)
            {
                hasX = true;
                break;
            }

            else if(inputSignal == Signal.LO)
                countLO++;
        }

        if(hasX)
            getOutput().setSignal(Signal.X);

        else if(countLO != inputs.size())
            getOutput().setSignal(Signal.LO);

        else getOutput().setSignal(Signal.HI);

1 个答案:

答案 0 :(得分:0)

您的表格应包含X,以便我们可以看到该程序应该执行的操作。无论如何,问题似乎在if(hasX) getOutput().setSignal(Signal.X);行。由于它是嵌套if语句中的第一行,因此优先于其他任何内容。因此,如果输入中有X,则会调用getOutput().setSignal(Signal.X)