Advertisement
Guest User

Untitled

a guest
May 31st, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _13.Bit_Shift_Matrix
  5. {
  6.     class CATastrophy
  7.     {
  8.  
  9.         static List<string> CheckDeclarations(string line)
  10.         {
  11.             string[] types = { "sbyte", "byte", "short", "ushort", "int", "uint", "long", "ulong", "float", "double", "decimal", "bool", "char", "string" };
  12.             string[] words = line.Split(new[] { ' ', '(', '?', ')' }, StringSplitOptions.RemoveEmptyEntries);
  13.             //list for the found declarations - if more than one
  14.             //TO-DO >> int a,b,c;
  15.  
  16.             List<string> names = new List<string>();
  17.  
  18.             foreach (var type in types)
  19.             {
  20.                 for (int i = 0; i < words.Length - 1; i++)
  21.                 {
  22.                     if (words[i] == type)
  23.                     {
  24.                         string[] namesFound = words[i + 1].Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
  25.                         foreach (var nameFound in namesFound)
  26.                         {
  27.                             names.Add(nameFound);
  28.                         }
  29.                     }
  30.                 }
  31.             }
  32.             return names;
  33.         }
  34.  
  35.         static void Main()
  36.         {
  37.             int n = int.Parse(Console.ReadLine());
  38.             string[] lines = new string[n];
  39.             for (int i = 0; i < n; i++)
  40.             {
  41.                 lines[i] = Console.ReadLine();
  42.             }
  43.  
  44.             int area = 0; //0 - Methods, 1 - Loops, 2 Conditionals
  45.             List<string> methods = new List<string>();
  46.             List<string> loops = new List<string>();
  47.             List<string> conditionals = new List<string>();
  48.  
  49.             List<List<string>> areaTypes = new List<List<string>>();
  50.             areaTypes.Add(methods);
  51.             areaTypes.Add(loops);
  52.             areaTypes.Add(conditionals);
  53.  
  54.  
  55.             List<string> temp = new List<string>();
  56.  
  57.             List<int> areaPath = new List<int>();
  58.             int brackets = 0;
  59.             bool afterMain = false;
  60.  
  61.             //DEBUGGING
  62.             for (int i = 0; i < n; i++)
  63.             {
  64.                 //if in a method
  65.                 if (lines[i].Contains(" static "))
  66.                 {
  67.                     afterMain = true;
  68.                     area = 0;
  69.                     areaPath.Add(area);
  70.  
  71.                     temp = CheckDeclarations(lines[i]);
  72.                     //Dont add the first word. Its the name of the method// Looks like the last ?!?
  73.                     if (temp.Count > 0)
  74.                     {
  75.                         for (int j = 0; j < temp.Count - 1; j++)
  76.                         {
  77.                             areaTypes[areaPath[areaPath.Count - 1]].Add(temp[j]);
  78.                         }
  79.                     }
  80.                     //skip the next row
  81.                 }
  82.                 else
  83.                 {
  84.                     //if in a Loop
  85.                     if (lines[i].Contains(" for ") || lines[i].Contains(" while ") || lines[i].Contains(" foreach "))
  86.                     {
  87.                         area = 1;
  88.                         areaPath.Add(area);
  89.                         //CHECK FOR DECLARATIONS
  90.                         temp = CheckDeclarations(lines[i]);
  91.                         if (temp.Count > 0)
  92.                         {
  93.                             for (int j = 0; j < temp.Count; j++)
  94.                             {
  95.                                 areaTypes[areaPath[areaPath.Count - 1]].Add(temp[j]);
  96.                             }
  97.                         }
  98.  
  99.                     }
  100.                     //if in a conditional
  101.                     else if (lines[i].Contains(" if ") || lines[i].Contains("else if") || lines[i].Contains("else"))
  102.                     {
  103.                         area = 2;
  104.                         areaPath.Add(area);
  105.                         //CHECK FOR DECLARATIONS
  106.                         temp = CheckDeclarations(lines[i]);
  107.                         if (temp.Count > 0)
  108.                         {
  109.                             for (int j = 0; j < temp.Count; j++)
  110.                             {
  111.                                 areaTypes[areaPath[areaPath.Count - 1]].Add(temp[j]);
  112.                             }
  113.                         }
  114.                     }
  115.                     //just check for
  116.                     else
  117.                     {
  118.                         if (afterMain)
  119.                         {
  120.                             if (lines[i].Contains("}"))
  121.                             {
  122.                                 brackets--;
  123.                                 if (brackets > 0)
  124.                                 {
  125.                                     areaPath.RemoveAt(areaPath.Count - 1);
  126.                                 }
  127.                             }
  128.                             else if (lines[i].Contains("{"))
  129.                             {
  130.                                 brackets++;
  131.                             }
  132.                         }
  133.                        
  134.                         //DECLARATIONS
  135.                         temp = CheckDeclarations(lines[i]);
  136.                         if (temp.Count > 0)
  137.                         {
  138.                             for (int j = 0; j < temp.Count; j++)
  139.                             {
  140.                                 areaTypes[areaPath[areaPath.Count - 1]].Add(temp[j]);
  141.                             }
  142.                         }
  143.                     }
  144.  
  145.                 }
  146.  
  147.             }
  148.  
  149.             string[] areaNames = { "Methods", "Loops ", "Conditional Statements" };
  150.             //PRINT
  151.             for (int i = 0; i < areaTypes.Count; i++)
  152.             {
  153.                 Console.WriteLine("{0} -> {1} -> {2}", areaNames[i], areaTypes[i].Count, string.Join(", ", areaTypes[i].ToArray()));
  154.             }
  155.  
  156.  
  157.         }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement