Beispiel #1
0
        public Determ NewMashine()
        {
            Determ result = new Determ();

            List <List <string> > CompositeState = new List <List <string> >();
            HashSet <string>      TmpStates      = new HashSet <string>();

            result.Alphabet            = Alphabet;
            result.InitialStateMachine = InitialStateMachine;
            result.StateMachine.Add(InitialStateMachine);
            var l = new List <string>();

            l.Add(InitialStateMachine);
            CompositeState.Add(l);

            for (int i = 0; i <= result.StateMachine.Count - 1; i++)
            {
                for (int j = 0; j < Alphabet.Length; j++)
                {
                    if (Alphabet[j] != "_")
                    {
                        TmpStates.Clear();
                        for (int k = 0; k < transitions.Count; k++)
                        {
                            if (CompositeState[i].Contains(transitions[k].From) && transitions[k].Symb == Alphabet[j])
                            {
                                TmpStates.Add(transitions[k].To);
                            }
                        }
                        EpsilonTransition(TmpStates);
                        List <string> names = new List <string>(TmpStates);
                        names.Sort();
                        if (names.Count > 0)
                        {
                            string statename = null;
                            for (int t = 0; t < CompositeState.Count; t++)
                            {
                                if (names.SequenceEqual(CompositeState[t]))
                                {
                                    statename = result.StateMachine[t];
                                    break;
                                }
                            }
                            if (statename == null)
                            {
                                statename = string.Join("_", names);
                                result.StateMachine.Add(statename);
                                CompositeState.Add(names);
                                TmpStates.IntersectWith(FinalStateMachine);
                                if (TmpStates.Count > 0)
                                {
                                    result.FinalStateMachine.Add(statename);
                                }
                            }
                            DTransition tr = new DTransition();
                            tr.From = result.StateMachine[i];
                            tr.Symb = Alphabet[j];
                            tr.To   = statename;
                            result.transitions.Add(tr);
                        }
                    }
                }
            }
            return(result);
        }
Beispiel #2
0
        public void Input()
        {
            string path = @"Test 2.txt";

            try
            {
                using (StreamReader sr = new StreamReader(path, System.Text.Encoding.Default))
                {
                    string buffer;
                    string A, Q, Final_Q;

                    A        = sr.ReadLine();
                    Alphabet = A.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);


                    Q = sr.ReadLine();

                    String[] TmpQ = Q.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    for (int i = 0; i < TmpQ.Length; i++)
                    {
                        StateMachine.Add(TmpQ[i]);
                    }

                    InitialStateMachine = sr.ReadLine();

                    Final_Q = sr.ReadLine();
                    String[] TmpF_Q = Final_Q.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    for (int i = 0; i < TmpF_Q.Length; i++)
                    {
                        StateMachine.Add(TmpF_Q[i]);
                    }

                    while ((buffer = sr.ReadLine()) != null)
                    {
                        string[] parts = buffer.Split(new char[] { ' ' }, 3);
                        if (parts.Length == 3)
                        {
                            DTransition transition = new DTransition();
                            transition.From = parts[0];
                            transition.Symb = parts[1];
                            transition.To   = parts[2];

                            transitions.Add(transition);
                        }
                        else
                        {
                            throw new Exception($"Некорректный переход: {buffer}");
                        }
                    }
                }

                Validate();
                Reset();
                Console.WriteLine("Введите строку разделяя символы пробелами");
                string tmp_string = "";
                tmp_string = Console.ReadLine();
                string[] input = tmp_string.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                Mashine(input);

                Reset();
            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.Message);
            }
            Console.ReadKey();
        }