> ch; . if (ch == '1') . { . cout << "Please enter contact's name" << endl; . contact tmp; . cin.ignore(); . getline(cin , tmp.name); . cout << "please enter contact's number" << endl; . cin.ignore(); . getline(cin , tmp.number); . int h = MyHash(tmp.name); . int s = search(tmp.name); . if (s != -1) . { . cout << "this name already exist" << endl; . cout << "Do you wanna update the number with thw given number? (press y)" << endl; . cin >> ch; . if (ch == 'y') . { . table[h][s].number = tmp.number; . cout << "the number updated" << endl; . } . } . else . { . table[h].push_back(tmp); . cout << "Successfully added" << endl; . } . system("pause"); . } . else if (ch == '2') . { . cout << "Please enter contact's name" << endl; . contact tmp; . cin.ignore(); . getline(cin , tmp.name); . int h = MyHash(tmp.name); . int s = search(tmp.name); . if (s != -1) . { . table[h].erase(table[h].begin() + s); . cout << "Successfully removed" << endl; . } . else . { . cout << "The name doesn't exist" << endl; . } . system("pause"); . } . else if (ch == '3') . { . cout << "Please enter contact's name" << endl; . contact tmp; . cin.ignore(); . getline(cin , tmp.name); . int h = MyHash(tmp.name); . int s = search(tmp.name); . if (s != -1) . { . cout << "The number is " << table[h][s].number << endl; . } . else . { . cout << "The name doesn't exist" << endl; . } . system("pause"); . } . else if (ch == '4') . { . cout << endl; . break; . system("pause"); . } . system("cls"); . } . }من توصیح این کد به صورت کامل میخواستم کمکم کنین " name="description"> > ch; . if (ch == '1') . { . cout << "Please enter contact's name" << endl; . contact tmp; . cin.ignore(); . getline(cin , tmp.name); . cout << "please enter contact's number" << endl; . cin.ignore(); . getline(cin , tmp.number); . int h = MyHash(tmp.name); . int s = search(tmp.name); . if (s != -1) . { . cout << "this name already exist" << endl; . cout << "Do you wanna update the number with thw given number? (press y)" << endl; . cin >> ch; . if (ch == 'y') . { . table[h][s].number = tmp.number; . cout << "the number updated" << endl; . } . } . else . { . table[h].push_back(tmp); . cout << "Successfully added" << endl; . } . system("pause"); . } . else if (ch == '2') . { . cout << "Please enter contact's name" << endl; . contact tmp; . cin.ignore(); . getline(cin , tmp.name); . int h = MyHash(tmp.name); . int s = search(tmp.name); . if (s != -1) . { . table[h].erase(table[h].begin() + s); . cout << "Successfully removed" << endl; . } . else . { . cout << "The name doesn't exist" << endl; . } . system("pause"); . } . else if (ch == '3') . { . cout << "Please enter contact's name" << endl; . contact tmp; . cin.ignore(); . getline(cin , tmp.name); . int h = MyHash(tmp.name); . int s = search(tmp.name); . if (s != -1) . { . cout << "The number is " << table[h][s].number << endl; . } . else . { . cout << "The name doesn't exist" << endl; . } . system("pause"); . } . else if (ch == '4') . { . cout << endl; . break; . system("pause"); . } . system("cls"); . } . }من توصیح این کد به صورت کامل میخواستم کمکم کنین " />

کد نویسی

عنوان : کد نویسی
گروه درخواست : برنامه نویسی C , C++ و جاوا
شرح درخواست :

سلام من کد هشینگ به زبان سی پلاس دفترچه تلفنinclude
# include
# include

using namespace std;

struct contact
{
string name , number;
};

const int hashSize = 1000; // size of hash table

vector table[hashSize]; // array with size of hash table
// every index of table is a vector


//example of hash function
unsigned int MyHash(const string& str)
{
unsigned int hash = 1315423911;

for(std::size_t i = 0; i < str.length(); i++)
{
hash ^= ((hash << 5) + str[i] + (hash >> 2));
}

return hash%hashSize;
}

int search(const string& str)
{
int h = MyHash(str);
for (int i = 0; i < table[h].size(); i++)
{
if (table[h][i].name == str)
return i;
}
return -1;
}


int main()
{
cout << "Welcome to conacts program" << endl;
while(1)
{
cout << "Please choose one of the potions below" << endl;
cout << "1. Add number" << endl;
cout << "2. Remove number" << endl;
cout << "3. Search number" << endl;
cout << "4. Exit" << endl;
char ch;
cin >> ch;
if (ch == '1')
{
cout << "Please enter contact's name" << endl;
contact tmp;
cin.ignore();
getline(cin , tmp.name);
cout << "please enter contact's number" << endl;
cin.ignore();
getline(cin , tmp.number);
int h = MyHash(tmp.name);
int s = search(tmp.name);
if (s != -1)
{
cout << "this name already exist" << endl;
cout << "Do you wanna update the number with thw given number? (press y)" << endl;
cin >> ch;
if (ch == 'y')
{
table[h][s].number = tmp.number;
cout << "the number updated" << endl;
}
}
else
{
table[h].push_back(tmp);
cout << "Successfully added" << endl;
}
system("pause");
}
else if (ch == '2')
{
cout << "Please enter contact's name" << endl;
contact tmp;
cin.ignore();
getline(cin , tmp.name);
int h = MyHash(tmp.name);
int s = search(tmp.name);
if (s != -1)
{
table[h].erase(table[h].begin() + s);
cout << "Successfully removed" << endl;
}
else
{
cout << "The name doesn't exist" << endl;
}
system("pause");
}
else if (ch == '3')
{
cout << "Please enter contact's name" << endl;
contact tmp;
cin.ignore();
getline(cin , tmp.name);
int h = MyHash(tmp.name);
int s = search(tmp.name);
if (s != -1)
{
cout << "The number is " << table[h][s].number << endl;
}
else
{
cout << "The name doesn't exist" << endl;
}
system("pause");
}
else if (ch == '4')
{
cout << endl;
break;
system("pause");
}
system("cls");
}
}من توصیح این کد به صورت کامل میخواستم کمکم کنین

شما هم سوال دارید ؟

از کارشناسان پاسخیاب بپرسید!
loading...