-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab_12.cpp
More file actions
79 lines (66 loc) · 1.87 KB
/
Copy pathLab_12.cpp
File metadata and controls
79 lines (66 loc) · 1.87 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
#include <iostream>
#include <string>
#include <stack>
#include <sstream>
#include <vector>
#include <algorithm>
class SimpleTextEditor {
private:
std::string currentText;
std::stack<std::string> history;
void saveState() {
history.push(currentText);
}
public:
SimpleTextEditor() : currentText("") {}
void insert(const std::string& value) {
saveState();
currentText.append(value);
}
void deleteChars(int count) {
if (count > 0 && !currentText.empty()) {
saveState();
size_t deleteLength = std::min((size_t)count, currentText.length());
currentText.erase(currentText.length() - deleteLength);
}
}
void get(int index) const {
if (index >= 0 && index < currentText.length()) {
std::cout << currentText[index] << "\n";
}
}
void undo() {
if (!history.empty()) {
currentText = history.top();
history.pop();
}
}
};
void solve() {
SimpleTextEditor editor;
int commandType;
while (std::cin >> commandType) {
if (commandType == 1) {
std::string value;
if (std::cin >> value) {
editor.insert(value);
}
} else if (commandType == 2) {
int count;
if (std::cin >> count) {
editor.deleteChars(count);
}
} else if (commandType == 3) {
int index;
if (std::cin >> index) {
editor.get(index - 1); // Adjust index to be 0-based for C++ string access
}
} else if (commandType == 4) {
editor.undo();
}
}
}
int main() {
solve();
return 0;
}