Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace ConsoleApp1
  5. {
  6.     class Program
  7.     {
  8.         static void PaprastiKomentarai(string fv, string fa)
  9.         {
  10.             string[] lines = File.ReadAllLines(fv);
  11.             using (var fr = File.CreateText(fa))
  12.             {
  13.                 foreach (string line in lines)
  14.                 {
  15.                     string nauja = line;
  16.                     for (int i = 0; i < line.Length - 1; i++)
  17.                     {
  18.                         if (line[i] == '/' && line[i + 1] == '/')
  19.                         {
  20.                             nauja = line.Remove(i);
  21.                         }
  22.                     }
  23.  
  24.                     if(nauja.Trim() != "") fr.WriteLine(nauja);
  25.                 }
  26.             }
  27.         }
  28.  
  29.         static void SudetingiKomentarai(string fv, string fd)
  30.         {
  31.             string text = File.ReadAllText(fv);
  32.             using (var fr = File.CreateText(fd))
  33.             {
  34.                 bool start = false;
  35.                 bool end = false;
  36.                 int k = 0;
  37.                 bool count = false;
  38.  
  39.                 bool comment = false;
  40.  
  41.                 int index1 = -1;
  42.  
  43.                 for (int i = 1; i < text.Length; i++)
  44.                 {
  45.                     if (text[i-1] == '"') comment = true;
  46.                     if (text[i] == '"' && comment) comment = false;
  47.  
  48.                     end = false;
  49.  
  50.                     if (text[i-1] == '/' && text[i] == '*' && !start && !comment)
  51.                     {
  52.                         index1 = i - 1;
  53.                         start = true;
  54.                         count = true;
  55.                     }
  56.  
  57.                     if (count) k++;
  58.  
  59.                     if (text[i - 1] == '*' && text[i] == '/' && !comment)
  60.                     {
  61.                         end = true;
  62.  
  63.                         text = text.Remove(index1, k+1);
  64.                         count = false;
  65.                         k = 0;
  66.                         start = false;
  67.                         i = 0;
  68.                     }
  69.                 }
  70.  
  71.                 if (start && !end)
  72.                 {
  73.                     k = text.Length - index1;
  74.                     text = text.Remove(index1, k);
  75.                 }
  76.  
  77.                 fr.Write(text);
  78.             }
  79.         }
  80.  
  81.         static void Main(string[] args)
  82.         {
  83.             const string CFd = "Duomenys.txt";
  84.             const string CFr = "Rezultatai.txt";
  85.             const string CFa = "Analize.txt";
  86.  
  87.             PaprastiKomentarai(CFd, CFa);
  88.             SudetingiKomentarai(CFa, CFr);
  89.  
  90.             Console.WriteLine("Finished");
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement