diff --git a/Lab4/.editorconfig b/Lab4/.editorconfig new file mode 100644 index 0000000..3159b2a --- /dev/null +++ b/Lab4/.editorconfig @@ -0,0 +1,14 @@ +root=true + +[*] +charset = utf-8 +end_of_line = lf +indent_style = tab +indent_size = 2 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.cs] +indent_style = space +indent_size = 4 + diff --git a/Lab4/.gitignore b/Lab4/.gitignore new file mode 100644 index 0000000..26ab8f4 --- /dev/null +++ b/Lab4/.gitignore @@ -0,0 +1,388 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.tlog +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Nuget personal access tokens and Credentials +# nuget.config + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp + +# JetBrains Rider +.idea/ +*.sln.iml diff --git a/Lab4/Lab4.AddSurname/Duomenys.txt b/Lab4/Lab4.AddSurname/Duomenys.txt new file mode 100644 index 0000000..e190be7 --- /dev/null +++ b/Lab4/Lab4.AddSurname/Duomenys.txt @@ -0,0 +1,11 @@ +Arvydas (g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas, +olimpinis ir pasaulio čempionas Arvydas, nuo Arvydas +2011 m. spalio 24 d. Lietuvos krepšinio federacijos prezidentas. +Profesionalaus žaidėjo karjerą Arvydas pradėjo 1981 m. +Kauno krepšinio klube "Žalgiris". asasd +Arvydas tris sezonus iš eilės (1985–1987 m.). Arvydas +padėjo komandai iškovoti SSRS krepšinio čempionato aukso medalius. +1982 m. Arvydas as SSRS rinktinės Arvydas sudėtyje Arvydas +dalyvavo pasaulio krepšinio čempionate ir laimėjo auksą. + +as \ No newline at end of file diff --git a/Lab4/Lab4.AddSurname/Lab4.AddSurname.csproj b/Lab4/Lab4.AddSurname/Lab4.AddSurname.csproj new file mode 100644 index 0000000..3a26489 --- /dev/null +++ b/Lab4/Lab4.AddSurname/Lab4.AddSurname.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp2.1 + + + + + PreserveNewest + + + + diff --git a/Lab4/Lab4.AddSurname/Program.cs b/Lab4/Lab4.AddSurname/Program.cs new file mode 100644 index 0000000..8ef3ff5 --- /dev/null +++ b/Lab4/Lab4.AddSurname/Program.cs @@ -0,0 +1,17 @@ + +namespace Lab4.AddSurname +{ + public class Program + { + public static void Main(string[] args) + { + const string CFd = "Duomenys.txt"; + const string CFr = "Rezultatai.txt"; + string punctuation = " .,!?:;()\t'"; + string name = "Arvydas"; + string surname = "Sabonis"; + TaskUtils.ProcessAddSurname(CFd, CFr, punctuation, name, surname); + /* TaskUtils.ProcessRemoveWord(CFd, CFr, punctuation, name); */ + } + } +} diff --git a/Lab4/Lab4.AddSurname/TaskUtils.cs b/Lab4/Lab4.AddSurname/TaskUtils.cs new file mode 100644 index 0000000..47bb650 --- /dev/null +++ b/Lab4/Lab4.AddSurname/TaskUtils.cs @@ -0,0 +1,77 @@ +using System.Text; +using System.Text.RegularExpressions; +using System.IO; + +namespace Lab4.AddSurname +{ + public class TaskUtils + { + /** Finds name in the line and constructs new line appending given surname. + @param line – string of data + @param punctuation – punctuation marks to separate words + @param name – word to find + @param surname – given word to add + @param newLine – string of result */ + private static string AddSurname(string line, string punctuation, string name, string surname) + { + StringBuilder newLine = new StringBuilder(); + string addLine = " " + line + " "; + int init = 1; + int ind = addLine.IndexOf(name); + while (ind != -1) + { + if (punctuation.IndexOf(addLine[ind - 1]) != -1 && punctuation.IndexOf(addLine[ind + name.Length]) != -1) + { + newLine.Append(addLine.Substring(init, ind + name.Length - init)); + newLine.Append(" "); + newLine.Append(surname); + init = ind + name.Length; + } + ind = addLine.IndexOf(name, ind + 1); + } + newLine.Append(line.Substring(init - 1)); + + return newLine.ToString(); + } + + /** Reads file and adds given surname to the given name. + @param fin – name of data file + @param fout – name of result file + @param punctuation – punctuation marks to separate words + @param name – word to find + @param surname – given word to add */ + public static void ProcessAddSurname(string fin, string fout, string punctuation, string name, string surname) + { + string[] lines = File.ReadAllLines(fin, Encoding.UTF8); + using (var writer = File.CreateText(fout)) + { + foreach (string line in lines) + { + string newLine = AddSurname(line, punctuation, name, surname); + writer.WriteLine(newLine); + } + } + } + + public static string RemoveWord(string line, string punctuation, string word) + { + string pattern = string.Format(@"(^|[{0}]+){1}($|[{0}]+)", Regex.Escape(punctuation), Regex.Escape(word)); + return Regex.Replace(line, pattern, "$1"); + } + + public static void ProcessRemoveWord(string fin, string fout, string punctuation, string word) + { + using (StreamWriter writer = File.CreateText(fout)) + { + using (StreamReader reader = new StreamReader(fin)) + { + string line = string.Empty; + while ((line = reader.ReadLine()) != null) + { + writer.WriteLine(RemoveWord(line, punctuation, word)); + } + } + } + } + } +} diff --git a/Lab4/Lab4.ChainedWords/InOut.cs b/Lab4/Lab4.ChainedWords/InOut.cs new file mode 100644 index 0000000..84f47ca --- /dev/null +++ b/Lab4/Lab4.ChainedWords/InOut.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Lab4.ChainedWords +{ + static class InOut + { + /// + /// Read a file line-by-line using an enumarable + /// + /// Target file + /// Enumerable + public static IEnumerable ReadByLines(string filename) + { + string line; + using (StreamReader reader = new StreamReader(filename)) + { + while ((line = reader.ReadLine()) != null) + { + yield return line; + } + } + } + } +} diff --git a/Lab4/Lab4.ChainedWords/Knyga.txt b/Lab4/Lab4.ChainedWords/Knyga.txt new file mode 100644 index 0000000..09c1b75 --- /dev/null +++ b/Lab4/Lab4.ChainedWords/Knyga.txt @@ -0,0 +1,4 @@ +Reikia teksto žodžius sulygiuoti, kad kiekvienos eilutės kiekvienas žodis +prasidėtų fiksuotoje toje pačioje pozicijoje. Galima įterpti tik minimalų +būtiną tarpų skaičių. Reikia šalinti iš pradinio teksto kelis iš eilės einančius +vienodus skyriklius, paliekant tik vieną jų atstovą. Įterpimo taisyklę taikome, diff --git a/Lab4/Lab4.ChainedWords/Lab4.ChainedWords.csproj b/Lab4/Lab4.ChainedWords/Lab4.ChainedWords.csproj new file mode 100644 index 0000000..8a5bcb0 --- /dev/null +++ b/Lab4/Lab4.ChainedWords/Lab4.ChainedWords.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp2.1 + + + + + Always + + + + diff --git a/Lab4/Lab4.ChainedWords/Program.cs b/Lab4/Lab4.ChainedWords/Program.cs new file mode 100644 index 0000000..a66e42f --- /dev/null +++ b/Lab4/Lab4.ChainedWords/Program.cs @@ -0,0 +1,19 @@ +using System; + +namespace Lab4.ChainedWords +{ + class Program + { + static void Main(string[] args) + { + string input = "Knyga.txt"; + string output1 = "Rodikliai.txt"; + string output2 = "ManoKnyga.txt"; + string punctuation = " ,.;!?"; + TaskUtils.ProcessChains(input, output1, punctuation); + //TaskUtils.ProcessAligned(input, output2, punctuation); + TaskUtils.ProccessVericallyAligned(input, output2, punctuation); + } + } +} + diff --git a/Lab4/Lab4.ChainedWords/TaskUtils.cs b/Lab4/Lab4.ChainedWords/TaskUtils.cs new file mode 100644 index 0000000..d1dccc7 --- /dev/null +++ b/Lab4/Lab4.ChainedWords/TaskUtils.cs @@ -0,0 +1,249 @@ +using System; +using System.Text; +using System.Text.RegularExpressions; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace Lab4.ChainedWords +{ + static class TaskUtils + { + /// + /// Return anything that matches a word in given text, depending on punctuation + /// + /// Target text + /// Target punctuation + /// Word matches + private static MatchCollection MatchByWords(string text, string punctuation) + { + string pattern = string.Format(@"[^{0}\n]+", Regex.Escape(punctuation)); + return Regex.Matches(text, pattern, RegexOptions.IgnoreCase); + } + + /// + /// Return anything that matches a positive integer in text + /// + /// Target text + /// Integer matches + private static MatchCollection MatchByIntegers(string text) + { + return Regex.Matches(text, @"\d+"); + } + + /// + /// Find all chains of words in given text. A chain is a sequence of words where every words + /// last letter matches the next words first letter. And split the text into words using punctuation + /// + /// Target text + /// Target punctuation + /// A list of chains with line number where it was found + public static List> FindChains(string text, string punctuation) + { + List> chains = new List>(); + + bool startedChain = false; + int chainStart = 0; + + MatchCollection matches = MatchByWords(text, punctuation); + for (int i = 1; i < matches.Count; i++) + { + Match prevMatch = matches[i-1]; + Match match = matches[i]; + bool matchesCriteria = prevMatch.Value.ToLowerInvariant().Last() == match.Value.ToLowerInvariant().First(); + + if (matchesCriteria && !startedChain) + { + startedChain = true; + chainStart = prevMatch.Index; + } + else if (!matchesCriteria && startedChain) + { + startedChain = false; + int line = text.Substring(0, chainStart).Count(c => c == '\n')+1; + string chain = text.Substring(chainStart, match.Index-chainStart).TrimEnd('\n'); + chains.Add(new Tuple(line, chain)); + } + } + + // Ensure that last chain gets added to list + if (startedChain) + { + int line = text.Substring(0, chainStart).Count(c => c == '\n')+1; + string chain = text.Substring(chainStart).TrimEnd('\n'); + chains.Add(new Tuple(line, chain)); + } + + return chains; + } + + /// + /// Find all positive integers in given text + /// + /// Target text + /// List of positive integers + public static List FindIntegers(string text) + { + List integers = new List(); + + MatchCollection matches = MatchByIntegers(text); + foreach (Match match in matches) + { + integers.Add(int.Parse(match.Value)); + } + + return integers; + } + + /// + /// Finds the longest chain in the given text, while using given punctuation to determine words. + /// + /// Target text + /// Target punctuation + /// + public static Tuple FindLongestChain(string text, string punctuation) + { + List> chains = FindChains(text, punctuation); + + if (chains.Count == 0) + { + return null; + } + + Tuple longestChain = chains[0]; + foreach (var pair in chains) + { + if (pair.Item2.Length > longestChain.Item2.Length) + { + longestChain = pair; + } + } + + return longestChain; + } + + /// + /// Ouput to file: + /// * longest chain and where it was found + /// * sum of all positive integers + /// + /// Input file + /// Output file + /// Target punctuation + public static void ProcessChains(string inputFile, string outputFile, string punctuation) + { + string text = File.ReadAllText(inputFile, Encoding.UTF8); + Tuple longestChain = FindLongestChain(text, punctuation); + List integers = FindIntegers(text); + + using (StreamWriter writer = new StreamWriter(outputFile)) + { + if (longestChain != null) + { + writer.WriteLine("Ilgiausia žodžių grandinė {0} eilutėje: {1}", longestChain.Item1, longestChain.Item2); + } + else + { + writer.WriteLine("Nėra žodžių grandinių."); + } + + writer.WriteLine("Yra {0} žodžiai sudaryti iš skaitmenų. Jų suma: {1}", integers.Count, integers.Sum()); + } + } + + private static string AlignWordsByColumns(List> words, string punctuation) + { + StringBuilder aligned = new StringBuilder(); + Dictionary columns = new Dictionary(); + + foreach (List row in words) + { + int column = 0; + foreach (string word in row) + { + if (!columns.ContainsKey(column)) + { + columns.Add(column, 0); + } + columns[column] = Math.Max(columns[column], word.Length); + column++; + } + } + + foreach (List row in words) + { + int column = 0; + for (int i = 0; i < row.Count; i++) + { + string word = row[i]; + aligned.Append(word); + if (i < row.Count - 1) + { + aligned.Append(new string(' ', columns[column] - word.Length)); + } + column++; + } + aligned.Append("\n"); + } + + return aligned.ToString(); + } + + /// + /// Align every word in a line by columns + /// + /// Input file + /// Output file + /// Target punctuation + public static void ProcessAligned(string inputFile, string outputFile, string punctuation) + { + string pattern = string.Format(@"[^{0}]+[{0}]*", Regex.Escape(punctuation)); + string text = File.ReadAllText(inputFile, Encoding.UTF8); + List> words = new List>(); + + foreach (string line in InOut.ReadByLines(inputFile)) + { + List row = new List(); + words.Add(row); + foreach (Match match in Regex.Matches(line, pattern)) + { + row.Add(match.Value.TrimEnd('\n')); + } + } + + File.WriteAllText(outputFile, AlignWordsByColumns(words, punctuation)); + } + + /// + /// Align every line by columns and print words vertically + /// + /// Input file + /// Output file + /// Punctuation + public static void ProccessVericallyAligned(string inputFile, string outputFile, string punctuation) + { + string pattern = string.Format(@"[^{0}]+[{0}]*", Regex.Escape(punctuation)); + string text = File.ReadAllText(inputFile, Encoding.UTF8); + List> words = new List>(); + + foreach (string line in InOut.ReadByLines(inputFile)) + { + int column = 0; + int row = 0; + foreach (Match match in Regex.Matches(line, pattern)) + { + if (words.Count <= row) + { + words.Add(new List()); + } + words[row].Add(match.Value.TrimEnd('\n')); + row++; + } + column++; + } + + File.WriteAllText(outputFile, AlignWordsByColumns(words, punctuation)); + } + } +} + diff --git a/Lab4/Lab4.ChainedWords/tests/1/Knyga.txt b/Lab4/Lab4.ChainedWords/tests/1/Knyga.txt new file mode 100644 index 0000000..299975f --- /dev/null +++ b/Lab4/Lab4.ChainedWords/tests/1/Knyga.txt @@ -0,0 +1,8 @@ +Reikia teksto žodžius sulygiuoti, kad 33 +eilutės 1 žodis +prasidėtų fiksuotoje toje pačioje pozicijoje. +Galima įterpti tik minimalų 22 +būtiną tarpų 111. Reikia šalinti iš +pradinio teksto kelis iš eilės einančius +vienodus skyriklius, paliekant tik vieną +jų atstovą. Įterpimo taisyklę 44, diff --git a/Lab4/Lab4.ChainedWords/tests/2/Knyga.txt b/Lab4/Lab4.ChainedWords/tests/2/Knyga.txt new file mode 100644 index 0000000..ddc1b04 --- /dev/null +++ b/Lab4/Lab4.ChainedWords/tests/2/Knyga.txt @@ -0,0 +1,8 @@ +Reikia teksto žodžius , kad kiekvienos +eilutės kiekvienas žodis 100 +prasidėtų fiksuotoje toje pačioje pozicijoje. +Galima įterpti 12 minimalų +ABC CDEEEEF FG. Reikia šalinti iš +pradinio teksto kelis iš eilės einančius +vienodus 420, paliekant tik vieną +jų atstovą. FOO OFO OOOOOF diff --git a/Lab4/Lab4.FirstEqualLast/Duomenys.txt b/Lab4/Lab4.FirstEqualLast/Duomenys.txt new file mode 100644 index 0000000..4a915c7 --- /dev/null +++ b/Lab4/Lab4.FirstEqualLast/Duomenys.txt @@ -0,0 +1,9 @@ +V. M. Putinas +Margi sakalai +Lydėdami gęstančią žarą vėlai +Pakilo į dangų;;, margi sakalai. +Paniekinę žemės vylingus sapnus, +Padangėje ištiesė,,; savo sparnus. +Ir tarė margieji: negrįšim į žemę, +Kol josios kalnai ir pakalnės aptemę. +... diff --git a/Lab4/Lab4.FirstEqualLast/Lab4.FirstEqualLast.csproj b/Lab4/Lab4.FirstEqualLast/Lab4.FirstEqualLast.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Lab4/Lab4.FirstEqualLast/Lab4.FirstEqualLast.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Lab4/Lab4.FirstEqualLast/Program.cs b/Lab4/Lab4.FirstEqualLast/Program.cs new file mode 100644 index 0000000..5f8de20 --- /dev/null +++ b/Lab4/Lab4.FirstEqualLast/Program.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; + +namespace Lab4.FirstEqualLast +{ + public class Program + { + public static void Main(string[] args) + { + const string CFd = "Duomenys.txt"; + string punctuation = "[\\s,.;:!?()\\-]+"; + Console.WriteLine("Sutampančių žodžių {0, 3:d}", TaskUtils.Process(CFd, punctuation)); + } + } +} diff --git a/Lab4/Lab4.FirstEqualLast/TaskUtils.cs b/Lab4/Lab4.FirstEqualLast/TaskUtils.cs new file mode 100644 index 0000000..87945c6 --- /dev/null +++ b/Lab4/Lab4.FirstEqualLast/TaskUtils.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace Lab4.FirstEqualLast +{ + public class TaskUtils + { + /** Reads file and finds the number of words having same the first and + the last letters. + @param fin – name of data file + @param punctuation – punctuation marks to separate words */ + public static int Process(string fin, string punctuation) + { + string[] lines = File.ReadAllLines(fin, Encoding.UTF8); + int equal = 0; + foreach (string line in lines) + if (line.Length > 0) + equal += FirstEqualLast(line, punctuation); + return equal; + } + + /** Splits line into words and counts the words having same the first + and the last letters. + @param line – string of data + @param punctuation – punctuation marks to separate words */ + private static int FirstEqualLast (string line, string punctuation) + { + string[] parts = Regex.Split(line.ToLower(), punctuation); + int equal = 0; + foreach (string word in parts) + if(word.Length > 0) // empty words at the end of line + if (word[0] == word[word.Length - 1]) + equal++; + return equal; + } + + /** Reads file and finds the number of words having same the first and + the last letters. + @param fin – name of data file + @param punctuation – punctuation marks to separate words */ + public static int Process(string fin, char[] punctuation) + { + string[] lines = File.ReadAllLines(fin, Encoding.UTF8); + int equal = 0; + foreach (string line in lines) + if (line.Length > 0) + equal += FirstEqualLast(line, punctuation); + return equal; + } + + /** Splits line into words and counts the words having same the first and the + last letters. + @param line – string of data + @param punctuation – punctuation marks to separate words */ + private static int FirstEqualLast (string line, char[] punctuation) + { + string[] parts = line.ToLower().Split(punctuation, StringSplitOptions.RemoveEmptyEntries); + int equal = 0; + foreach (string word in parts) + if (word[0] == word[word.Length - 1]) + equal++; + return equal; + } + } +} diff --git a/Lab4/Lab4.K2/Lab4.K2.csproj b/Lab4/Lab4.K2/Lab4.K2.csproj new file mode 100644 index 0000000..23df604 --- /dev/null +++ b/Lab4/Lab4.K2/Lab4.K2.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.1 + + + diff --git a/Lab4/Lab4.K2/Program.cs b/Lab4/Lab4.K2/Program.cs new file mode 100644 index 0000000..607ba5b --- /dev/null +++ b/Lab4/Lab4.K2/Program.cs @@ -0,0 +1,169 @@ +using System.Text; +using System.Text.RegularExpressions; +using System.Collections.Generic; +using System.IO; + +namespace Lab4.K2 +{ + static class TaskUtils + { + public static bool NoDigits(string line) + { + return !Regex.IsMatch(line, @"\d"); + } + + public static int NumberDifferentVowelsInLine(string line) + { + string vowels = "aeiyouąęėįųū"; + int count = 0; + + line = line.ToLower(); + foreach (char vowel in vowels) + { + count += line.Contains(vowel) ? 1 : 0; + } + + return count; + } + + // Note: returns words including punctuation after them + public static MatchCollection MatchByWords(string line, string punctuation) + { + string pattern = string.Format(@"([^{0}]+)[{0}]*", Regex.Escape(punctuation)); + return Regex.Matches(line, pattern); + } + + public static string FindWord1Line(string line, string punctuation) + { + Match longestWord = null; + foreach (Match match in MatchByWords(line, punctuation)) + { + int vowelCount = NumberDifferentVowelsInLine(match.Value); + if (vowelCount == 3 && (longestWord == null || longestWord.Groups[1].Length < match.Groups[1].Length)) + { + longestWord = match; + } + } + + if (longestWord == null) + { + return ""; + } + else + { + return longestWord.Value; + } + } + + public static string EditLine(string line, string punctuation, string word) + { + // 1. Find given word in line + MatchCollection words = MatchByWords(line, punctuation); + int wordPosition = -1; + + for (int i = 0; i < words.Count; i++) + { + if (words[i].Value == word) + { + wordPosition = i; + break; + } + } + + if (wordPosition == -1) return line; + + // 2. Move found word to front + StringBuilder builder = new StringBuilder(); + builder.Append(words[wordPosition]); + for (int i = 0; i < words.Count; i++) + { + if (i != wordPosition) + { + builder.Append(words[i]); + } + } + + // 3. Profit + return builder.ToString(); + } + + public static Match MatchLastWord(string line, string punctuation) + { + string pattern = string.Format(@"[^{0}]+[{0}]*$", Regex.Escape(punctuation)); + return Regex.Match(line, pattern); + } + + public static string FindWord2Line(string line, string punctuation) + { + Match lastWord = MatchLastWord(line, punctuation); + if (NoDigits(lastWord.Value)) + { + return lastWord.Value; + } + else + { + return ""; + } + } + + public static IEnumerable ReadByLines(string filename) + { + string line; + using (StreamReader reader = new StreamReader(filename, Encoding.UTF8)) + { + while ((line = reader.ReadLine()) != null) + { + yield return line; + } + } + } + + public static void PerformTask(string fd, string fr) + { + List wordsWithoutDigits = new List(); + string punctuation = null; + using (StreamWriter writer = File.CreateText(fr)) + { + foreach (string line in ReadByLines(fd)) + { + if (punctuation == null) + { + punctuation = line; + continue; + } + + string longestWord = TaskUtils.FindWord1Line(line, punctuation); + if (longestWord != "") + { + writer.WriteLine(TaskUtils.EditLine(line, punctuation, longestWord)); + } + else + { + writer.WriteLine(line); + } + + string wordWithoutDigits = TaskUtils.FindWord2Line(line, punctuation); + if (wordWithoutDigits != "") + { + wordsWithoutDigits.Add(wordWithoutDigits); + } + } + + char[] punctuationChars = punctuation.ToCharArray(); + foreach (string word in wordsWithoutDigits) + { + writer.WriteLine(word.Trim(punctuationChars)); + } + } + } + } + + class Program + { + static void Main(string[] args) + { + TaskUtils.PerformTask("Tekstas.txt", "RedTekstas.txt"); + } + } +} + diff --git a/Lab4/Lab4.K2/Tekstas.txt b/Lab4/Lab4.K2/Tekstas.txt new file mode 100644 index 0000000..aa597a6 --- /dev/null +++ b/Lab4/Lab4.K2/Tekstas.txt @@ -0,0 +1,5 @@ + .!?;:@ +bar fooo,@ !ba1 +bar bAaar baar bAoUr!! +bar foėo@ 1ba +bar? baer boar f!! diff --git a/Lab4/Lab4.LetterFrequency/Duomenys.txt b/Lab4/Lab4.LetterFrequency/Duomenys.txt new file mode 100644 index 0000000..289a351 --- /dev/null +++ b/Lab4/Lab4.LetterFrequency/Duomenys.txt @@ -0,0 +1,6 @@ +Draugas draugui sako: +- Ką pasakytum, jei sutiktum moterį, kuri visada būtų švelni ir maloni, + viską tau atleistų, tave dievintų. +- Tokių nebūna. +- Būna. Reiktų pasakyti: +- Labas, mama! \ No newline at end of file diff --git a/Lab4/Lab4.LetterFrequency/InOut.cs b/Lab4/Lab4.LetterFrequency/InOut.cs new file mode 100644 index 0000000..6b667b2 --- /dev/null +++ b/Lab4/Lab4.LetterFrequency/InOut.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Lab4.LetterFrequency +{ + static class InOut + { + /** Prints repetition of letters using two columns into a given file. + @param fout – name of the file for the output + @param letters – object having letters and their repetitions */ + public static void PrintRepetitions(string fout, LettersFrequency letters) + { + using (var writer = File.CreateText(fout)) + { + foreach (var pair in letters.GetOrderedFrequencies()) + { + writer.WriteLine("{0, 3:c} {1, 4:d}", pair.Item1, pair.Item2); + } + } + } + + /** Inputs from the given data file and counts repetition of letters. + @param fin – name of data file + @param letters – object having letters and their repetitions*/ + public static void Repetitions(string fin, LettersFrequency letters) + { + using (StreamReader reader = new StreamReader(fin)) + { + string line; + while ((line = reader.ReadLine()) != null) + { + letters.line = line; + letters.CountLetters(); + } + } + } + } +} diff --git a/Lab4/Lab4.LetterFrequency/Lab4.LetterFrequency.csproj b/Lab4/Lab4.LetterFrequency/Lab4.LetterFrequency.csproj new file mode 100644 index 0000000..3a26489 --- /dev/null +++ b/Lab4/Lab4.LetterFrequency/Lab4.LetterFrequency.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp2.1 + + + + + PreserveNewest + + + + diff --git a/Lab4/Lab4.LetterFrequency/LettersFrequency.cs b/Lab4/Lab4.LetterFrequency/LettersFrequency.cs new file mode 100644 index 0000000..07e01aa --- /dev/null +++ b/Lab4/Lab4.LetterFrequency/LettersFrequency.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Text; + +namespace Lab4.LetterFrequency +{ + /** To count letters frequency */ + class LettersFrequency + { + private const int CMax = 256; + private Dictionary Frequency; // Frequency of letters + public string line { get; set; } + + public LettersFrequency() + { + line = ""; + Frequency = new Dictionary(); + for (char c = 'a'; c <= 'z'; c++) + { + Frequency.Add(c, 0); + } + for (char c = 'A'; c <= 'Z'; c++) + { + Frequency.Add(c, 0); + } + } + + public int Get(char character) + { + return Frequency[character]; + } + + /** Counts repetition of letters. */ + public void CountLetters() + { + for (int i = 0; i < line.Length; i++) + { + if (Char.IsLetter(line[i])) + { + if (!Frequency.ContainsKey(line[i])) + { + Frequency.Add(line[i], 0); + } + Frequency[line[i]]++; + } + } + } + + public char GetMostFrequent() + { + char mostFrequent = (char)(0); + foreach (var pair in Frequency) + { + if (mostFrequent == 0 || pair.Value > Frequency[mostFrequent]) + { + mostFrequent = pair.Key; + } + } + return mostFrequent; + } + + public List> GetOrderedFrequencies() + { + List> frequencies = new List>(); + foreach (var pair in Frequency) + { + frequencies.Add(new Tuple(pair.Key, pair.Value)); + } + + frequencies.Sort(delegate (Tuple x, Tuple y) + { + return -x.Item2.CompareTo(y.Item2); + }); + + return frequencies; + } + } + +} diff --git a/Lab4/Lab4.LetterFrequency/Program.cs b/Lab4/Lab4.LetterFrequency/Program.cs new file mode 100644 index 0000000..e9cad4c --- /dev/null +++ b/Lab4/Lab4.LetterFrequency/Program.cs @@ -0,0 +1,21 @@ +using System; + +namespace Lab4.LetterFrequency +{ + class Program + { + static void Main(string[] args) + { + const string CFd = "Duomenys.txt"; + const string CFr = "Rezultatai.txt"; + LettersFrequency letters = new LettersFrequency(); + InOut.Repetitions(CFd, letters); + char mostFrequent = letters.GetMostFrequent(); + if (mostFrequent != 0) + { + Console.WriteLine("Dažniausiai pasikartojanti raidė: {0}", mostFrequent); + } + InOut.PrintRepetitions(CFr, letters); + } + } +} diff --git a/Lab4/Lab4.RemoveComments/Duomenys.txt b/Lab4/Lab4.RemoveComments/Duomenys.txt new file mode 100644 index 0000000..c0b1ff0 --- /dev/null +++ b/Lab4/Lab4.RemoveComments/Duomenys.txt @@ -0,0 +1,19 @@ +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); +} diff --git a/Lab4/Lab4.RemoveComments/InOut.cs b/Lab4/Lab4.RemoveComments/InOut.cs new file mode 100644 index 0000000..7469530 --- /dev/null +++ b/Lab4/Lab4.RemoveComments/InOut.cs @@ -0,0 +1,39 @@ +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)) + { + int j = 0; // Used for cleaned lines + // Check line by line which differ + for (int i = 0; i < lines.Length && j < cleanedLines.Length; i++) + { + if (lines[i] != cleanedLines[j]) + { + writer.WriteLine(lines[i]); + if (lines[i].StartsWith(cleanedLines[j])) + { + j++; + } + } + else + { + j++; + } + } + } + } + } +} diff --git a/Lab4/Lab4.RemoveComments/Lab4.RemoveComments.csproj b/Lab4/Lab4.RemoveComments/Lab4.RemoveComments.csproj new file mode 100644 index 0000000..3a26489 --- /dev/null +++ b/Lab4/Lab4.RemoveComments/Lab4.RemoveComments.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp2.1 + + + + + PreserveNewest + + + + diff --git a/Lab4/Lab4.RemoveComments/Program.cs b/Lab4/Lab4.RemoveComments/Program.cs new file mode 100644 index 0000000..f789a14 --- /dev/null +++ b/Lab4/Lab4.RemoveComments/Program.cs @@ -0,0 +1,16 @@ +using System; +using System.Text.RegularExpressions; + +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/Lab4.RemoveComments/TaskUtils.cs b/Lab4/Lab4.RemoveComments/TaskUtils.cs new file mode 100644 index 0000000..dfa30f3 --- /dev/null +++ b/Lab4/Lab4.RemoveComments/TaskUtils.cs @@ -0,0 +1,61 @@ +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(); + + bool multiLineComment = false; + + foreach (string line in lines) + { + Match case1 = Regex.Match(line, @"^(.*)/\*.*$"); + Match case2 = Regex.Match(line, @"^.*\*/(.*)$"); + Match case3 = Regex.Match(line, @"^(.*)//.*$"); + + if (case1.Success) + { + if (!multiLineComment) { + multiLineComment = true; + if (case1.Groups[1].Value.Length > 0) + { + cleanedLines.Add(case1.Groups[1].Value); + } + } + } + else if (case2.Success) + { + if (multiLineComment) { + multiLineComment = false; + if (case2.Groups[1].Value.Length > 0) + { + cleanedLines.Add(case2.Groups[1].Value); + } + } + } + else if (case3.Success) + { + if (case3.Groups[1].Value.Length > 0) + { + cleanedLines.Add(case3.Groups[1].Value); + } + } + else + { + cleanedLines.Add(line); + } + } + + return cleanedLines.ToArray(); + } + } +} diff --git a/Lab4/Lab4.RemoveLines/Duomenys.txt b/Lab4/Lab4.RemoveLines/Duomenys.txt new file mode 100644 index 0000000..9202125 --- /dev/null +++ b/Lab4/Lab4.RemoveLines/Duomenys.txt @@ -0,0 +1,10 @@ +Aš atverčiau knygą kur pakliuvo. +Raidžių formos man buvo niekur nematytos. +Puslapiai apšiurę, raidės neryškios, tekstas ėjo dviem skiltimis. +Puslapiai apšiurę, raidės neryškios, tekstas ėjo dviem skiltimis. +Šriftas buvo tankus ir padalytas į pastraipas. +Ir mažas paveikslėlis, toks, koks pasitaiko žodynuose: +plunksna nupieštas inkaras, bet lyg negrabia vaiko ranka. +Puslapiai apšiurę, raidės neryškios, tekstas ėjo dviem skiltimis. + + Jorge Luis Borges. Smėlio knyga. Vilnius, 2006. \ No newline at end of file diff --git a/Lab4/Lab4.RemoveLines/InOut.cs b/Lab4/Lab4.RemoveLines/InOut.cs new file mode 100644 index 0000000..094d744 --- /dev/null +++ b/Lab4/Lab4.RemoveLines/InOut.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Lab4.RemoveLines +{ + class InOut + { + public static int LongestLine(string fin) + { + int No = -1; + using (StreamReader reader = new StreamReader(fin, Encoding.UTF8)) + { + string line; + int length = 0; + int lineNo = 0; + while ((line = reader.ReadLine()) != null) + { + if (line.Length > length) + { + length = line.Length; + No = lineNo; + } + lineNo++; + } + } + return No; + } public static List LongestLines(string fin) + { + List lines = new List(); + int longestLength = 0; + + using (StreamReader reader = new StreamReader(fin, Encoding.UTF8)) + { + string line; + int lineNo = 0; + while ((line = reader.ReadLine()) != null) + { + if (line.Length > longestLength) + { + longestLength = line.Length; + lines.Clear(); + lines.Add(lineNo); + } + else if (line.Length == longestLength) + { + lines.Add(lineNo); + } + lineNo++; + } + } + + return lines; + } public static void RemoveLine(string fin, string fout, int No) + { + using (StreamReader reader = new StreamReader(fin, Encoding.UTF8)) + { + string line; + int lineNo = 0; + using (var writer = File.CreateText(fout)) + { + while ((line = reader.ReadLine()) != null) + { + if (No != lineNo) + { + writer.WriteLine(line); + } + lineNo++; + } + } + } + } + + public static void RemoveLines(string fin, string fout, List lines) + { + using (StreamReader reader = new StreamReader(fin, Encoding.UTF8)) + { + string line; + int lineNo = 0; + using (var writer = File.CreateText(fout)) + { + while ((line = reader.ReadLine()) != null) + { + if (!lines.Contains(lineNo)) + { + writer.WriteLine(line); + } + lineNo++; + } + } + } + } + } +} diff --git a/Lab4/Lab4.RemoveLines/Lab4.RemoveLines.csproj b/Lab4/Lab4.RemoveLines/Lab4.RemoveLines.csproj new file mode 100644 index 0000000..3a26489 --- /dev/null +++ b/Lab4/Lab4.RemoveLines/Lab4.RemoveLines.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp2.1 + + + + + PreserveNewest + + + + diff --git a/Lab4/Lab4.RemoveLines/Program.cs b/Lab4/Lab4.RemoveLines/Program.cs new file mode 100644 index 0000000..7eb6f5b --- /dev/null +++ b/Lab4/Lab4.RemoveLines/Program.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; + +namespace Lab4.RemoveLines +{ + class Program + { + static void Main(string[] args) + { + const string CFd = "Duomenys.txt"; + const string CFr = "Rezultatai.txt"; + List lines = InOut.LongestLines(CFd); + InOut.RemoveLines(CFd, CFr, lines); + foreach (int line in lines) + { + Console.WriteLine("Ilgiausios eilutės nr. {0, 4:d}", line + 1); + } + } + } +} diff --git a/Lab4/Lab4.RemoveVowels/Duomenys.txt b/Lab4/Lab4.RemoveVowels/Duomenys.txt new file mode 100644 index 0000000..4968592 --- /dev/null +++ b/Lab4/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/Lab4.RemoveVowels/Lab4.RemoveVowels.csproj b/Lab4/Lab4.RemoveVowels/Lab4.RemoveVowels.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Lab4/Lab4.RemoveVowels/Lab4.RemoveVowels.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Lab4/Lab4.RemoveVowels/Program.cs b/Lab4/Lab4.RemoveVowels/Program.cs new file mode 100644 index 0000000..89b1bda --- /dev/null +++ b/Lab4/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/Lab4.RemoveVowels/TaskUtils.cs b/Lab4/Lab4.RemoveVowels/TaskUtils.cs new file mode 100644 index 0000000..0212a92 --- /dev/null +++ b/Lab4/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/Lab4.sln b/Lab4/Lab4.sln new file mode 100644 index 0000000..ce89cec --- /dev/null +++ b/Lab4/Lab4.sln @@ -0,0 +1,67 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.1525 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "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("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab4.LetterFrequency", "Lab4.LetterFrequency\Lab4.LetterFrequency.csproj", "{F1797B5C-1541-4821-9485-7C3520C5DD8D}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab4.FirstEqualLast", "Lab4.FirstEqualLast\Lab4.FirstEqualLast.csproj", "{83CB5DEC-25E5-42FF-8601-9BF8DA73E397}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab4.AddSurname", "Lab4.AddSurname\Lab4.AddSurname.csproj", "{F7EFA186-AD5D-4E5D-B405-9ACBAEDB7BF3}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab4.RemoveVowels", "Lab4.RemoveVowels\Lab4.RemoveVowels.csproj", "{5AD87F42-DD6D-4ADF-B1D5-66553BAA4BCC}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab4.ChainedWords", "Lab4.ChainedWords\Lab4.ChainedWords.csproj", "{DA96CD19-65AA-4665-831F-8D9B21A6E369}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab4.K2", "Lab4.K2\Lab4.K2.csproj", "{BE0E7198-95F7-4EFA-BFE1-CA5BBE7798D2}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5309D21D-2A14-4332-90EC-504AD89CE397}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5309D21D-2A14-4332-90EC-504AD89CE397}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5309D21D-2A14-4332-90EC-504AD89CE397}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5309D21D-2A14-4332-90EC-504AD89CE397}.Release|Any CPU.Build.0 = Release|Any CPU + {4D9B08C9-5735-4C95-8D2A-BDFC888B1E68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4D9B08C9-5735-4C95-8D2A-BDFC888B1E68}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4D9B08C9-5735-4C95-8D2A-BDFC888B1E68}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4D9B08C9-5735-4C95-8D2A-BDFC888B1E68}.Release|Any CPU.Build.0 = Release|Any CPU + {F1797B5C-1541-4821-9485-7C3520C5DD8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F1797B5C-1541-4821-9485-7C3520C5DD8D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F1797B5C-1541-4821-9485-7C3520C5DD8D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F1797B5C-1541-4821-9485-7C3520C5DD8D}.Release|Any CPU.Build.0 = Release|Any CPU + {83CB5DEC-25E5-42FF-8601-9BF8DA73E397}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {83CB5DEC-25E5-42FF-8601-9BF8DA73E397}.Debug|Any CPU.Build.0 = Debug|Any CPU + {83CB5DEC-25E5-42FF-8601-9BF8DA73E397}.Release|Any CPU.ActiveCfg = Release|Any CPU + {83CB5DEC-25E5-42FF-8601-9BF8DA73E397}.Release|Any CPU.Build.0 = Release|Any CPU + {F7EFA186-AD5D-4E5D-B405-9ACBAEDB7BF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {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 + {DA96CD19-65AA-4665-831F-8D9B21A6E369}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DA96CD19-65AA-4665-831F-8D9B21A6E369}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DA96CD19-65AA-4665-831F-8D9B21A6E369}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DA96CD19-65AA-4665-831F-8D9B21A6E369}.Release|Any CPU.Build.0 = Release|Any CPU + {BE0E7198-95F7-4EFA-BFE1-CA5BBE7798D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BE0E7198-95F7-4EFA-BFE1-CA5BBE7798D2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BE0E7198-95F7-4EFA-BFE1-CA5BBE7798D2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BE0E7198-95F7-4EFA-BFE1-CA5BBE7798D2}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8BAFACEF-C8ED-4ADA-A33E-7A62B410E9B5} + EndGlobalSection +EndGlobal diff --git a/Lab4/README.md b/Lab4/README.md new file mode 100644 index 0000000..24dc07e --- /dev/null +++ b/Lab4/README.md @@ -0,0 +1,6 @@ +# Lab4 example project + +This project is from the "Object oriented programming" course in KTU. + +It is not polished in any way, so don't expect anything from this. + diff --git a/Lab4/global.json b/Lab4/global.json new file mode 100644 index 0000000..4aa91ae --- /dev/null +++ b/Lab4/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version": "3.0.103" + } +}