diff --git a/Lab4.ChainedWords/Program.cs b/Lab4.ChainedWords/Program.cs index a66aa2d..a66e42f 100644 --- a/Lab4.ChainedWords/Program.cs +++ b/Lab4.ChainedWords/Program.cs @@ -11,7 +11,8 @@ namespace Lab4.ChainedWords string output2 = "ManoKnyga.txt"; string punctuation = " ,.;!?"; TaskUtils.ProcessChains(input, output1, punctuation); - TaskUtils.ProcessAligned(input, output2, punctuation); + //TaskUtils.ProcessAligned(input, output2, punctuation); + TaskUtils.ProccessVericallyAligned(input, output2, punctuation); } } } diff --git a/Lab4.ChainedWords/TaskUtils.cs b/Lab4.ChainedWords/TaskUtils.cs index 7cfb8c8..d1dccc7 100644 --- a/Lab4.ChainedWords/TaskUtils.cs +++ b/Lab4.ChainedWords/TaskUtils.cs @@ -151,6 +151,44 @@ namespace Lab4.ChainedWords } } + 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 /// @@ -161,45 +199,50 @@ namespace Lab4.ChainedWords { string pattern = string.Format(@"[^{0}]+[{0}]*", Regex.Escape(punctuation)); string text = File.ReadAllText(inputFile, Encoding.UTF8); - Dictionary columns = new Dictionary(); List> words = new List>(); foreach (string line in InOut.ReadByLines(inputFile)) { List row = new List(); words.Add(row); - int column = 0; foreach (Match match in Regex.Matches(line, pattern)) { - string word = match.Value.TrimEnd('\n'); - row.Add(word); - if (!columns.ContainsKey(column)) - { - columns.Add(column, 0); - } - columns[column] = Math.Max(columns[column], word.Length); - column++; + row.Add(match.Value.TrimEnd('\n')); } } - using (StreamWriter writer = new StreamWriter(outputFile)) + 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)) { - foreach (List row in words) + int column = 0; + int row = 0; + foreach (Match match in Regex.Matches(line, pattern)) { - int column = 0; - for (int i = 0; i < row.Count; i++) + if (words.Count <= row) { - string word = row[i]; - writer.Write(word); - if (i < row.Count-1) - { - writer.Write(new string(' ', columns[column]-word.Length)); - } - column++; + words.Add(new List()); } - writer.Write("\n"); + words[row].Add(match.Value.TrimEnd('\n')); + row++; } + column++; } + + File.WriteAllText(outputFile, AlignWordsByColumns(words, punctuation)); } } }