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
|
/**************************************************************************
**************************************************************************
A C++ Program to show an example of Hashing using Mid-Square Method.
**************************************************************************
**************************************************************************/
/*************************************************************************
By :
Muhammad Tahir Shahzad [ MTS ]
B.C.S Honours [ 2000-04 ]
Government College University Lahore
Pakistan
E-mail : mtshome@wol.net.pk
Web-Site : www.mts-home.cjb.net [ www.wol.net.pk/mtshome ]
www.mtshome.cjb.net [ www.geocities.com/mtahirshahzad ]
*************************************************************************/
/*************************************************************************/
/*************************************************************************/
//--------------------------- Header Files ----------------------------//
/*************************************************************************/
/*************************************************************************/
# include <iostream.h>
# include <string.h>
# include <stdlib.h>
# include <conio.h>
# include <math.h>
/*************************************************************************/
/*************************************************************************/
//------------------------------ main( ) ------------------------------//
/*************************************************************************/
/*************************************************************************/
int main( )
{
clrscr( );
cout << endl << "Mid-Square Hashing Method" << endl;
cout << "*************************" << endl << endl;
unsigned int iNumber = 0;
unsigned int iLimit = 0;
cout << "Enter the Number = N = ";
cin >> iNumber;
cout << endl << "Enter the Limit = K = ";
cin >> iLimit;
unsigned long lNumber = powl(iNumber, 2);
char sNumber[50] = {NULL};
ultoa(lNumber, sNumber, 10);
int iCount = (strlen(sNumber) - iLimit);
int iLength = 0;
char sTemp[30] = {NULL};
cout << endl << "K * K = "<< lNumber << endl;
for (int i = 0; i < iCount; i ++)
{
iLength = (strlen(sNumber) - 1);
if ( (i % 2) == 0)
{
strset(sTemp, NULL);
strncpy(sTemp, sNumber, iLength);
strset(sNumber, NULL);
strcpy(sNumber, sTemp);
}
else
{
strrev(sNumber);
strset(sTemp, NULL);
strncpy(sTemp, sNumber, iLength);
strrev(sTemp);
strset(sNumber, NULL);
strcpy(sNumber, sTemp);
}
}
int iHashKey = atoi(sNumber);
cout << endl << "Hashing Key = " << iHashKey;
getch( );
return 0;
}
/*************************************************************************/
/*************************************************************************/
//----------------------------- THE END -------------------------------//
/*************************************************************************/
/*************************************************************************/
| |