-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC_Simple_Text_Editor.cpp
68 lines (63 loc) · 1.7 KB
/
C_Simple_Text_Editor.cpp
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
#include <stack>
#include <map>
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const int addition = 1;
const int deletion = 2;
const int printing = 3;
const int undoing = 4;
int main()
{
stack<pair<int, string>> operationsPairs;
int n;
cin >> n;
int arr[n];
string res;
for (int i = 0; i < n; i++)
{
int operation;
cin >> operation;
if (operation == 1)
{
string input;
cin >> input;
pair<int, string> currentJob;
currentJob.first = 1;
currentJob.second = input;
operationsPairs.push(currentJob);
res += input;
}
else if (operation == deletion)
{
int ToDelete;
cin >> ToDelete;
string deleted;
deleted = res.substr(res.size() - ToDelete, ToDelete);
pair<int, string> currentJob;
currentJob.first = 2;
currentJob.second = deleted;
operationsPairs.push(currentJob);
res.erase(res.size() - ToDelete, ToDelete);
}
else if (operation == printing)
{
int idx;
cin >> idx;
cout << res[idx-1] << endl;
}
else if (operation == undoing)
{
if(!operationsPairs.empty()) {
if(operationsPairs.top().first == 1) {
res = res.substr(0, res.size() - operationsPairs.top().second.size());
operationsPairs.pop();
} else {
res += operationsPairs.top().second;
operationsPairs.pop();
}
}
}
}
}