-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.cpp
More file actions
179 lines (133 loc) · 3.25 KB
/
menu.cpp
File metadata and controls
179 lines (133 loc) · 3.25 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <cstdlib>
#include <string>
#include "HashTable.h"
using namespace std;
bool checkKey(const string& key) {
if (key.length() != 6)
return false;
if (key[0] < '0' || key[0] > '9')
return false;
if (key[1] < '0' || key[1] > '9')
return false;
if (key[2] < 'A' || key[2] > 'Z')
return false;
if (key[3] < 'A' || key[3] > 'Z')
return false;
if (key[4] < '0' || key[4] > '9')
return false;
if (key[5] < 'A' || key[5] > 'Z')
return false;
return true;
}
int gethash(const string& key) {
int res = 0;
for (int i = 0; i < key.length(); i++)
res += key[i] * key[i] * key[i] * key[i];
return res % 1500;
}
string getRndKey() {
string key = "";
key += '0' + rand() % 10;
key += '0' + rand() % 10;
key += 'A' + rand() % 26;
key += 'A' + rand() % 26;
key += '0' + rand() % 10;
key += 'A' + rand() % 26;
return key;
}
void Print(HashTable& table) {
table.Print();
}
void Insert(HashTable& table) {
string key;
int value;
cout << "Enter key, value: ";
cin >> key >> value;
while (!checkKey(key)) {
cout << "Enter key again: ";
cin >> key;
}
table.Insert(key, value);
}
void Remove(HashTable& table) {
string key;
cout << "Enter key: ";
cin >> key;
while (!checkKey(key)) {
cout << "Enter key again: ";
cin >> key;
}
table.Remove(key);
}
void Search(HashTable& table) {
string key;
cout << "Enter key: ";
cin >> key;
while (!checkKey(key)) {
cout << "Enter key again: ";
cin >> key;
}
table.Search(key);
}
void Save(HashTable& table) {
string path;
cout << "Enter path to file: ";
cin >> path;
table.SaveInfo(path);
}
void FillRandom(HashTable& table) {
int n;
cout << "Enter count of elements: ";
cin >> n;
for (int i = 0; i < n; i++) {
table.Insert(getRndKey(), i);
}
}
int main() {
HashTable table(1500, gethash);
int item; // выбираемый пункт меню
do {
system("cls"); // очищаем экран
// выводим меню
cout << "What do you want to do?" << endl;
cout << "1. Print" << endl;
cout << "2. Insert" << endl;
cout << "3. Remove" << endl;
cout << "4. Search" << endl;
cout << "5. Save" << endl;
cout << "6. Fill random" << endl;
cout << "7. Quit" << endl;
cout << ">";
cin >> item; // считываем пункт меню
// обрабатываем некорректный ввод
while(item < 1 || item > 7) {
cout << "Incorrect titem. Try again: ";
cin >> item; // считываем пункт меню заново
}
switch (item) {
case 1:
Print(table);
break;
case 2:
Insert(table);
break;
case 3:
Remove(table);
break;
case 4:
Search(table);
break;
case 5:
Save(table);
break;
case 6:
FillRandom(table);
break;
}
if (item != 7) {
system("pause"); // задерживаем экран
}
} while (item != 7); // повторяем, пока не решим выйти
return 0;
}