-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.cpp
138 lines (118 loc) · 2.4 KB
/
hash.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
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
#include <cstdlib>
#include <iostream>
#include <string>
#include "hash.h"
using namespace std;
hash::hash(){
for(int i = 0; i <tableSize; i++){
HashTable[i] = new item;
HashTable[i]->user = "empty";
HashTable[i]->user_id = "empty";
HashTable[i]->pass = "empty";
HashTable[i]->next = NULL;
}
}
int hash::usuario_valido(string user){
int response = 2;
int index = clave_de_Hash(user);
item* Ptr = HashTable[index];
while(Ptr->next != NULL){
if(Ptr->user == user){
response = 1;
Ptr = Ptr->next;
}else{
Ptr = Ptr->next;
}
}
if(response > 1){
if(Ptr->user == user){
response = 1;
}else{
response = 0;
}
}
return response;
}
int hash::verificar_clave(string user, string password){
int index = clave_de_Hash(user);
item* Ptr = HashTable[index];
while(Ptr->next != NULL){
if(Ptr->user == user){
if(Ptr->pass == password){
return 1;
}else{
return 0;
}
}
}
}
void hash::crear_usuario(string user, string password){
int index = clave_de_Hash(user);
if(usuario_valido(user)){
cout << "nombre de usuario ya existente: " << user <<endl;
}else{
if(HashTable[index]->user == "empty"){
HashTable[index]->user = user;
HashTable[index]->pass = password;
}
else{
item* Ptr = HashTable[index];
item* n = new item;
n->user = user;
n->pass = password;
n->next = NULL;
while (Ptr->next != NULL)
{
Ptr = Ptr->next;
}
Ptr->next = n;
}
}
}
void hash::eliminar_usuario(string user){
if(usuario_valido(user)){
int index = clave_de_Hash(user);
item* Ptr = HashTable[index];
if(Ptr->user == user){
HashTable[index] = Ptr->next;
delete(Ptr);
}else{
while(Ptr->next->user != user){
Ptr = Ptr->next;
}
item* PtrAux = Ptr->next->next;
Ptr->next->next = NULL;
delete(Ptr->next);
Ptr->next = PtrAux;
}
}else{
cout << "el usuario no puede ser eliminado porque no existe" << endl;
}
}
void hash::ver_usuarios(){
int number;
cout << "----------------------------\n";
cout << "Lista de usuarios : \n";
for (int i = 0; i < tableSize; i++)
{
item* Ptr = HashTable[i];
while( Ptr->next != NULL){
cout << Ptr->user << endl;
Ptr = Ptr->next;
}
if(Ptr->user != "empty"){
cout << Ptr->user << endl;
}
}
};
int hash::clave_de_Hash(string key)
{
int hash = 0;
int index;
for (int i = 0; i < key.length(); i++)
{
hash = hash + (int)key[i];
}
index = hash % tableSize;
return index;
}