-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunredo.java
67 lines (66 loc) · 2.14 KB
/
unredo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.*;
class unredo
{
public static void main(String args[])
{
Stack<String> undostack = new Stack<>();
Stack<String> redostack = new Stack<>();
Stack<String> stack = new Stack<>();
Scanner in = new Scanner(System.in);
String word = "";
String text = "";
System.out.println(" Type '<' to undo a word.\n Type '>' to redo a undo.\n Type '-' to delete a word.\n Type '< >' to display the sentence.\n Type 'exit' to exit Text editor.");
//Iterates until the user doesn't type exit.
while(!word.equals("exit"))
{
word=in.next();
if(word.equals("<"))
undo(undostack,redostack);
else if(word.equals(">"))
redo(undostack,redostack,stack);
else if(word.equals("-"))
{
//redostack.push(undostack.peek());
System.out.println("'deleted'"+undostack.pop());
}
else
{
undostack.push(word);
}
}
}
//Function to undo the words one by one from very recent onwards.
static void undo(Stack<String> undostack,Stack<String> redostack)
{
if(!undostack.isEmpty())
{
String text = undostack.pop();
redostack.push(text);
System.out.println("'removed'"+text);
}
}
//Function to redo the words from undoed words.
static void redo(Stack<String> undostack ,Stack<String> redostack,Stack<String> stack)
{
String sentence=" ";
String temp;
if(!redostack.isEmpty())
{
while(!undostack.isEmpty())
{
String text = undostack.pop();
stack.push(text);
}
while(!stack.isEmpty())
{
String text = stack.pop();
sentence +=text + " ";
undostack.push(text);
}
sentence+=redostack.peek();
System.out.println("'added'"+redostack.peek());
undostack.push(redostack.pop());
System.out.println(sentence);
}
}
}