forked from maiquynhtruong/algorithms-and-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-editor-buffer.java
143 lines (140 loc) · 3.65 KB
/
text-editor-buffer.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
public class Buffer1() {
Stack<Character> stack1;
Stack<Character> stack2;
// create an empty buffer
public Buffer1() {
stack1 = new Stack<Character>();
stack2 = new Stack<Character>();
}
// insert c at the cursor position
public void insert(char c) {
stack1.push(c);
}
// character at the cursor position
public char get() {
return stack1.peek();
}
// delete and return the character at the cursor
// left delete, more like backspace
public char delete() {
return stack1.pop();
}
// move the cursor k positions to the left
public void left(int k) {
// assuming no stackoverflow
for (int i = 0; i < k; i++) {
stack2.push(stack1.pop())
}
}
// move the cursor k positions to the right
public void right(int k) {
for (int i = 0; i < k; i++) {
stack1.push(stack2.pop());
}
}
// number of characters in the buffer
public int size() {
return stack1.size() + stack2.size();
}
}
public class Buffer2() {
Stack<Character> leftStack;
Stack<Character> rightStack;
boolean reverse = true;
public Buffer2() {
leftStack = new Stack<Character>();
rightStack = new Stack<Character>();
}
public void insert(char c) {
leftStack.push(c);
}
public char get() {
return leftStack.peek();
}
public char delete() {
return rightStack.isEmpty() ? '\0' : rightStack.pop();
}
public void left(int k) {
while (!leftStack.isEmpty() && --k >= 0) rightStack.push(leftStack.pop());
}
public void right(int k) {
while (!rightStack.isEmpty() && --k >= 0) leftStack.push(rightStack.pop());
}
public int size() {
return rightStack.size() + leftStack.size();
}
public String serializeBuffer(Stack<Character> buffer) {
int size = buffer.size();
StringBuilder sb = new StringBuilder();
if (reverse) {
for (int i = 0; i < size; i++) {
sb.append(buffer.get(i));
}
reverse = !reverse;
} else {
for (int i = size - 1; i >= 0; i--) {
sb.append(buffer.get(i));
}
reverse = !reverse;
}
return sb.toString();
}
// '*' means the position of the cursor
public String toString() {
serializeBuffer(leftStack) + '*' + serializeBuffer(rightStack);
}
}
public class Editor() {
Buffer1 buffer = new Buffer1();
// Buffer2 buffer = new Buffer2();
Scanner sc = new Scanner(System.in);
System.out.println("This is a simple text editor.\n\n"
+ "\t- '*' represents the location of the cursor.\n"
+ "\t- Type any character and press enter to add it to the stream.\n"
+ "\nThe following is a list of commands. "
+ "\n\n\t+C\tAdd a special character (C) to the stream."
+ "\n\t-\tRemove a character from the stream."
+ "\n\t?\tGet information about the stream (i.e., the size)."
+ "\n\t!\tQuit the text editor"
+ "\n\t<#\tMove the cursor left by (#) number of places."
+ "\n\t>#\tMove the cursor right by (#) number of places.\n\n");
while (true) {
// System.out.println("\t" + buffer); // this is for buffer2
System.out.println(Arrays.toString(buffer.stack1.toArray()) + "*" + Arrays.toString(buffer.stack2.toArray())); // This is for buffer 1
System.out.print(">")
char c = '\0';
String s = "";
if (sc.hasNextLine()) {
s = sc.nextLine();
c = s.charAt(0);
}
switch (c) {
case '-':
buffer.delete();
break;
case '?':
System.out.println(buffer.size());
break;
case '!':
System.out.println("So long");
sc.close();
return;
case '<':
int arg = new Integer(s.substring(1, s.length()));
buffer.left(arg);
break;
case '>':
arg = new Integer(s.substring(1, s.length()));
buffer.right(arg);
break;
case '\0':
System.out.println("End of input");
return;
case '+':
c = s.charAt(1);
default:
buffer.insert(c);
break;
}
}
}