Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Clones
  5. {
  6. public class StackItem
  7. {
  8. public string Value { get; set; }
  9. public StackItem Next { get; set; }
  10. }
  11.  
  12. public class Stack
  13. {
  14. public StackItem tail;
  15. public void Push(string value)
  16. {
  17. if (tail == null)
  18. tail = new StackItem { Value = value, Next = null };
  19. else
  20. {
  21. var item = new StackItem { Value = value, Next = tail };
  22. tail = item;
  23. }
  24. }
  25.  
  26. public string Depush()
  27. {
  28. if (tail == null) throw new InvalidOperationException();
  29. var result = tail.Value;
  30. tail = tail.Next;
  31. return result.ToString();
  32. }
  33.  
  34. internal Stack GetCopy()
  35. {
  36. return new Stack { tail = tail };
  37. }
  38. }
  39.  
  40. public class Clone
  41. {
  42. public Stack learnedProgramm = new Stack();
  43. public Stack unlearnedProgramm = new Stack();
  44.  
  45. public Clone()
  46. {
  47. if (learnedProgramm.tail == null)
  48. learnedProgramm.Push("basic");
  49. }
  50.  
  51. public void Learn(string programm)
  52. {
  53. learnedProgramm.Push(programm);
  54. }
  55. public void Rollback()
  56. {
  57. var lastProgramm = learnedProgramm.Depush();
  58. unlearnedProgramm.Push(lastProgramm);
  59. }
  60.  
  61. public Clone GetCLone()
  62. {
  63. return new Clone { learnedProgramm = learnedProgramm.GetCopy(), unlearnedProgramm = unlearnedProgramm };
  64. }
  65.  
  66. public string Chek()
  67. {
  68. return learnedProgramm.tail.Value;
  69. }
  70.  
  71. public void Relearn()
  72. {
  73. learnedProgramm.Push(unlearnedProgramm.Depush());
  74. }
  75. }
  76.  
  77. public class CloneVersionSystem : ICloneVersionSystem
  78. {
  79. public List<Clone> clones = new List<Clone>();
  80. public CloneVersionSystem()
  81. => clones.Add(new Clone());
  82. public string Execute(string query)
  83. {
  84. var instruction = query.Split();
  85. switch (instruction[0])
  86. {
  87. case "learn": clones[int.Parse(instruction[1]) - 1].Learn(instruction[2]); break;
  88. case "rollback": clones[int.Parse(instruction[1]) - 1].Rollback(); break;
  89. case "check": return clones[int.Parse(instruction[1]) - 1].learnedProgramm.tail.Value;
  90. case "relearn": clones[int.Parse(instruction[1]) - 1].Relearn(); break;
  91. case "clone": clones.Add(clones[int.Parse(instruction[1]) - 1].GetCLone()); break;
  92. }
  93. return null;
  94. }
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement