-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTextEditor.cpp
More file actions
165 lines (137 loc) · 4.21 KB
/
TextEditor.cpp
File metadata and controls
165 lines (137 loc) · 4.21 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <stack>
using namespace std;
class textEditor {
private:
stack<char> leftStack; //Left stack
stack<char> rightStack; //Right stack
public:
void insertWord(char word[]);
void insertCharacter(char character);
bool deleteCharacter();
bool backSpaceCharacter();
void moveCursor(int position);
void moveLeft(int position);
void moveRight(int position);
void findAndReplaceChar(char findWhat, char replaceWith);
void examineTop();
}; //End of class
void textEditor::examineTop(){
if(leftStack.empty())
cout << "leftStack: empty\t";
else
cout << "leftStack: " << leftStack.top() << "," << leftStack.size() << "\t\t";
if(rightStack.empty())
cout << "rightStack: empty\n";
else
cout << "rightStack: " << rightStack.top() << "," << rightStack.size() << endl;
} //End of function
void textEditor::insertWord(char word[]) {
int i=0;
while(word[i]!='\0') {
insertCharacter(word[i]);
i++;
}
} //End of function
void textEditor::insertCharacter(char character){
leftStack.push(character);
} //End of function
bool textEditor::deleteCharacter(){
if (rightStack.empty())
return false;
else
rightStack.pop();
return true;
}//End of function
bool textEditor::backSpaceCharacter(){
if (leftStack.empty())
return false;
else
leftStack.pop();
return true;
}//End of function
void textEditor::moveCursor(int position){
int leftSize, rightSize, count;
leftSize = leftStack.size();
rightSize = rightStack.size();
if (position < leftSize)
moveLeft(position);
else {
count = position - leftSize;
moveRight(count);
}
}//End of function
void textEditor::moveLeft(int position){
int leftSize;
leftSize = leftStack.size();
while(position!=leftSize) {
rightStack.push(leftStack.top());
leftStack.pop();
leftSize = leftStack.size();
}
}//End of function
void textEditor::moveRight(int count){
int rightSize, i=1;
rightSize = rightStack.size();
if (count > rightSize)
cout << "Cannot move the cursor, right, to the specified position";
else {
while(i<=count) {
leftStack.push(rightStack.top());
rightStack.pop();
i++;
} //End of while
} //End of else
}//End of function
void textEditor::findAndReplaceChar(char findWhat, char replaceWith){
int count=1, originalCursorPoistion = leftStack.size();
moveCursor(0); //Move characters from left stack to right stack
//Move characters from right stack to left stack and examine
while(!rightStack.empty()) {
if(rightStack.top()==findWhat) {
deleteCharacter();
insertCharacter(replaceWith);
}
else
moveCursor(count);
count++;
} //End of while
moveCursor(originalCursorPoistion); //Retain the original cursor position
} //End of function
int main() {
int i=0; char text[80];
textEditor txt;
cout << "Enter the word to be inserted: ";
cin.getline(text,80);
txt.insertWord(text);
cout << "Inserting the word:";
txt.examineTop();
cout << "Enter the word to be inserted: ";
cin.getline(text,80);
txt.insertWord(text);
cout << "Inserting the word:";
txt.examineTop();
cout << "Move cursor to the position 14: ";
txt.moveCursor(14); txt.examineTop();
cout << "Move cursor to the position 17: ";
txt.moveCursor(17);
txt.examineTop();
for(i=0;i<3;i++){
if(!txt.deleteCharacter())
cout << "There is nothing to delete. Move your cursor accordingly to delete the character";
else
cout << "Delete characters using DEL: ";
txt.examineTop();
}
for(i=0;i<7;i++){
if(!txt.backSpaceCharacter())
cout << "There is nothing to delete. Move your cursor accordingly to delete the character";
else
cout << "Delete characters using Backspace: ";
txt.examineTop();
}
txt.findAndReplaceChar('t','T');
cout << "Replace occurrences of t with T: ";
txt.examineTop();
return 0;
} //End of main