-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
159 lines (136 loc) · 3.94 KB
/
main.cpp
File metadata and controls
159 lines (136 loc) · 3.94 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
148
149
150
151
152
153
154
155
156
157
158
159
#include <fstream>
#include <iostream>
#include <vector>
#include "listas.hpp"
using namespace std;
int main()
{
fstream arch_in;
arch_in.open("entrada.txt");
if (!arch_in) {
cout << "Error al inicializar el archivo entrada.txt" << endl;
return 1;
}
fstream arch_out;
arch_out.open("salida.txt");
if (!arch_out) {
cout << "Error al inicializar el archivo salida.txt" << endl;
return 1;
}
int size = 14;
char sb[size]{}; //search buffer
char lb[size]{}; //lock-ahead buffer
Nodo<char> *window = nullptr;
// arch_in >> sb[0];
for (int i = 0; arch_in >> lb[i] && i < size; i++) {
agregar(window, lb[i]);
} //Carga inicial del lb y sw
arch_in.seekg(size, ios::beg);
// arch_out << "< " << 0 << "; " << 0 << "; " << sb[0] << " >" << endl;a
char letra;
int length = 0;
int offset = 0;
char ltr_fin;
int j = 0;
int i = 0;
int d = 0;
int cp = 0;
Nodo<char> *aux = window;
for (cp;lb[0] != '\0'; cp++) {
for (i = 0; i < size; i++) {
if (sb[i] == lb[j]) {
offset = i;
length = j;
while (sb[i++] == lb[j++]) {
length = j;
if (sb[i] == '\0') {
i--;
}
}
break;
}
}
if (length == size) {
length--;
ltr_fin = lb[length];
} else {
ltr_fin = lb[length];
}
arch_out << length << " " << offset << " " << ltr_fin << endl;
if (cp < size) {
for (int i = 0; i < length+1 && arch_in.get(letra); i++) {
agregar(window, letra);
}
if(length +1 >= size) {
pop(window);
}
Nodo<char> *aux = window;
if (length +1 < size) {
for (int i = 0; i < d; i++) {
aux = aux->sig;
}
}
for (int i = 0; i < length+1; i++) {
sb[d] = aux->dato;
aux = aux->sig;
d++;
}
for (int i = 0; i < size; i++) {
lb[i] = aux->dato;
aux = aux->sig;
}
} else {
for (int i = 0; i < length+1; i++) {
if (!(arch_in >> letra)) {
pop(window);
agregar(window, '\0');
} else {
pop(window);
agregar(window, letra);
}
}
Nodo<char> *aux = window;
for (int i = 0; i < size; i++) {
sb[i] = aux->dato;
aux = aux->sig;
}
for (int i = 0; i < size; i++) {
lb[i] = aux->dato;
aux = aux->sig;
}
}
cp += length;
length = 0;
j = 0;
}
arch_out.seekg(0, ios::beg);
char texto[cp]{};
for (int indice = 0; arch_out >> length >> offset >> ltr_fin; indice++) {
if (indice < size && indice != 0) {
for (int i = 0; i < length; i++) {
letra = texto[i+offset];
texto[indice] = letra;
indice++;
}
} else {
for (int i = 0; i < length; i++) {
if (offset > length) {
letra = texto[(indice-size)+offset-i];
texto[indice] = letra;
indice++;
} else {
letra = texto[(indice-size)+offset];
texto[indice] = letra;
indice++;
}
}
}
texto[indice] = ltr_fin;
}
for (int i = 0; i < cp; i++) {
cout << texto[i];
}
arch_in.close();
arch_out.close();
return 0;
}