using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace vyhodnoceni { // vstup, vystup nebo hradlo public abstract class Node { protected bool? value = null; public virtual bool HasValue() { return value != null; } public virtual bool GetValue() { return this.value.Value; } public void Reset() { value = null; } } // vstup nebo vystup public class ConnectionPoint : Node { protected string id; protected void init(string id) { this.id = id; } } public class Input : ConnectionPoint { protected Output output = null; public Input(string id) { init(id); } // nastavi prislusny vystup, ze ktereho se ziskaji data public void SetOutput(Output output) { this.output = output; } public override bool HasValue() { return this.output.HasValue(); } public override bool GetValue() { return this.output.GetValue(); } public string GetId() { return this.id; } } public class Output : ConnectionPoint { public Output(string id) { init(id); } public void SetValue(bool value) { this.value = value; } } public abstract class Gate : Node { protected List inputs = new List { }; protected List outputs = new List { }; public void Add(Input input) { inputs.Add(input); } public void Add(Output output) { outputs.Add(output); } public bool Calculate() { // kontrola, jestli vsechny vstupy maji hodnotu foreach (var input in inputs) { if (!input.HasValue()) return false; } // vlastni vypocet this.value = CalculateInner(); if (this.value != null) { // jednoduche hradlo (jeden vystup) foreach (var output in outputs) { output.SetValue(this.value.Value); } } else { // vnorene hradlo (vice vystupu) var data = this.CalculateInnerComplex(); for (int i = 0; i < data.Length; i++) { outputs[i].SetValue(data[i]); } this.value = true; } return true; } // vlastni vypocet pro jednoducha hradla (s jednou vystupnii hodnotou) protected abstract bool? CalculateInner(); // vlastni vypocet pro vnorena hradla protected virtual bool[] CalculateInnerComplex() { throw new NotSupportedException(); } } public class AndGate : Gate { protected override bool? CalculateInner() { foreach (var input in inputs) { if (input.GetValue() == false) return false; } return true; } } public class OrGate : Gate { protected override bool? CalculateInner() { foreach (var input in inputs) { if (input.GetValue() == true) return true; } return false; } } public class XorGate : Gate { protected override bool? CalculateInner() { bool value = false; foreach (var input in inputs) { value ^= input.GetValue(); } return value; } } public class NotGate : Gate { protected override bool? CalculateInner() { return !inputs[0].GetValue(); } } public class ComplexGate : Gate { private Network network; public ComplexGate(string netFile) { network = Network.GetNetwork(netFile); } protected override bool? CalculateInner() { return null; } protected override bool[] CalculateInnerComplex() { var data = new bool[inputs.Count]; for (int i = 0; i < inputs.Count; i++) { data[i] = inputs[i].GetValue(); } return network.ProcessData(data); } } }