Advertisement
vlad0

Sample Exam CleanCode

Feb 3rd, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _01.CleanCode
  7. {
  8.     class Program
  9.     {
  10.         static bool isQuoted = false;
  11.         static bool isComment = false;
  12.         static bool isLongComment = false;
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             int lengthLines = int.Parse(Console.ReadLine());
  17.             string[] lines = new string[lengthLines];
  18.             string[] editedLines = new string[lengthLines];
  19.  
  20.             for (int i = 0; i < lengthLines; i++)
  21.             {
  22.                 lines[i] = Console.ReadLine();
  23.             }
  24.             for (int i = 0; i < lengthLines; i++)
  25.             {
  26.                 editedLines[i] = RemoveComments(lines[i]);
  27.                 if (isLongComment == true)
  28.                 {
  29.                     int start = i;
  30.                     while (isLongComment == true)
  31.                     {
  32.                         editedLines[i] += RemoveComments(lines[start]);
  33.                         start++;
  34.                     }
  35.                     i = start - 1;
  36.                 }
  37.             }
  38.             for (int i = 0; i < editedLines.Length; i++)
  39.             {
  40.                 if (editedLines[i] != null && editedLines[i].Trim().Length != 0)
  41.                 {
  42.                     Console.WriteLine(editedLines[i]);
  43.                 }
  44.             }
  45.         }
  46.  
  47.         private static string RemoveComments(string lines)
  48.         {
  49.             List<char> editedLine = new List<char>();
  50.             int length = lines.Length;
  51.             for (int i = 0; i < length; i++)
  52.             {
  53.                 if (isQuoted == true)
  54.                 {
  55.                     //string str2 = "/*no\"oo\\oo*/";
  56.                     while (i < length && lines[i] != '"')
  57.                     {
  58.  
  59.                         editedLine.Add(lines[i]);
  60.                         if (lines[i] == '\\' && i + 1 < length && lines[i + 1] == '"')
  61.                         {
  62.                             editedLine.Add(lines[i + 1]);
  63.                             i++;
  64.                         }
  65.                         i++;
  66.                     }
  67.                     if (i < length && lines[i] == '"')
  68.                     {
  69.                         editedLine.Add(lines[i]);
  70.                         isQuoted = false;
  71.                     }
  72.                 }
  73.                 else if (isLongComment == true)
  74.                 {
  75.                     int index = lines.IndexOf("*/");
  76.                     if (index == -1)
  77.                     {
  78.                         break;
  79.                     }
  80.                     else
  81.                     {
  82.                         i = index + 1;
  83.                         isLongComment = false;
  84.                     }
  85.                 }
  86.                 else if (lines[i] == '"')
  87.                 {
  88.                     if (i-1>0 && lines[i-1] != '\\')
  89.                     {
  90.                         isQuoted = true;    
  91.                     }
  92.                    
  93.                     editedLine.Add(lines[i]);
  94.                 }
  95.                 else if (lines[i] == '/')
  96.                 {
  97.                     if (i + 1 < length && lines[i + 1] == '/')
  98.                     {
  99.                         if (i + 2 < length &&  lines[i + 2] == '/')
  100.                         {
  101.                             editedLine.Add(lines[i]);
  102.                             editedLine.Add(lines[i]);
  103.                             editedLine.Add(lines[i]);
  104.                             i=i+2;
  105.                         }
  106.                         else
  107.                         {
  108.                             break;
  109.                         }
  110.                     }
  111.                     else if (i + 1 < length && lines[i + 1] == '*')
  112.                     {
  113.                         isLongComment = true;
  114.                     }
  115.                     else
  116.                     {
  117.                         editedLine.Add(lines[i]);
  118.                     }
  119.  
  120.                 }
  121.                 else
  122.                 {
  123.                     editedLine.Add(lines[i]);
  124.                 }
  125.             }
  126.  
  127.             StringBuilder result = new StringBuilder();
  128.             for (int i = 0; i < editedLine.Count; i++)
  129.             {
  130.                 result.Append(editedLine[i]);
  131.             }
  132.  
  133.             return result.ToString();
  134.         }
  135.  
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement