200, suma posicional, redispersión lineal

This commit is contained in:
2025-10-29 15:20:06 +01:00
parent 6bb476f53e
commit 39bcfbac80
8 changed files with 128 additions and 77 deletions

BIN
200.tar Normal file

Binary file not shown.

View File

@@ -1,7 +1,7 @@
GPP = /usr/bin/g++
OPTS = -Wall -Wno-deprecated -std=c++17 -g
a.out: diccionariocuacs.o cuac.o fecha.o
$(GPP) $(OPTS) main.cpp diccionariocuacs.o cuac.o fecha.o
OPTS = -Wall -Wno-deprecated -std=c++17 -O2
a.out: diccionariocuacs.o cuac.o fecha.o tablahash.o
$(GPP) $(OPTS) main.cpp tablahash.o cuac.o fecha.o diccionariocuacs.o
fecha.o: fecha.cpp fecha.hpp
$(GPP) $(OPTS) -c fecha.cpp
@@ -9,7 +9,12 @@ fecha.o: fecha.cpp fecha.hpp
cuac.o: cuac.hpp cuac.cpp fecha.hpp
$(GPP) $(OPTS) -c cuac.cpp
diccionariocuacs.o: diccionariocuacs.hpp diccionariocuacs.cpp cuac.hpp fecha.hpp
tablahash.o: cuac.hpp fecha.hpp tablahash.cpp tablahash.hpp
$(GPP) $(OPTS) -c tablahash.cpp
diccionariocuacs.o: diccionariocuacs.cpp diccionariocuacs.hpp cuac.hpp fecha.hpp tablahash.hpp
$(GPP) $(OPTS) -c diccionariocuacs.cpp
clean:
rm *.o a.out

View File

@@ -37,7 +37,7 @@ const string pcuac[30] = {"Afirmativo.",
class Cuac {
private:
friend class DiccionarioCuacs;
friend class TablaHash;
string usuario;
Fecha fecha;
string mensaje;

View File

@@ -1,65 +1,22 @@
#include "diccionariocuacs.hpp"
DiccionarioCuacs::DiccionarioCuacs() {
contador = 0;
DiccionarioCuacs::DiccionarioCuacs(int m) {
TablaHash th = TablaHash(m);
this -> tabla = th;
}
void DiccionarioCuacs::insertar(Cuac nuevo) {
list<Cuac>::iterator it = lista.begin();
while (it != lista.end() && nuevo.comparar(*it)){
it++;
}
if (it==lista.end() || !nuevo.comparar(*it)) {
lista.insert(it--, nuevo);
it++;
}
contador++;
tabla.insertar(nuevo);
}
void DiccionarioCuacs::last(int n){
cout << "last " << n << endl;
list<Cuac>::iterator it = lista.begin();
int i = 0;
while (it != lista.end() && n--) {
Cuac c = *it;
i++;
cout << i << ". ";
c.escribir();
it++;
}
cout << "Total: " << i << " cuac" << endl;
}
void DiccionarioCuacs::follow(string nombre){
cout << "follow " << nombre << endl;
list<Cuac>::iterator it;
int i = 0;
for (it = lista.begin(); it != lista.end(); it++) {
Cuac c = *it;
if (c.usuario == nombre) {
i++;
cout << i << ". ";
c.escribir();
}
}
cout << "Total: " << i << " cuac" << endl;
}
void DiccionarioCuacs::date(Fecha f1, Fecha f2){
cout << "date ";
f1.escribir();
cout << ' ';
f2.escribir();
cout << '\n';
int i = 0;
list<Cuac>::iterator it;
for (it = lista.begin(); it != lista.end(); it++) {
Cuac c = *it;
if ((f1.es_menor(c.fecha) && !f2.es_menor(c.fecha)) || f1.es_igual(c.fecha) || f2.es_igual(c.fecha)) {
i++;
cout << i << ". ";
c.escribir();
}
}
cout << "Total: " << i << " cuac" << endl;
tabla.consultar(nombre);
}
int DiccionarioCuacs::elem() {
return tabla.elem();
}
DiccionarioCuacs::~DiccionarioCuacs() {
delete[] tabla.lista;
}

View File

@@ -1,22 +1,19 @@
#pragma once
#include "fecha.hpp"
#include "cuac.hpp"
#include <list>
#include "tablahash.hpp"
#include <string>
#include <iostream>
using namespace std;
class DiccionarioCuacs {
private:
list<Cuac> lista;
int contador;
TablaHash tabla;
public:
DiccionarioCuacs();
DiccionarioCuacs(int m);
~DiccionarioCuacs();
void insertar(Cuac nuevo);
void last(int n);
void follow(string nombre);
void date(Fecha f1, Fecha f2);
int numElem() { return contador; }
int elem();
};

View File

@@ -4,22 +4,23 @@
#include "fecha.hpp"
#include "cuac.hpp"
#include "diccionariocuacs.hpp"
#include "tablahash.hpp"
using namespace std;
DiccionarioCuacs dic;
DiccionarioCuacs dic = DiccionarioCuacs(20000);
void procesar_pcuac() {
Cuac nuevo;
nuevo.leer_pcuac();
dic.insertar(nuevo);
cout << dic.numElem() << " cuac" << endl;;
cout << dic.elem() << " cuac" << endl;
}
void procesar_mcuac() {
Cuac nuevo;
nuevo.leer_mcuac();
dic.insertar(nuevo);
cout << dic.numElem() << " cuac" << endl;
cout << dic.elem() << " cuac" << endl;
}
void procesar_follow() {
@@ -31,14 +32,12 @@ void procesar_follow() {
void procesar_last() {
int n;
cin >> n;
dic.last(n);
}
void procesar_date() {
Fecha f1, f2;
f1.leer();
f2.leer();
dic.date(f1, f2);
}
int main() {
@@ -51,12 +50,10 @@ int main() {
} else if (comando == "mcuac") {
procesar_mcuac();
cuacs++;
} else if (comando == "last") {
procesar_last();
} else if (comando == "follow") {
procesar_follow();
} else if (comando == "date") {
procesar_date();
} else if (comando == "exit") {
break;
}
}

62
tablahash.cpp Normal file
View File

@@ -0,0 +1,62 @@
#include "tablahash.hpp"
TablaHash::TablaHash(int M) {
nElem = 0;
this -> M = M;
this -> lista = new list<Cuac>[M];
}
TablaHash::TablaHash() {
}
TablaHash::~TablaHash() {
}
// eeeeeeeeeeeeeeeeeeeh
void TablaHash::insertar(Cuac nuevo) {
int pos = h(nuevo.usuario);
list<Cuac>::iterator it = lista[pos].begin();
while (it != lista[pos].end() && nuevo.comparar(*it)){
it++;
}
if (it==lista[pos].end() || !nuevo.comparar(*it)) {
lista[pos].insert(it--, nuevo);
it++;
}
nElem++;
}
// eeeeeeeeeeeeeeeeh
// hay que consultar de más reciente a más antiguo
void TablaHash::consultar(string clave) {
int pos = h(clave);
int i = 0;
for (list<Cuac>::iterator it = lista[pos].begin(); it != lista[pos].end(); it++) {
Cuac c = *it;
i++;
cout << i << ". " << c.usuario << " ";
c.fecha.escribir();
cout << '\n' << " " << c.mensaje << endl;
}
cout << "Total: " << i << " cuac" << endl;
}
// probamos suma posicional
// necesita redispersión
// probamos lineal
unsigned int TablaHash::h(string clave) {
unsigned int res = 0;
for (int i = 0; i < clave.length(); i++) {
res = 67 * res + clave[i];
}
while (!lista[res % M].empty()) {
Cuac c = lista[res % M].front();
if (clave != c.usuario) {
res = res + 1;
} else {
return res % M;
}
}
return res % M;
}

33
tablahash.hpp Normal file
View File

@@ -0,0 +1,33 @@
#pragma once
#include <list>
#include "cuac.hpp"
#include "fecha.hpp"
#include <iostream>
#include <string>
using namespace std;
class TablaHash {
private:
friend class DiccionarioCuacs;
int nElem;
int M;
list<Cuac> *lista;
public:
// implementar dispersión abierta
// tamaño variable (memoria dinámica)
// probar funciones de dispersión
// suma posicional
// posicional por trozos
// extracción
// etc
TablaHash();
TablaHash(int M);
~TablaHash();
void insertar(Cuac nuevo);
void consultar(string nombre);
unsigned int h(string clave);
// unsigned int h_spt(string clave);
int elem() { return nElem; }
};