Home > Back-end >  QQ account login application with pta run timeout, bosses help have a look
QQ account login application with pta run timeout, bosses help have a look

Time:09-24

# include
# include
# include
H # include
# define KEYLENGTH 10
Typedef char ElemType [KEYLENGTH + 1];
Typedef char PWType [17].
Typedef struct listnode
{
ElemType data;
Struct listnode * next;
PWType PW.
ListNodePtr} ListNode, * and * a List;

Typedef struct hashtbl
{
Int TableSize;
List the head;
} HashTbl, * HashTable;

Int NextPrime (int N)
{
Int I, j;
If (N==1) return 2;
For (I=N;; I++)
{
For (j=2; J <=SQRT (N); J++)
If (I % j==0) break;
If (j> SQRT (N)) return the I;
}
}

HashTable CreateTable (int N)
{
HashTable H;
int i;
H=(HashTable) malloc (sizeof (HashTbl));
H -> TableSize=NextPrime (N);
H -> head=(List) malloc (H -> TableSize * sizeof (ListNode));
for(i=0; I TableSize; I++)
{
H -> head [I] next=NULL;
H -> head [I]. Data [0]='\ 0';
H -> head [I]. PW [0]='\ 0';
}
Return H;
}

Int Hash (int Size, ElemType Key)
{
Int I, intkey=0;
For (I=strlen (Key) - 1; I> strlen (Key) - 1-4; I -)
Intkey=intkey * 10 + Key [I] - '0';
Return intkey % Size;
}

Int the Login (HashTable H, ElemType Key, PWType PW)
{
Int Pos=Hash (H -> TableSize, Key);
ListNodePtr L;
For (L=H -> head (Pos). The next; L; L=L -> next)
If (STRCMP (Key, L -> data)==0)
{
If (STRCMP (PW, L -> PW)==0)
{
Printf (" Login: OK \ n ");
return 0;
}
The else
{
Printf (" ERROR: Wrong PW \ n ");
return 0;
}
}
Printf (" ERROR: Not Exist \ n ");
return 0;
}

Int New (HashTable H, ElemType Key, PWType PW)
{
Int Pos=Hash (H -> TableSize, Key);
ListNodePtr L;
For (L=H -> head (Pos). The next; L; L=L -> next)
If (STRCMP (Key, L -> data)==0) break;
if(! L)
{
L=(ListNodePtr) malloc (sizeof (ListNode));
Strcpy (L -> data, Key);
Strcpy (L -> PW, PW);
L -> next=H -> head (Pos). The next;
H -> head (Pos). The next=L;
Printf (" New: OK \ n ");
}
The else
Printf (" ERROR: Exist \ n ");
return 0;
}

Int main ()
{
Int N, I;
char c;
ElemType Key;
PWType PW.
The scanf (" % d ", & N);
HashTable H=CreateTable (N);
for(i=0; i{
getchar();
The scanf (" % c % s % s ", & c, Key, PW);
If (c=='L')
Login (H, Key, PW);
Else if (c=='N')
New (H, Key, PW);
}
return 0;
}
  • Related