From 64a56eca2af0ca37b98fdadf15e5ecb75d5484f0 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Mon, 22 Nov 2021 02:36:34 +0200 Subject: [PATCH] feat: add "Lab4.RemoveVowels" --- Lab4.RemoveVowels/Duomenys.txt | 15 +++++ Lab4.RemoveVowels/Lab4.RemoveVowels.csproj | 10 +++ Lab4.RemoveVowels/Program.cs | 18 ++++++ Lab4.RemoveVowels/TaskUtils.cs | 74 ++++++++++++++++++++++ Lab4.sln | 6 ++ 5 files changed, 123 insertions(+) create mode 100644 Lab4.RemoveVowels/Duomenys.txt create mode 100644 Lab4.RemoveVowels/Lab4.RemoveVowels.csproj create mode 100644 Lab4.RemoveVowels/Program.cs create mode 100644 Lab4.RemoveVowels/TaskUtils.cs diff --git a/Lab4.RemoveVowels/Duomenys.txt b/Lab4.RemoveVowels/Duomenys.txt new file mode 100644 index 0000000..4968592 --- /dev/null +++ b/Lab4.RemoveVowels/Duomenys.txt @@ -0,0 +1,15 @@ +Kūčių rytą + + Anksti Kūčių rytą šeimininkė budina savo vyrą: + - Eik greičiau, saulei netekėjus, kur dalgės kabo, ištverk dalges. Dalges +padėk po stogu, o dalgiakočius sudėk svirnan. + Šeimininkė ieško kubilo lanko, kad būt visai apskritas, nepertrūkęs niekur. +Tą lanką neša vištų tvartan, vidury tvarto paguldo. O tada šeimininkė skuba +tvartant prie kodžio, kur būna žirniai supilti. Šeimininkė tuos žirnius semia +negailėdama didžiulį gorčių, kad visos vištos prilestų lig soties. Šeimininkė +pila tuos žirnius tan kubilo lankan, kad nei vienas žirnis nebūt už kubilo +lanko - kad vištos visos dėtų kiaušinius vienan daiktan, nemėtytų kiaušinių. +Berdama žirnius tan lankan, šeimininkė garsiai sako vištom: + - Žiūrėkite, kad nei vieno kiaušinio, nei vieno niekur nepamestut, visus +vienon vieton dėkite! +Na ir visos vištos šeimininkės įsakymą vykdo. diff --git a/Lab4.RemoveVowels/Lab4.RemoveVowels.csproj b/Lab4.RemoveVowels/Lab4.RemoveVowels.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Lab4.RemoveVowels/Lab4.RemoveVowels.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Lab4.RemoveVowels/Program.cs b/Lab4.RemoveVowels/Program.cs new file mode 100644 index 0000000..89b1bda --- /dev/null +++ b/Lab4.RemoveVowels/Program.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; + +namespace Lab4.RemoveVowels +{ + class Program + { + static void Main(string[] args) + { + const string CFd = "Duomenys.txt"; + const string CFr = "Rezultatai.txt"; + const string CFa = "Analize.txt"; + const string vowels = "AEIYOUaeiyouĄąĘęĖėĮįŲųŪū"; + char[] punctuation = {' ', '.', ',', '!', '?', ':', ';', '(', ')', '\t'}; + TaskUtils.Process(CFd, CFr, CFa, punctuation, vowels); + } + } +} diff --git a/Lab4.RemoveVowels/TaskUtils.cs b/Lab4.RemoveVowels/TaskUtils.cs new file mode 100644 index 0000000..0212a92 --- /dev/null +++ b/Lab4.RemoveVowels/TaskUtils.cs @@ -0,0 +1,74 @@ +using System; +using System.Text; +using System.IO; + +namespace Lab4.RemoveVowels +{ + class TaskUtils + { + /** Finds the longest word in the line. + @param line – string of data + @param punctuation – punctuation marks to separate words */ + private static string LongestWord(string line, char[] punctuation) + { + string[] parts = line.Split(punctuation, StringSplitOptions.RemoveEmptyEntries); + string longestWord=""; + foreach (string word in parts) + if (word.Length > longestWord.Length) + longestWord = word; + return longestWord; + } + + /** Removes vowels from the given word. + @param line – word with possible vowels + @param vowels – vowels of the alphabet */ + private static StringBuilder RemoveVowels(string line, string vowels) + { + StringBuilder newLine = new StringBuilder(); + for (int i = 0; i < line.Length; i++) + if (vowels.IndexOf(line[i]) == -1) + newLine.Append(line[i]); + return newLine; + } + + /** Reads file, removes vowels from the longest word, creates files of + results and of information. + @param fin – name of data file + @param fout – name of result file + @param finfo – name of informative file + @param punctuation – punctuation marks to separate words + @param vowels – vowels of the alphabet */ + public static void Process(string fin, string fout, string finfo, char[] punctuation, string vowels) + { + string[] lines = File.ReadAllLines(fin, Encoding.UTF8); + string dashes = new string('-', 38); + using (var writerF = File.CreateText(fout)) + { + using (var writerI = File.CreateText(finfo)) + { + writerI.WriteLine(dashes); + writerI.WriteLine("| Ilgiausias žodis | Pradžia | Ilgis |"); + writerI.WriteLine(dashes); + foreach (string line in lines) + { + if (line.Length > 0) + { + string longestWord = LongestWord(line, punctuation); + string wordNoVowels = RemoveVowels(longestWord, vowels).ToString(); + writerI.WriteLine("| {0,-16} | {1, 7:d} | {2, 5:d} |", + longestWord, line.IndexOf(longestWord), longestWord.Length); + string newLine = line.Replace(longestWord, wordNoVowels); + // The shortest word cannot be replaced this way. + // It can be a part of the other word; solution is 4.5 subsection. + writerF.WriteLine(newLine); + } + else + writerF.WriteLine(line); + } + writerI.WriteLine(dashes); + } + } + } + } +} + diff --git a/Lab4.sln b/Lab4.sln index e4a4e10..70dafb1 100644 --- a/Lab4.sln +++ b/Lab4.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4.FirstEqualLast", "Lab4 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4.AddSurname", "Lab4.AddSurname\Lab4.AddSurname.csproj", "{F7EFA186-AD5D-4E5D-B405-9ACBAEDB7BF3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4.RemoveVowels", "Lab4.RemoveVowels\Lab4.RemoveVowels.csproj", "{5AD87F42-DD6D-4ADF-B1D5-66553BAA4BCC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -43,6 +45,10 @@ Global {F7EFA186-AD5D-4E5D-B405-9ACBAEDB7BF3}.Debug|Any CPU.Build.0 = Debug|Any CPU {F7EFA186-AD5D-4E5D-B405-9ACBAEDB7BF3}.Release|Any CPU.ActiveCfg = Release|Any CPU {F7EFA186-AD5D-4E5D-B405-9ACBAEDB7BF3}.Release|Any CPU.Build.0 = Release|Any CPU + {5AD87F42-DD6D-4ADF-B1D5-66553BAA4BCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5AD87F42-DD6D-4ADF-B1D5-66553BAA4BCC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5AD87F42-DD6D-4ADF-B1D5-66553BAA4BCC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5AD87F42-DD6D-4ADF-B1D5-66553BAA4BCC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE