Advertisement
Guest User

Untitled

a guest
Aug 15th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.22 KB | None | 0 0
  1. class notepad
  2. {
  3.   var hist = List[Tuple3[Int, Char, Boolean]]() // actions. int - pos, char - sym (added or deleted), boolean - true, if action = adding
  4.   var posInHist = 0 // actions 0, 1, ..., posInHist-1 from hist applied to "" will give text
  5.   var text = "" // current text
  6.  
  7.   def addSym(pos: Int, sym: Char)
  8.   {
  9.     if (posInHist < hist.length )
  10.       hist = hist.slice(0,posInHist)
  11.  
  12.     text = text.substring(0, pos) + sym + text.substring(pos,text.length)
  13.     hist = hist ::: (pos, sym, true) :: Nil
  14.     posInHist += 1
  15.   }
  16.  
  17.   def delSym(pos: Int)
  18.   {
  19.     if (posInHist < hist.length )
  20.       hist = hist.slice(0,posInHist)
  21.  
  22.     var sym: Char = text(pos)
  23.  
  24.     text = text.substring(0, pos) + text.substring(pos+1,text.length)
  25.     hist = hist ::: (pos, sym, false) :: Nil
  26.     posInHist += 1
  27.   }
  28.  
  29.   def undo()
  30.   {
  31.     if (posInHist > 0)
  32.     {
  33.       if (hist(posInHist-1)._3)// add
  34.       {
  35.         text = text.substring(0,hist(posInHist-1)._1) + text.substring(hist(posInHist-1)._1+1, text.length)
  36.       }
  37.       else
  38.       {
  39.         text = text.substring(0,hist(posInHist-1)._1) + hist(posInHist-1)._2 + text.substring(hist(posInHist-1)._1, text.length)
  40.       }
  41.       posInHist -= 1
  42.     }
  43.   }
  44.  
  45.   def redo()
  46.   {
  47.     if (posInHist < hist.length )
  48.     {
  49.       if (hist(posInHist)._3)// add
  50.       {
  51.         text = text.substring(0,hist(posInHist)._1) + hist(posInHist)._2 + text.substring(hist(posInHist)._1, text.length)
  52.       }
  53.       else
  54.       {
  55.         text = text.substring(0,hist(posInHist)._1) + text.substring(hist(posInHist)._1+1, text.length)
  56.       }
  57.  
  58.       posInHist += 1
  59.     }
  60.   }
  61.  
  62. }
  63.  
  64. object test {
  65.   def main(args: Array[String])
  66.   {
  67.     var n = new notepad
  68.     n.addSym(0,'a')
  69.     n.addSym(1,'b')
  70.     n.addSym(2,'c')
  71.     println(n.text)
  72.     n.undo()
  73.     println(n.text)
  74.     n.redo()
  75.     println(n.text)
  76.     n.undo()
  77.     n.undo()
  78.     println(n.text)
  79.     n.redo()
  80.     println(n.text)
  81.     n.redo()
  82.     println(n.text)
  83.     println("---------------")
  84.  
  85.     n.delSym(1)
  86.     println(n.text)
  87.     n.undo()
  88.     println(n.text)
  89.     n.redo()
  90.     println(n.text)
  91.     n.addSym(2,'d')
  92.     println(n.text)
  93.     n.undo()
  94.     println(n.text)
  95.     n.undo()
  96.     println(n.text)
  97.   }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement