Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class notepad
- {
- var hist = List[Tuple3[Int, Char, Boolean]]() // actions. int - pos, char - sym (added or deleted), boolean - true, if action = adding
- var posInHist = 0 // actions 0, 1, ..., posInHist-1 from hist applied to "" will give text
- var text = "" // current text
- def addSym(pos: Int, sym: Char)
- {
- if (posInHist < hist.length )
- hist = hist.slice(0,posInHist)
- text = text.substring(0, pos) + sym + text.substring(pos,text.length)
- hist = hist ::: (pos, sym, true) :: Nil
- posInHist += 1
- }
- def delSym(pos: Int)
- {
- if (posInHist < hist.length )
- hist = hist.slice(0,posInHist)
- var sym: Char = text(pos)
- text = text.substring(0, pos) + text.substring(pos+1,text.length)
- hist = hist ::: (pos, sym, false) :: Nil
- posInHist += 1
- }
- def undo()
- {
- if (posInHist > 0)
- {
- if (hist(posInHist-1)._3)// add
- {
- text = text.substring(0,hist(posInHist-1)._1) + text.substring(hist(posInHist-1)._1+1, text.length)
- }
- else
- {
- text = text.substring(0,hist(posInHist-1)._1) + hist(posInHist-1)._2 + text.substring(hist(posInHist-1)._1, text.length)
- }
- posInHist -= 1
- }
- }
- def redo()
- {
- if (posInHist < hist.length )
- {
- if (hist(posInHist)._3)// add
- {
- text = text.substring(0,hist(posInHist)._1) + hist(posInHist)._2 + text.substring(hist(posInHist)._1, text.length)
- }
- else
- {
- text = text.substring(0,hist(posInHist)._1) + text.substring(hist(posInHist)._1+1, text.length)
- }
- posInHist += 1
- }
- }
- }
- object test {
- def main(args: Array[String])
- {
- var n = new notepad
- n.addSym(0,'a')
- n.addSym(1,'b')
- n.addSym(2,'c')
- println(n.text)
- n.undo()
- println(n.text)
- n.redo()
- println(n.text)
- n.undo()
- n.undo()
- println(n.text)
- n.redo()
- println(n.text)
- n.redo()
- println(n.text)
- println("---------------")
- n.delSym(1)
- println(n.text)
- n.undo()
- println(n.text)
- n.redo()
- println(n.text)
- n.addSym(2,'d')
- println(n.text)
- n.undo()
- println(n.text)
- n.undo()
- println(n.text)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement