aay jay

aay jay

  • NA
  • 85
  • 0

Read text file and process calculation Impreva

Jun 27 2019 2:12 PM
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
  1. private static void Main(string[] args)  
  2. {  
  3. Console.WriteLine("Please enter the Path");  
  4. string path = Console.ReadLine();  
  5. ReadTextFile(path);  
  6. }  
  7. // this method can replace with Recursive function to make it more robust  
  8. // if I'm given job oportunity I will submit code with Recursive function  
  9. private static void ReadTextFile(string path)  
  10. {  
  11. string string1 = ""; Decimal elaps1;  
  12. try  
  13. {  
  14. string[] lines = File.ReadAllLines(path);  
  15. var list = new List>();  
  16. var Listabove = new List>();  
  17. for (int i = 0; i < lines.Count(); i++)  
  18. {  
  19. int result; int result1;  
  20. int a = 0;  
  21. // save in list one  
  22. if (lines[i].Split(',')[0] == "start" && lines[i + 1].Split(',')[0] != "stop")  
  23. Listabove.Add(new KeyValuePair(lines[i].Split(',')[1], Convert.ToDecimal(lines[i].Split(',')[2])));  
  24. if (lines[i].Split(',')[0] == "stop")  
  25. {  
  26. if (lines[i - 1].Split(',')[0] != "stop")  
  27. {  
  28. var j = Convert.ToDecimal(lines[i - 1].Split(',')[2]);  
  29. var j1 = Convert.ToDecimal(lines[i].Split(',')[1]);  
  30. var elapstime = j1 - j;  
  31. //save into list2  
  32. list.Add(new KeyValuePair(lines[i - 1].Split(',')[1], elapstime));  
  33. // catch left over  
  34. // check how may time stop occurs  
  35. for (int s = 1; s < 8; s++) // need to set correct number by counting consequetive stops  
  36. {  
  37. try  
  38. {  
  39. if (lines[i + s].Split(',')[0] == "stop")  
  40. {  
  41. var k = Convert.ToDecimal(lines[i + s].Split(',')[1]);  
  42. list.Add(new KeyValuePair(Listabove.Last().Key, k - Listabove.Last().Value));  
  43. Listabove.RemoveAt(Listabove.Count - 1);  
  44. }  
  45. else  
  46. {  
  47. break;  
  48. }  
  49. }  
  50. catch (Exception e)  
  51. {  
  52. break;  
  53. }  
  54. }  
  55. }  
  56. }  
  57. }  
  58. foreach (var item in list)  
  59. {  
  60. Console.WriteLine($" {item.Key} : {item.Value}");  
  61. }  
  62. Console.ReadLine();  
  63. }  
  64. catch (FileNotFoundException ef)  
  65. {  
  66. // FileNotFoundExceptions are handled here.  
  67. }  
  68. catch (IOException e)  
  69. {  
  70. // Extract some information from this exception, and then  
  71. // throw it to the parent method.  
  72. if (e.Source != null)  
  73. Console.WriteLine("IOException source: {0}", e.Source);  
  74. throw;  
  75. }  
  76. catch (Exception e)  
  77. {  
  78. Console.WriteLine("{0} Exception caught.", e);  
  79. }  

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

Attachment: archive_(2).zip

Answers (5)