Есть такая программа. Должна получать на вход строку и методом хеширования записывать в массив.
#include <iostream>
using namespace std;
class HashEntry {
private:
int key;
char value[32];
public:
HashEntry(int key, char value[32]) {
this->key = key;
strcpy_s(this->value,value);
}
int getKey() {
return key;
}
char* getValue() {
return value;
}
void setKey(int key)
{
this->key= key;
}
void setValue(char value[32])
{
strcpy_s(this->value, value);
}
};
int hashFunc(char h[32])
{
int l = strlen(h);
int k = ((int)h[0] + (int)h[l-1])%100;
return k;
}
int reHashFunc(int k)
{
return(k*2)%100;
}
int main()
{
setlocale(LC_ALL, "Russian");
char* arr[100];
char x[32];
int b = 2;
int c = 0;
for (int i = 99; i > 0; i--)
{
arr[i] = NULL;
}
do{
cin >> x;
if (arr[b] != NULL) cout << "arr[" << b << "]= " << arr[b] << endl;
b = hashFunc(x);
cout << "Hash function = " << b << endl;
if (arr[b] == NULL)
{
arr[b] = x; cout << "Элемент записан, код - " << b << endl;
}
else
{
if (strcmp(arr[b], x) == 0)cout << "Такой элемент уже есть1" << endl;
else{
c = reHashFunc(b);
cout << "Rehash function = " << c << endl;
if (arr[c] == NULL)
{
arr[c] = x; cout << "Элемент записан, код - " << c << endl;
}
else
{
if (strcmp(arr[c], x) == 0)cout << "Такой элемент уже есть2" << endl;
else{
while (c != b){
c = reHashFunc(c);
cout << "Rehash function = " << c << endl;
if (arr[c] == NULL){
arr[c] = x; cout << "Элемент записан, код - " << c << endl; break;
}
if (strcmp(arr[c], x) == 0){cout << "Такой элемент уже есть3" << endl; break;}
}
if (c == b){ cout << "Невозможно идентификатор"; }
}
}
}
}
if (arr[b] != NULL) cout << "arr[" << b << "]= " << arr[b] << endl;
} while (strcmp(x, "stop") != 0);
do
{
int b = 0;
int c = 0;
cin >> x;
b = hashFunc(x);
if (arr[b] != NULL) cout << "arr[" << b << "]= " << arr[b] << endl;
else
{
c = reHashFunc(b);
if (arr[c] != NULL) cout << "arr[" << c << "]= " << arr[c] << endl;
else cout << "Нет такой";
}
} while (strcmp(x, "stop") != 0);
system("pause");
return 0;
}
Вот в этом цикле почему-то значение arr[b] присваивается х -ом сразу после того как его вводишь. Не могу понять по какой причине это случается.
do{
cin >> x;
if (arr[b] != NULL) cout << "arr[" << b << "]= " << arr[b] << endl;
b = hashFunc(x);
cout << "Hash function = " << b << endl;
if (arr[b] == NULL)
{
arr[b] = x; cout << "Элемент записан, код - " << b << endl;
}
else
{
if (strcmp(arr[b], x) == 0)cout << "Такой элемент уже есть1" << endl;
else{
c = reHashFunc(b);
cout << "Rehash function = " << c << endl;
if (arr[c] == NULL)
{
arr[c] = x; cout << "Элемент записан, код - " << c << endl;
}
else
{
if (strcmp(arr[c], x) == 0)cout << "Такой элемент уже есть2" << endl;
else{
while (c != b){
c = reHashFunc(c);
cout << "Rehash function = " << c << endl;
if (arr[c] == NULL){
arr[c] = x; cout << "Элемент записан, код - " << c << endl; break;
}
if (strcmp(arr[c], x) == 0){cout << "Такой элемент уже есть3" << endl; break;}
}
if (c == b){ cout << "Невозможно разместить идентификатор"; }
}
}
}
}
if (arr[b] != NULL) cout << "arr[" << b << "]= " << arr[b] << endl;
} while (strcmp(x, "stop") != 0);