📜  门| GATE-CS-2015(模拟测试)|问题5(1)

📅  最后修改于: 2023-12-03 15:28:44.290000             🧑  作者: Mango

门 | GATE-CS-2015(模拟测试)|问题5

本题是GATE-CS-2015(模拟测试)考试的第五道题,属于计算机科学领域的数据结构和算法题目。

题目描述

你被要求实现一个门类(Gate),每个门通过一个信号输入(0或1)打开或关闭。如果输入的信号为1,则门打开;如果输入的信号为0,则门关闭。门也有两个输出状态:打开和关闭。

门是通过两个方法 open()close() 控制的。将open()方法调用门的状态设为“打开”,将close()方法调用门的状态设为“关闭”。

门的两个输出状态可以通过 isOpen()isClosed() 方法得到。如果门打开,则 isOpen()返回true, isClosed()返回false;如果门关闭,则 isOpen()返回false, isClosed()返回true。

给定一个初始状态 initalState(0或1),以及多个门的实例数据,实现一个门 LogicalCircuit,该门具有以下属性:

  1. setGate() 方法根据给定的索引值(从1开始)设置门实例对象数组中的门。

  2. performOperation() 方法根据给定的输入信号 signal,模拟门电路的运行,并返回门电路的结果。

示例
Gate inputGate1 = new Gate(initialState);
Gate inputGate2 = new Gate(initialState);
Gate andGate1 = new AndGate(inputGate1, inputGate2);
Gate outputGate = new NotGate(andGate1);
LogicalCircuit circuit = new LogicalCircuit();
circuit.setGate(1, inputGate1);
circuit.setGate(2, inputGate2);
circuit.setGate(3, andGate1);
circuit.setGate(4, outputGate);
System.out.println("初始状态 = " + circuit.performOperation(1)); // false
System.out.println("非门输出状态 = " + circuit.performOperation(0)); // true
inputGate2.SetState(1);
System.out.println("切换后的状态 = " + circuit.performOperation(1)); // true
实现

我们可以定义一个 Gate 门类,包含两个实例变量 openedclosed ,它们分别用于表示门是否打开和关闭;还有两个方法 open()close() ,用于控制门的状态,以及两个方法 isOpen()isClosed() 用于获取门的开关状态。

public class Gate {
    private boolean opened;
    private boolean closed;

    public Gate(boolean initialState) {
        if (initialState) {
            open();
        } else {
            close();
        }
    }

    public void open() {
        opened = true;
        closed = false;
    }

    public void close() {
        opened = false;
        closed = true;
    }

    public boolean isOpen() {
        return opened;
    }

    public boolean isClosed() {
        return closed;
    }
}

在此基础上,我们可以进一步继承并实现逻辑门 AndGateOrGateNotGate 等门类,通过组合这些门类实现 LogicalCircuit 类。

public abstract class LogicGate {
    protected Gate inputGate1;
    protected Gate inputGate2;

    public LogicGate(Gate inputGate1, Gate inputGate2) {
        this.inputGate1 = inputGate1;
        this.inputGate2 = inputGate2;
    }

    public abstract boolean performOperation();
}

public class AndGate extends LogicGate {
    public AndGate(Gate inputGate1, Gate inputGate2) {
        super(inputGate1, inputGate2);
    }

    public boolean performOperation() {
        boolean i1 = inputGate1.isOpen();
        boolean i2 = inputGate2.isOpen();
        return i1 && i2;
    }
}

public class OrGate extends LogicGate {
    public OrGate(Gate inputGate1, Gate inputGate2) {
        super(inputGate1, inputGate2);
    }

    public boolean performOperation() {
        boolean i1 = inputGate1.isOpen();
        boolean i2 = inputGate2.isOpen();
        return i1 || i2;
    }
}

public class NotGate extends LogicGate {
    public NotGate(Gate inputGate) {
        super(inputGate, null);
    }

    public boolean performOperation() {
        boolean i = inputGate1.isOpen();
        return !i;
    }
}

public class LogicalCircuit {
    private LogicGate[] circuit;

    public LogicalCircuit() {
        circuit = new LogicGate[4];
    }

    public void setGate(int index, Gate gate) {
        circuit[index - 1] = gate;
    }

    public boolean performOperation(int signal) {
        if (circuit[0] == null || circuit[1] == null || circuit[2] == null || circuit[3] == null) {
            return false;
        }
        circuit[0].inputGate1.SetState(signal);
        boolean a = circuit[2].performOperation();
        circuit[3].inputGate1.SetState(a);
        return circuit[3].performOperation();
    }
}

最后,我们可以使用上述门类和 LogicalCircuit 类模拟门电路的运行,并得到电路的结果。