diff --git a/Lab4.RemoveComments/Duomenys.txt b/Lab4.RemoveComments/Duomenys.txt index c44a7de..c0b1ff0 100644 --- a/Lab4.RemoveComments/Duomenys.txt +++ b/Lab4.RemoveComments/Duomenys.txt @@ -3,16 +3,17 @@ void DuomenysInternet(Grybai & grybai) ifstream fd(u2); // string pav, tip; // GrybasInfo s1; - int ns = 0; + 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; + 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 index b0e7236..7469530 100644 --- a/Lab4.RemoveComments/InOut.cs +++ b/Lab4.RemoveComments/InOut.cs @@ -16,14 +16,17 @@ namespace Lab4.RemoveComments using (var writer = File.CreateText(finfo)) { - // Check line by line which differ int j = 0; // Used for cleaned lines + // Check line by line which differ 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]); + if (lines[i].StartsWith(cleanedLines[j])) + { + j++; + } } else { @@ -31,30 +34,6 @@ namespace Lab4.RemoveComments } } } - - /* - 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/TaskUtils.cs b/Lab4.RemoveComments/TaskUtils.cs index d87b69a..dfa30f3 100644 --- a/Lab4.RemoveComments/TaskUtils.cs +++ b/Lab4.RemoveComments/TaskUtils.cs @@ -14,14 +14,39 @@ namespace Lab4.RemoveComments { List cleanedLines = new List(); + bool multiLineComment = false; + foreach (string line in lines) { - Match case1 = Regex.Match(line, @"^(.*)//.*$"); - if (case1.Success) + 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 (case1.Groups[1].Value.Length > 0) + if (case3.Groups[1].Value.Length > 0) { - cleanedLines.Add(case1.Groups[1].Value); + cleanedLines.Add(case3.Groups[1].Value); } } else @@ -30,35 +55,7 @@ namespace Lab4.RemoveComments } } - /* - 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;*/ } } }