-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleTextEditor.cpp
More file actions
64 lines (57 loc) · 1.44 KB
/
SimpleTextEditor.cpp
File metadata and controls
64 lines (57 loc) · 1.44 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
// SimpleTextEditor.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <stack>
#include <iostream>
using namespace std;
class TextEditor {
private:
string text;
stack<string> reverseMemory;
public:
void append(string toAppend) {
string newMemory;
newMemory += "2";
newMemory += to_string(toAppend.size());
reverseMemory.push(newMemory);
text += toAppend;
}
void deleet(int numOfChars) {//if I can't use delete then you'll no longer be 1337
string newMemory;
newMemory += "1";
newMemory += text.substr(text.size() - numOfChars);//should get <numOfChars> last elements
reverseMemory.push(newMemory);
text = text.substr(0, text.size() - numOfChars);
}
void print(int pos) {
cout << text[pos-1] << endl;
}
void undo() {
if (reverseMemory.top()[0] == '1') {
text += reverseMemory.top().substr(1);
reverseMemory.pop();
}
else {
text = text.substr(0, text.size() - stoi(reverseMemory.top().substr(1)));
reverseMemory.pop();
}
}
};
int main(){
string inputString;
int numberOfQueries, input;
TextEditor editor1;
cin >> numberOfQueries;
for (int i = 0; i < numberOfQueries; i++) {
cin >> input;
switch (input) {
case 1: cin >> inputString; editor1.append(inputString); break;
case 2: cin >> input; editor1.deleet(input); break;
case 3: cin >> input; editor1.print(input); break;
case 4: editor1.undo(); break;
}
}
system("pause");
return 0;
}