-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack_simple_text_editor_array.c
115 lines (95 loc) · 1.61 KB
/
stack_simple_text_editor_array.c
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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
char newstr[100000];
int op_history[1000000];
int arg_history[1000000];
int op_top;
char front[1000000];
int front_top;
char back[1000000];
int back_top;
void current(void)
{
struct one_char *pos;
int i;
printf("front: ");
for (i = 0; i < front_top; i++)
printf("%c", front[i]);
printf("\n");
printf("back: ");
for (i = 0; i < back_top; i++)
printf("%c", back[i]);
printf("\n");
for (i = 0; i < op_top; i++)
printf("%d %d\n", op_history[i], arg_history[i]);
}
void add_str(char *str)
{
op_history[op_top] = 1;
arg_history[op_top] = strlen(str);
op_top++;
// add at the end of front
while (*str != '\0') {
front[front_top++] = *str++;
}
}
void del_str(int count)
{
op_history[op_top] = 2;
arg_history[op_top] = count;
op_top++;
for (; count > 0; count--) {
front_top--;
back[back_top++] = front[front_top];
}
}
void print_char(int index)
{
printf("%c\n", front[index - 1]);
}
void revert(void)
{
int arg;
int op;
op_top--;
arg = arg_history[op_top];
op = op_history[op_top];
if (op == 1) {
front_top -= arg;
} else if (op == 2) {
for (; arg > 0; arg--)
front[front_top++] = back[--back_top];
}
}
int main(void)
{
int num_op;
int i;
int op;
int arg;
scanf("%d", &num_op);
for (i = 0; i < num_op; i++) {
scanf("%d", &op);
switch (op) {
case 1:
scanf("%s", newstr);
add_str(newstr);
break;
case 2:
scanf("%d", &arg);
del_str(arg);
break;
case 3:
scanf("%d", &arg);
print_char(arg);
break;
case 4:
revert();
break;
}
//current();
}
return 0;
}