Ejemplo n.º 1
0
        // создание массива для вывода на форму Майерса
        private CodeToGraph[] DoCodeGraph(string text)
        {
            Console.WriteLine(text);
            Console.WriteLine("============================================================");
            CodeToGraph[] codeArray = new CodeToGraph[1];
            int countBrackets = 0;
            int count = 0;

            string currentCode = "";
            for (int i = 0; i < text.Length; i++)
            {
                if (text[i] == '{')
                {
                    codeArray[count].color = countBrackets;
                    codeArray[count].code = currentCode;
                    codeArray[count].input = new List<int>();
                    codeArray[count].output = new List<int>();
                    Array.Resize(ref codeArray, ++count + 1);
                    countBrackets++;
                    currentCode = "";
                    continue;
                }
                else
                {
                    if (text[i] == '}')
                    {
                        codeArray[count].color = countBrackets;
                        codeArray[count].code = currentCode;
                        codeArray[count].input = new List<int>();
                        codeArray[count].output = new List<int>();
                        Array.Resize(ref codeArray, ++count + 1);
                        countBrackets--;
                        currentCode = "";
                        continue;
                    }
                    else
                    {
                        currentCode += text[i];
                    }
                }
            }
            codeArray[count].color = countBrackets;
            codeArray[count].code = "";
            codeArray[count].input = new List<int>();
            codeArray[count].output = new List<int>();

            codeToGraphMas = codeArray;

            // сохраняем связи между вершинами
            for (int i = 0; i < codeArray.Length-1; i++)
            {
                if (!codeArray[i].output.Contains(i + 1))
                {
                    codeArray[i].output.Add(i + 1);
                }
                if (!codeArray[i].output.Contains(NextCodeNode(i, codeArray[i].color)) && (i!=0))
                {
                    codeArray[i].output.Add(NextCodeNode(i, codeArray[i].color));
                }
                if (Regex.IsMatch(codeArray[i].code, @"\s*(while|for|foreach|do)\s*"))
                {
                    if (!codeArray[i].input.Contains(i + 1))
                    {
                        codeArray[i].input.Add(i + 1);
                        codeArray[i + 1].output.Clear();
                    }
                }
                if (Regex.IsMatch(codeArray[i].code, @"\s*else\s*"))
                {
                    codeArray[i - 1].output.Remove(i);
                    if (!codeArray[i-1].output.Contains(NextCodeNode(i, codeArray[i].color)))
                    {
                        codeArray[i-1].output.Add(NextCodeNode(i, codeArray[i].color));
                    }
                }
                if (Regex.IsMatch(codeArray[i].code, @"\s*return\s*"))
                {
                    if (!codeArray[i].output.Contains(count))
                    {
                        codeArray[i].output.Add(count);
                    }
                }
            }

            // делаем симметрию
            for (int i = 0; i < codeArray.Length - 1; i++)
            {
                foreach (int c in codeArray[i].input)
                {
                    if (!codeArray[c].output.Contains(i))
                    {
                        codeArray[c].output.Add(i);
                    }
                }
                foreach (int c in codeArray[i].output)
                {
                    if (!codeArray[c].input.Contains(i))
                    {
                        codeArray[c].input.Add(i);
                    }
                }
            }

            // убираем обратные связи для последней вершины
            codeArray[codeArray.Length - 1].output.Clear();

            // отладочная печать
            /*for (int i = 0; i < codeArray.Length - 1; i++)
            {
                Console.WriteLine("{0}======{1}=======", i, codeArray[i].color);
                foreach (int c in codeArray[i].output)
                {
                    Console.Write("{0}   ", c);
                }
                Console.WriteLine(codeArray[i].code);
            }*/

            // считаем число маккейба
            int sum = 2;
            for (int i = 0; i < codeArray.Length - 1; i++)
            {
                sum += codeArray[i].output.Count - 1;
                if (Regex.IsMatch(codeArray[i].code, @"\s*(case|default)\s*"))
                {
                    sum += Regex.Matches(codeArray[i].code, @"\s*(case|default)\s*").Count;
                }
            }
            makkeibuMetric = sum;

            return codeArray;
        }
