-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexteditor.cpp
More file actions
147 lines (128 loc) · 3.13 KB
/
texteditor.cpp
File metadata and controls
147 lines (128 loc) · 3.13 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
#include<bits/stdc++.h>
#include<stack>
#include<string>
//using namespace std;
class texteditor{
private:
std::stack<char> leftstack, rightstack;
void moveleft(size_t position);
void moveright(size_t position);
public:
void insertcharacter(char );
void insertword(char word[]);
void movecursor(size_t position);
bool deletechar();
bool backspacechar();
void findandreplacechar(char findwhat, char replacewith);
void examinetop();
};
signed main(){
int i =0;
char text[80];
texteditor txt;
std::cout<<"Enter the word to be inserted : \n";
std::cin.getline(text,80);
txt.insertword(text);
std::cout<<"Inserting word \n";
txt.examinetop();
std::cout<<"Move the cursor to position 14\n";
txt.movecursor(14);
txt.examinetop();
txt.movecursor(25);
txt.examinetop();
for(auto i = 0; i<3 ; i++){
if(txt.deletechar())
std::cout<<"There is nothing to delete in the stack\n";
else
txt.deletechar();
txt.examinetop();
}
return 0;
}
void texteditor::insertcharacter(char x){
leftstack.push(x);
return;
}
void texteditor::insertword(char word[]){
int i = 0;
while(word[i]!= '\0'){
insertcharacter(word[i]);
i++;
}
return;
}
void texteditor::examinetop(){
if(leftstack.empty())
std::cout<<"The leftstack is empty\n";
else
std::cout<<"The element at the end of the stack is '"<<leftstack.top()<<"'\t"
<<leftstack.size()<<"\n";
if(rightstack.empty())
std::cout<<"The rightstack is empty\n";
else
std::cout<<"The element at the end of the stack is '"<<rightstack.top()<<"'\n";
return;
}
void texteditor::moveleft(size_t position){
int leftsize;
leftsize = leftstack.size();
//rightsize = rightstack.size();
while(position!=leftsize){
rightstack.push(leftstack.top());
leftstack.pop();
leftsize--;
}
}
void texteditor::moveright(size_t position){
position = position-leftstack.size();
int rightsize = rightstack.size();
while(position!=rightsize){
leftstack.push(rightstack.top());
rightstack.pop();
rightsize = rightstack.size();
//position--;
}
}
bool texteditor::backspacechar(){
if(leftstack.empty())
return false;
else{
leftstack.pop();
return true;
}
}
bool texteditor::deletechar(){
if(rightstack.empty())
return false;
else{
rightstack.pop();
return true;
}
}
void texteditor::findandreplacechar(char findwhat, char replacewith){
int originialcurpos = leftstack.size();
int count = 1;
movecursor(0);
while(!rightstack.empty()){
if(findwhat==rightstack.top()){
deletechar();
insertcharacter(replacewith);
}
else
movecursor(count);
count++;
}
movecursor(originialcurpos);
}
void texteditor::movecursor(size_t position){
int count = 0;
auto leftsize = leftstack.size();
auto rightsize = rightstack.size();
if (position < leftsize){
moveleft(position);
}
else{
count = position - leftsize;
moveright(position);
}
}