I have attached small.txt that I wand to read and expect out in "output-small"
here is the question
see multiple starts before a stop, then you can look at it as nested function calls, so the next stop will correspond with the most recently started function that hasn't yet been stopped, in a stack-like fashion.
task is to write a program to parse an input file and produce a list of functions with their total running time. Ideally, this output should also allow you to visualize the tree structure of the program as well. included an example output for the small.txt input file as well.
To skin this cat I came up following logic

and developed follwing code
- private static void Main(string[] args)
- {
- Console.WriteLine("Please enter the Path");
- string path = Console.ReadLine();
- ReadTextFile(path);
- }
-
-
- private static void ReadTextFile(string path)
- {
- string string1 = ""; Decimal elaps1;
- try
- {
- string[] lines = File.ReadAllLines(path);
- var list = new List>();
- var Listabove = new List>();
- for (int i = 0; i < lines.Count(); i++)
- {
- int result; int result1;
- int a = 0;
-
- if (lines[i].Split(',')[0] == "start" && lines[i + 1].Split(',')[0] != "stop")
- Listabove.Add(new KeyValuePair(lines[i].Split(',')[1], Convert.ToDecimal(lines[i].Split(',')[2])));
- if (lines[i].Split(',')[0] == "stop")
- {
- if (lines[i - 1].Split(',')[0] != "stop")
- {
- var j = Convert.ToDecimal(lines[i - 1].Split(',')[2]);
- var j1 = Convert.ToDecimal(lines[i].Split(',')[1]);
- var elapstime = j1 - j;
-
- list.Add(new KeyValuePair(lines[i - 1].Split(',')[1], elapstime));
-
-
- for (int s = 1; s < 8; s++)
- {
- try
- {
- if (lines[i + s].Split(',')[0] == "stop")
- {
- var k = Convert.ToDecimal(lines[i + s].Split(',')[1]);
- list.Add(new KeyValuePair(Listabove.Last().Key, k - Listabove.Last().Value));
- Listabove.RemoveAt(Listabove.Count - 1);
- }
- else
- {
- break;
- }
- }
- catch (Exception e)
- {
- break;
- }
- }
- }
- }
- }
- foreach (var item in list)
- {
- Console.WriteLine($" {item.Key} : {item.Value}");
- }
- Console.ReadLine();
- }
- catch (FileNotFoundException ef)
- {
-
- }
- catch (IOException e)
- {
-
-
- if (e.Source != null)
- Console.WriteLine("IOException source: {0}", e.Source);
- throw;
- }
- catch (Exception e)
- {
- Console.WriteLine("{0} Exception caught.", e);
- }
- }
my question is if there is better way to do this so that I do not have do
for (int s = 1; s < 8; s++)
or someone can share Recursive function idea
where I'm assuming there are no more than 8 consecutive stops occurs
in reality, there could be any numbers of stops can occur
any help will be appreciated