Ejemplo n.º 2
0
        // создание массива для вывода на форму Майерса
        private CodeToGraph[] DoCodeGraph(string text)
        {
            Console.WriteLine(text);
            Console.WriteLine("============================================================");
            CodeToGraph[] codeArray     = new CodeToGraph[1];
            int           countBrackets = 0;
            int           count         = 0;

            string currentCode = "";

            for (int i = 0; i < text.Length; i++)
            {
                if (text[i] == '{')
                {
                    codeArray[count].color  = countBrackets;
                    codeArray[count].code   = currentCode;
                    codeArray[count].input  = new List <int>();
                    codeArray[count].output = new List <int>();
                    Array.Resize(ref codeArray, ++count + 1);
                    countBrackets++;
                    currentCode = "";
                    continue;
                }
                else
                {
                    if (text[i] == '}')
                    {
                        codeArray[count].color  = countBrackets;
                        codeArray[count].code   = currentCode;
                        codeArray[count].input  = new List <int>();
                        codeArray[count].output = new List <int>();
                        Array.Resize(ref codeArray, ++count + 1);
                        countBrackets--;
                        currentCode = "";
                        continue;
                    }
                    else
                    {
                        currentCode += text[i];
                    }
                }
            }
            codeArray[count].color  = countBrackets;
            codeArray[count].code   = "";
            codeArray[count].input  = new List <int>();
            codeArray[count].output = new List <int>();

            codeToGraphMas = codeArray;

            // сохраняем связи между вершинами
            for (int i = 0; i < codeArray.Length - 1; i++)
            {
                if (!codeArray[i].output.Contains(i + 1))
                {
                    codeArray[i].output.Add(i + 1);
                }
                if (!codeArray[i].output.Contains(NextCodeNode(i, codeArray[i].color)) && (i != 0))
                {
                    codeArray[i].output.Add(NextCodeNode(i, codeArray[i].color));
                }
                if (Regex.IsMatch(codeArray[i].code, @"\s*(while|for|foreach|do)\s*"))
                {
                    if (!codeArray[i].input.Contains(i + 1))
                    {
                        codeArray[i].input.Add(i + 1);
                        codeArray[i + 1].output.Clear();
                    }
                }
                if (Regex.IsMatch(codeArray[i].code, @"\s*else\s*"))
                {
                    codeArray[i - 1].output.Remove(i);
                    if (!codeArray[i - 1].output.Contains(NextCodeNode(i, codeArray[i].color)))
                    {
                        codeArray[i - 1].output.Add(NextCodeNode(i, codeArray[i].color));
                    }
                }
                if (Regex.IsMatch(codeArray[i].code, @"\s*return\s*"))
                {
                    if (!codeArray[i].output.Contains(count))
                    {
                        codeArray[i].output.Add(count);
                    }
                }
            }

            // делаем симметрию
            for (int i = 0; i < codeArray.Length - 1; i++)
            {
                foreach (int c in codeArray[i].input)
                {
                    if (!codeArray[c].output.Contains(i))
                    {
                        codeArray[c].output.Add(i);
                    }
                }
                foreach (int c in codeArray[i].output)
                {
                    if (!codeArray[c].input.Contains(i))
                    {
                        codeArray[c].input.Add(i);
                    }
                }
            }

            // убираем обратные связи для последней вершины
            codeArray[codeArray.Length - 1].output.Clear();

            // отладочная печать

            /*for (int i = 0; i < codeArray.Length - 1; i++)
             * {
             *      Console.WriteLine("{0}======{1}=======", i, codeArray[i].color);
             *      foreach (int c in codeArray[i].output)
             *      {
             *              Console.Write("{0}   ", c);
             *      }
             *      Console.WriteLine(codeArray[i].code);
             * }*/

            // считаем число маккейба
            int sum = 2;

            for (int i = 0; i < codeArray.Length - 1; i++)
            {
                sum += codeArray[i].output.Count - 1;
                if (Regex.IsMatch(codeArray[i].code, @"\s*(case|default)\s*"))
                {
                    sum += Regex.Matches(codeArray[i].code, @"\s*(case|default)\s*").Count;
                }
            }
            makkeibuMetric = sum;

            return(codeArray);
        }