Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Clones
  5. {
  6.     public class CloneVersionSystem : ICloneVersionSystem
  7.     {
  8.         List<Clone> clones;
  9.         public string Execute(string query)
  10.         {
  11.             string[] commands = query.Split();
  12.             clones = new List<Clone>();
  13.                 switch (commands[0])
  14.                 {
  15.                     case "learn":
  16.                     {
  17.                         Learn(Int32.Parse(commands[1])-1, commands[2]);
  18.                         break;
  19.                     }
  20.                     case "rollback":
  21.                     {
  22.                         RollBack(Int32.Parse(commands[1])-1);
  23.                         break;
  24.                     }
  25.                     case "relearn":
  26.                     {
  27.                         Relearn(Int32.Parse(commands[1])-1);
  28.                         break;
  29.                     }
  30.                     case "clone":
  31.                     {
  32.                         MakeClone(Int32.Parse(commands[1])-1);
  33.                         break;
  34.                     }
  35.                     case "check":
  36.                     {
  37.                         return Check(Int32.Parse(commands[1])-1);
  38.                     }
  39.                 }
  40.                 return null;
  41.         }
  42.  
  43.         private void Learn(int index, string command){
  44.             if(index >= clones.Count){
  45.                 clones.Insert(index, new Clone(index));
  46.             }
  47.             clones[index].Learned.Push(command);
  48.         }
  49.  
  50.         private void RollBack(int index){
  51.             var lastProgramm = clones[index].Learned.Pop();
  52.             clones[index].Canceled.Push(lastProgramm);
  53.         }
  54.  
  55.         private void Relearn(int index){
  56.             var lastProgramm = clones[index].Canceled.Pop();
  57.             clones[index].Learned.Push(lastProgramm);
  58.         }
  59.  
  60.         private void MakeClone(int index){
  61.             clones.Add(new Clone(clones.Count));
  62.             clones[clones.Count].Learned.Head = clones[index].Learned.Head;
  63.             clones[clones.Count].Canceled.Head = clones[index].Canceled.Head;
  64.         }
  65.  
  66.         private string Check(int index){
  67.             if(clones[index].Learned.Head == null)
  68.                 return "basic";
  69.             return clones[index].Learned.Head.Value;
  70.         }
  71.     }
  72.  
  73.     public class Clone{
  74.         public CloneStack Learned;
  75.         public CloneStack Canceled;
  76.         int Number;
  77.  
  78.         public Clone(int number){
  79.             Number = number;
  80.             Learned = new CloneStack();
  81.             Canceled = new CloneStack();
  82.         }
  83.     }
  84.  
  85.     public class StackItem{
  86.         public string Value {get; set; }
  87.         public StackItem Next{get; set;}
  88.     }
  89.  
  90.     public class CloneStack{
  91.         public StackItem Head;
  92.  
  93.         public void Push(string value){
  94.             if(Head == null){
  95.                 Head = new StackItem { Value = value, Next = null };
  96.             }
  97.             else
  98.             {
  99.                 Head = new StackItem { Value = value, Next = Head };
  100.             }
  101.         }
  102.  
  103.         public string Pop(){
  104.             if (Head == null) throw new InvalidOperationException();
  105.             var result = Head.Value;
  106.             Head = Head.Next;
  107.             return result;
  108.         }
  109.  
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement