Skip to content

Commit e57a8ad

Browse files
authored
lab_4
Solución al laboratorio 4
1 parent b0a4fc4 commit e57a8ad

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed

lab_4.cpp

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <iomanip>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
//declaracion------------------------------------
7+
struct _dato{
8+
int n;
9+
char palabra[25];
10+
};
11+
typedef _dato dato;
12+
struct nodo{
13+
dato info;
14+
nodo *ant, *sig;
15+
};
16+
typedef nodo *ptnodo;
17+
ptnodo lista_d = NULL;
18+
//funiones----------------------------------------
19+
void INSERTAR_FIN(ptnodo &Ld, dato X);
20+
ptnodo ULTIMO(ptnodo &Ld);
21+
void MOSTRAR_LISTA (ptnodo &Lista);
22+
void BORRAR_LISTA(ptnodo &Ld);
23+
void buscar_pal(ptnodo Ld, char pal[]);
24+
using namespace std;
25+
int main(){
26+
ifstream A1;
27+
char aux[25], let, pal[25];
28+
int i, flag = 0, cont = 0,opc = 0, ban = 0;
29+
dato regis;
30+
while(opc != 6){
31+
cout << "Menu de opciones\n";
32+
cout << "1.- Cargar un archivo de texto\n";
33+
cout << "2.- Mostrar la lista de palabras\n";
34+
cout << "3.- Agregar datos\n";
35+
cout << "4.- Buscar palabra\n";
36+
cout << "5.- Opcion extra\n";
37+
cout << "6.- Salir\n";
38+
cin >> opc;
39+
system("cls");
40+
switch(opc){
41+
case 1:
42+
cont = 0;
43+
if(lista_d != NULL)
44+
BORRAR_LISTA(lista_d);
45+
A1.open("texto.txt");
46+
if (!A1){
47+
cout << "Problemas con archivo\n";
48+
break;
49+
}
50+
A1.get(let);
51+
while(! A1.eof()){
52+
i = 0;
53+
flag = 0;
54+
while(isalpha(let)){
55+
aux[i] = let;
56+
A1.get(let);
57+
i ++;
58+
flag = 1;
59+
}
60+
if(flag == 1){
61+
aux [i] = '\0';
62+
cont ++;
63+
regis.n = cont; strcpy(regis.palabra,aux);
64+
INSERTAR_FIN(lista_d, regis);
65+
}
66+
A1.get(let);
67+
}
68+
ban = 1;
69+
A1.close();
70+
break;
71+
case 2:
72+
if (ban){
73+
MOSTRAR_LISTA (lista_d);
74+
}else
75+
cout << "Se deben cargar los datos primero\n";
76+
break;
77+
case 3:
78+
break;
79+
case 4:
80+
cout << "ingrese la palabra a buscar\n";
81+
cin.ignore(1);
82+
cin.getline(pal, 25);
83+
buscar_pal(lista_d, pal);
84+
break;
85+
case 5:
86+
break;
87+
case 6:
88+
if(lista_d != NULL)
89+
BORRAR_LISTA(lista_d);
90+
break;
91+
default:
92+
cout << "Ingrese una opcion valida\n";
93+
break;
94+
}
95+
}
96+
return 0;
97+
}
98+
void INSERTAR_FIN(ptnodo &Ld, dato X){
99+
ptnodo NUEVO, U;
100+
NUEVO = new(nodo);
101+
NUEVO -> info = X;
102+
NUEVO -> sig = NULL; NUEVO -> ant = NULL;
103+
if(Ld == NULL) Ld = NUEVO;
104+
else
105+
{ U = ULTIMO(Ld);
106+
U -> sig = NUEVO;
107+
NUEVO -> ant = U;
108+
}
109+
}
110+
ptnodo ULTIMO(ptnodo &Ld){
111+
ptnodo A = Ld;
112+
if(A != NULL){
113+
while (A -> sig != NULL)
114+
A = A -> sig;
115+
return A;
116+
}else {
117+
cout << "Lista vacia\n";
118+
return NULL; }
119+
}
120+
void MOSTRAR_LISTA (ptnodo &Lista){
121+
ptnodo A = Lista;
122+
if (A != NULL){
123+
while(A != NULL){
124+
cout << setw(3) << A -> info.n << setw(20) << A -> info.palabra << endl;
125+
A = A -> sig;
126+
}
127+
}
128+
else cout << "Lista vacia\n";
129+
}
130+
void BORRAR_LISTA(ptnodo &Ld){
131+
ptnodo P = Ld;
132+
if(Ld != NULL){
133+
if(P == Ld && P -> sig == NULL)
134+
Ld = NULL;
135+
else{
136+
Ld = Ld -> sig;
137+
Ld -> ant = NULL;
138+
}
139+
delete(P);
140+
BORRAR_LISTA(Ld);
141+
}
142+
}
143+
void buscar_pal(ptnodo Ld, char pal[]){
144+
ptnodo A = Ld, P[15];
145+
int b = 0, a = 4, i = 0;
146+
for(i = 0; i < b; i++)
147+
P[i] = NULL;
148+
if(A != NULL){
149+
while(A != NULL){
150+
a = strcmp(pal, A -> info.palabra);
151+
if(!a){
152+
b ++;
153+
P[i] = A;
154+
i++;
155+
}
156+
A = A -> sig;
157+
}
158+
}else{
159+
cout << "La lista esta vacia\n";
160+
}
161+
if (b){
162+
cout << "Palabra encontrada\n";
163+
for(i = 0; i < b; i++){
164+
cout << setw(3) << P[i] -> info.n << setw(20) << P[i] -> info.palabra << endl;
165+
P[i] = NULL;
166+
}
167+
}else
168+
cout << "No hay coincidencia\n";
169+
}

0 commit comments

Comments
 (0)