Esempio n. 1
0
        public void TestMethod1()
        {
            State q0 = new State("q0");
            State q1 = new State("q1");
            State q2 = new State("q2");

            q0.AddLink(new Link() { symbol = "0", newState = q0 });
            q0.AddLink(new Link() { symbol = "1", newState = q0 });
            q0.AddLink(new Link() { symbol = "0", newState = q1 });
            q1.AddLink(new Link() { symbol = "0", newState = q2 });

            List<State> endStates = new List<State>() { q2 };

            List<State> listStates = new List<State>() { q0, q1, q2 };
            string str = "100100";

            Automation a = new Automation(q0, endStates, listStates);
            bool results = Automation.Check(str, a);

            Assert.AreEqual(true, results);
        }
Esempio n. 2
0
        /// <summary>
        /// Метод проверки: принимается строка автоматом или нет
        /// </summary>
        /// <param name="str"> строка в виде листа символов </param>
        /// <param name="automation"> автомат </param>
        /// <returns> true - строка принимается автоматом. false - строка не принимаетс автоматом </returns>
        public static bool Check(string _str, Automation automation)
        {
            List<string> str = new List<string>();
            for (int i = 0; i < _str.Length; i++)
            {
                str.Add(_str[i].ToString());
            }
                if (str.Count == 0)
                    return false;
            List<State> currentState = new List<State>();
            List<State> Help = new List<State>();
            currentState.Add(automation.startState);
            str.Add("");
            for (int i = 0; i < str.Count; i++)
            {
                for (int j = 0; j < currentState.Count; j++)
                {
                    for (int k = 0; k < automation.states.Count; k++)
                    {
                        if (automation.states[k].nameState == currentState[j].nameState)
                            foreach (var element in automation.states[k].links)
                                if (element.symbol == str[i])
                                    Help.Add(element.newState);
                    }
                }
                if (Help.Count == 0)
                    return false;
                currentState = new List<State>();
                for (int j = 0; j < Help.Count; j++)
                    currentState.Add(Help[j]);
                Help = new List<State>();

            }
            for (int i = 0; i < currentState.Count; i++)
                if (currentState[i].nameState == automation.finishState.nameState)
                    return true;
            return false;
        }
Esempio n. 3
0
        public void TestMethod3()
        {
            State q0 = new State("q0");
            State q1 = new State("q1");
            State q2 = new State("q2");
            State q3 = new State("q3");
            State q4 = new State("q4");
            State q5 = new State("q5");
            State q6 = new State("q6");
            State q7 = new State("q7");
            State q8 = new State("q8");

            q0.AddLink(new Link() { symbol = "0", newState = q1 });
            q0.AddLink(new Link() { symbol = "1", newState = q2 });
            q1.AddLink(new Link() { symbol = "0", newState = q2 });
            q1.AddLink(new Link() { symbol = "1", newState = q2 });
            q1.AddLink(new Link() { symbol = "0", newState = q4 });
            q2.AddLink(new Link() { symbol = "0", newState = q2 });
            q2.AddLink(new Link() { symbol = "1", newState = q8 });
            q3.AddLink(new Link() { symbol = "0", newState = q5 });
            q4.AddLink(new Link() { symbol = "0", newState = q6 });
            q4.AddLink(new Link() { symbol = "1", newState = q3 });
            q5.AddLink(new Link() { symbol = "0", newState = q2 });
            q5.AddLink(new Link() { symbol = "1", newState = q7 });
            q6.AddLink(new Link() { symbol = "0", newState = q6 });
            q6.AddLink(new Link() { symbol = "1", newState = q4 });
            q7.AddLink(new Link() { symbol = "0", newState = q8 });

            List<State> endStates = new List<State>() { q2, q6, q8 };
            List<State> listStates = new List<State>() { q0, q1, q2, q3, q4, q5, q6, q7, q8 };

            Automation a = new Automation(q0, endStates, listStates);

            string str = "1100010100011";

            bool results = Automation.Check(str, a);

            Assert.AreEqual(false, results);
        }