示例#1
0
        public static Circuit BuildCircuit(string inputText)
        {
            Regex regexCapture = new Regex(@"(\d+)?(\w)(\d+)?(\w)0#(\d+)?(\w)(\d+)?(\w)");

            var rawList = inputText.Split('\n').ToList <string>();

            // filter blank lines and comments
            var gateList = rawList.Where(x => x.Trim().Length > 0 && !x.StartsWith("#")).ToList();

            // remove inputsteam/outputstream lines
            gateList.RemoveAt(0);
            gateList.RemoveAt(gateList.Count - 1);

            Circuit c = new Circuit(gateList.Count);

            for (int i = 0; i < gateList.Count; i++)
            {
                c.AddGate();
            }

            for (int i = 0; i < gateList.Count; i++)
            {
                MatchCollection mc = regexCapture.Matches(gateList[i]);
                if (mc[0].Groups[2].Value == "X")
                {
                    c.Gates[i].InputL.ConnectTo(c.InputStream);
                }
                else if (mc[0].Groups[2].Value == "L")
                {
                    c.Gates[i].InputL.ConnectTo(c.Gates[Int32.Parse(mc[0].Groups[1].Value)].OutputL);
                }
                else if (mc[0].Groups[2].Value == "R")
                {
                    c.Gates[i].InputL.ConnectTo(c.Gates[Int32.Parse(mc[0].Groups[1].Value)].OutputR);
                }

                if (mc[0].Groups[4].Value == "X")
                {
                    c.Gates[i].InputR.ConnectTo(c.InputStream);
                }
                else if (mc[0].Groups[4].Value == "L")
                {
                    c.Gates[i].InputR.ConnectTo(c.Gates[Int32.Parse(mc[0].Groups[3].Value)].OutputL);
                }
                else if (mc[0].Groups[4].Value == "R")
                {
                    c.Gates[i].InputR.ConnectTo(c.Gates[Int32.Parse(mc[0].Groups[3].Value)].OutputR);
                }

                if (mc[0].Groups[6].Value == "X")
                {
                    c.Gates[i].OutputL.ConnectTo(c.OutputStream);
                }
                else if (mc[0].Groups[6].Value == "L")
                {
                    c.Gates[i].OutputL.ConnectTo(c.Gates[Int32.Parse(mc[0].Groups[5].Value)].InputL);
                }
                else if (mc[0].Groups[6].Value == "R")
                {
                    c.Gates[i].OutputL.ConnectTo(c.Gates[Int32.Parse(mc[0].Groups[5].Value)].InputR);
                }

                if (mc[0].Groups[8].Value == "X")
                {
                    c.Gates[i].OutputR.ConnectTo(c.OutputStream);
                }
                else if (mc[0].Groups[8].Value == "L")
                {
                    c.Gates[i].OutputR.ConnectTo(c.Gates[Int32.Parse(mc[0].Groups[7].Value)].InputL);
                }
                else if (mc[0].Groups[8].Value == "R")
                {
                    c.Gates[i].OutputR.ConnectTo(c.Gates[Int32.Parse(mc[0].Groups[7].Value)].InputR);
                }
            }

            return(c);
        }