-
Notifications
You must be signed in to change notification settings - Fork 16
/
Лаба2.cpp
145 lines (125 loc) · 2.59 KB
/
Лаба2.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
139
140
141
142
143
144
145
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstdio>
#define HASHTABLE_size 52
using namespace std;
struct Node
{
string value;
int count;
Node *next;
}**hashtable;
void Create_TAB()
{
hashtable = new Node * [HASHTABLE_size];
for (int i = 0; i < HASHTABLE_size; i++)
hashtable[i] = NULL;
}
void Remove_TAB()
{
for(int i = 0;i < HASHTABLE_size; i++)
if(hashtable[i] != NULL)
{
Node *q;
while(hashtable[i] != NULL)
{
q = hashtable[i];
hashtable[i] = hashtable[i]->next;
delete q;
q = NULL;
}
}
delete [] hashtable;
}
unsigned int hash_index (string value)
{
int sum = 0;
for(int i=0 ;i <value.length(); i++)
sum +=value[i]-'0';
return (sum % HASHTABLE_size);
}
Node *Search_TAB (string value)
{
Node *p;
p = hashtable[hash_index(value)];
while (p && p->value != value)
p = p ->next;
if( !p )
return NULL;
return p;
}
void Insert_TAB (string value)
{
Node *search = Search_TAB(value);
if (search == NULL)
{
Node *cur = new Node;
int index = hash_index(value);
if (cur != NULL)
{
cur->value = value;
cur->count = 1;
cur->next = hashtable[index];
hashtable[index] = cur;
}
}
else
search->count++;
}
void Printf_TAB()
{
cout << "The resulting hash function:" << endl;
int sum = 0;
for (int i = 0; i < HASHTABLE_size; i++)
if(hashtable[i] != NULL)
{
Node *q;
while(hashtable[i]!=NULL)
{
q = hashtable[i];
sum += q->count;
cout << q-> value << " = " << q->count << endl;
hashtable[i]= hashtable[i]->next;
}
}
cout << "The total amount: " << sum << endl;
}
int main()
{
bool t = true;
int n = 127, m = 18, i;
char text[n];
Create_TAB();
char signs[] = ",.:;-()=+/*'><{}[]";
ifstream fin ("input.txt");
if (!fin)
{
cerr << "Error opening file" << endl;
}
while (fin >> text)
{
if ((text[0] >= 'a'&& text[0] <= 'z') || (text[0] >= 'A' && text[0] <= 'Z'))
{
for (i = 0; text[i] != '\0'; i++);
if (text[i-1 ] == ';' || text[i-1 ] == '.' || text[i-1 ] == ':' || text[i-1 ] == ',')
text[i-1]='\0';
for (int l = 0; text[l] != '\0'; l++)
for (int j = 0; j < m ; j++)
if (text[l] == signs[j])
{
t = false;
break;
}
}
else
t = false;
if (t)
Insert_TAB(text);
t = true;
}
Printf_TAB();
Remove_TAB();
return 0;
}