#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define KEYLENGTH 10
typedef char ElemType[KEYLENGTH+1];
typedef char PWType[17];
typedef struct listnode
{
ElemType data;
struct listnode *next;
PWType PW;
}ListNode,*ListNodePtr,*List;
typedef struct hashtbl
{
int TableSize;
List 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 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<H->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 Login(HashTable H,ElemType Key,PWType PW)
{
int Pos=Hash(H->TableSize,Key);
ListNodePtr L;
for(L=H->head[Pos].next;L;L=L->next)
if(strcmp(Key,L->data)==0)
{
if(strcmp(PW,L->PW)==0)
{
printf("Login: OK\n");
return 0;
}
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].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].next;
H->head[Pos].next=L;
printf("New: OK\n");
}
else
printf("ERROR: Exist\n");
return 0;
}
int main()
{
int N,i;
char c;
ElemType Key;
PWType PW;
scanf("%d",&N);
HashTable H=CreateTable(N);
for(i=0;i<N;i++)
{
getchar();
scanf("%c%s%s",&c,Key,PW);
if(c=='L')
Login(H,Key,PW);
else if(c=='N')
New(H,Key,PW);
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/71289.html
標籤:C語言
