From 30a3aede9031fd50b578accc04caa0a7a1bed5a7 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sun, 21 Nov 2021 01:14:29 +0200 Subject: [PATCH] feat: add Lab4.RemoveComments --- Lab4.RemoveComments/Duomenys.txt | 18 ++++++ Lab4.RemoveComments/InOut.cs | 60 +++++++++++++++++ .../Lab4.RemoveComments.csproj | 14 ++++ Lab4.RemoveComments/Program.cs | 15 +++++ Lab4.RemoveComments/TaskUtils.cs | 64 +++++++++++++++++++ Lab4.sln | 2 + 6 files changed, 173 insertions(+) create mode 100644 Lab4.RemoveComments/Duomenys.txt create mode 100644 Lab4.RemoveComments/InOut.cs create mode 100644 Lab4.RemoveComments/Lab4.RemoveComments.csproj create mode 100644 Lab4.RemoveComments/Program.cs create mode 100644 Lab4.RemoveComments/TaskUtils.cs diff --git a/Lab4.RemoveComments/Duomenys.txt b/Lab4.RemoveComments/Duomenys.txt new file mode 100644 index 0000000..c44a7de --- /dev/null +++ b/Lab4.RemoveComments/Duomenys.txt @@ -0,0 +1,18 @@ +void DuomenysInternet(Grybai & grybai) +{ + ifstream fd(u2); +// string pav, tip; +// GrybasInfo s1; + int ns = 0; + bool yra = true; + while(!fd.eof() && yra) { // kol yra duomenų ir jie telpa į masyvą + fd >> pav >> tip; + s1.Dėti (pav, tip); + if(!fd.eof() && (ns - 1 < Grybai::CMax ) ) + grybai[ns++] = s1; // įrašo naują elementą + else + yra = false; + } + fd.close(); + grybai.Dėti(ns); +} \ No newline at end of file diff --git a/Lab4.RemoveComments/InOut.cs b/Lab4.RemoveComments/InOut.cs new file mode 100644 index 0000000..b0e7236 --- /dev/null +++ b/Lab4.RemoveComments/InOut.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Lab4.RemoveComments +{ + class InOut + { + public static void Process(string fin, string fout, string finfo) + { + string[] lines = File.ReadAllLines(fin, Encoding.UTF8); + string[] cleanedLines = TaskUtils.RemoveComments(lines); + + File.WriteAllLines(fout, cleanedLines); + + using (var writer = File.CreateText(finfo)) + { + // Check line by line which differ + int j = 0; // Used for cleaned lines + for (int i = 0; i < lines.Length && j < cleanedLines.Length; i++) + { + Console.WriteLine("{0} ============ {1}", lines[i], cleanedLines[j]); + if (lines[i] != cleanedLines[j]) + { + writer.WriteLine(lines[i]); + } + else + { + j++; + } + } + } + + /* + using (var writerF = File.CreateText(fout)) + { + using (var writerI = File.CreateText(finfo)) + { + foreach (string line in lines) + { + if (line.Length > 0) + { + string newLine = line; + if (TaskUtils.RemoveComments(line, out newLine)) + writerI.WriteLine(line); + if (newLine.Length > 0) + writerF.WriteLine(newLine); + } + else + { + writerF.WriteLine(line); + } + } + } + } + */ + } + } +} diff --git a/Lab4.RemoveComments/Lab4.RemoveComments.csproj b/Lab4.RemoveComments/Lab4.RemoveComments.csproj new file mode 100644 index 0000000..3a26489 --- /dev/null +++ b/Lab4.RemoveComments/Lab4.RemoveComments.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp2.1 + + + + + PreserveNewest + + + + diff --git a/Lab4.RemoveComments/Program.cs b/Lab4.RemoveComments/Program.cs new file mode 100644 index 0000000..5e8d41e --- /dev/null +++ b/Lab4.RemoveComments/Program.cs @@ -0,0 +1,15 @@ +using System; + +namespace Lab4.RemoveComments +{ + class Program + { + static void Main(string[] args) + { + const string CFd = "Duomenys.txt"; + const string CFr = "Rezultatai.txt"; + const string CFa = "Analize.txt"; + InOut.Process(CFd, CFr, CFa); + } + } +} diff --git a/Lab4.RemoveComments/TaskUtils.cs b/Lab4.RemoveComments/TaskUtils.cs new file mode 100644 index 0000000..d87b69a --- /dev/null +++ b/Lab4.RemoveComments/TaskUtils.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace Lab4.RemoveComments +{ + class TaskUtils + { + /** Removes comments and returns an indication about performed activity. + @param line – line having possible comments + @param newLine – line without comments */ + public static string[] RemoveComments(string[] lines) + { + List cleanedLines = new List(); + + foreach (string line in lines) + { + Match case1 = Regex.Match(line, @"^(.*)//.*$"); + if (case1.Success) + { + if (case1.Groups[1].Value.Length > 0) + { + cleanedLines.Add(case1.Groups[1].Value); + } + } + else + { + cleanedLines.Add(line); + } + } + + /* + Match case1 = Regex.Match(line, @"^(.*)//.*$"); + if (case1.Success) + { + newLine = case1.Groups[1].Value; + return true; + } + */ + + return cleanedLines.ToArray(); + + /* + Match case2 = Regex.Match(line, @"^$"); + if (case2.Success) + { + newLine = case2.Captures[1].Value; + return true; + } + */ + + + /*newLine = line; + for (int i = 0; i < line.Length - 1; i++) + if (line[i] == '/' && line[i + 1] == '/') + { + newLine = line.Remove(i); + return true; + } + return false;*/ + } + } +} diff --git a/Lab4.sln b/Lab4.sln index 50b9c14..ed0ec99 100644 --- a/Lab4.sln +++ b/Lab4.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.28307.1525 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4.RemoveLines", "Lab4.RemoveLines\Lab4.RemoveLines.csproj", "{5309D21D-2A14-4332-90EC-504AD89CE397}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab4.RemoveComments", "Lab4.RemoveComments\Lab4.RemoveComments.csproj", "{4D9B08C9-5735-4C95-8D2A-BDFC888B1E68}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4.LetterFrequency", "Lab4.LetterFrequency\Lab4.LetterFrequency.csproj", "{F1797B5C-1541-4821-9485-7C3520C5DD8D}" EndProject Global