這裏是我的代碼:Ç - 哈希表不能正常工作
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define m 9
int flag1=1;
//Creating memory for username and password
struct node // The list that is being used to store username and password
{
char username[30];
char password[30];
struct node *next;
};
int getkey(char[30]); //a method to get "randomly" a key
int hash(int); // a method to create the hashcode
void insert(struct node **,char[30],char[30]); //adding a node struct to a specific possition in hashtable
void initializeHashTable(struct node **);
void search(struct node **,char[30]);//check if password exists
void searchuser(struct node **,char[30]); //check if username exists
void print(struct node**);//printing the hashtable
void print(struct node **T)
{
struct node *tmp;
int i,flag=0;
printf("-------Data Base-------\n");
for(i=0;i<m;i++)
{
tmp=*(T+i);
while(tmp!=NULL)
{
printf("Username: %s Password: %s\n",tmp->username,tmp->password);
tmp=tmp->next;
}
}
}
void search(struct node **T,char password[30])
{
struct node *tmp;
int i,flag=0;
for(i=0; i<m; i++)
{
tmp=*(T+i);
while(tmp!=NULL)
{
if(strcmp(tmp->password,password)==0)
{
flag=1;
break;
}
tmp=tmp->next;
}
if(flag==1)
break;
}
if(flag==1)
printf("Authentication Successfull\n");
else
printf("Authentication Failed\n");
}
void searchuser(struct node **T,char user[30])
{
struct node *tmp;
int i,flag=0;
for(i=0; i<m; i++)
{
tmp=*(T+i);
while(tmp!=NULL)
{
if(strcmp(tmp->username,user)==0)
{
flag=1;
break;
}
tmp=tmp->next;
}
if(flag==1)
break;
}
if(flag==0)
{
printf("Username NOT Found\n");
flag1=0;
}
}
void initializeHashTable(struct node **T)
{
int i;
for (i=0; i<m; i++)
*(T+i)=NULL;
}
int getkey(char name[30])
{
int i=0;
int key=0;
for (i=0; i<15; i++)
{
key+=name[i];
}
return key;
}
int hash(int key)
{
return key%m;
}
void insert (struct node **T,char name[30],char password[30])
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
strcpy(newnode->username,name);
strcpy(newnode->password,password);
int key;
key=getkey(name);
int hashcode;
hashcode=hash(key);
//printf("Key:%d\n",key);
// printf("Hashcode:%d\n",hashcode);
if(*T+hashcode==NULL)
{
(*(T+hashcode))->next=newnode;
newnode->next=NULL;
}
else
{
newnode->next=(*(T+hashcode));
(*(T+hashcode))=newnode;
}
}
int main()
{//possible content of file: phill 1234
char choice;
struct node **T;
struct node **head;
head==NULL;
T=(struct node**)malloc(m*sizeof(struct node));//creating the hashmap
FILE *fp;
char name[30];
char pass[30];
fp=fopen("passwords.txt","r");
if (fp==NULL)
{
printf("File Not Found");
return 0;
}
else
{
initializeHashTable(&(*T));
while(!feof(fp)) // end of file
{
fscanf(fp,"%s %s",name,pass); //reads until first wild space,
//one each loop, you get 1 username and 1 password from the file
//adding them on hashmap
insert(&(*T),name,pass);
}
}
//user authentication
do{
printf("Enter Username:\n");
fflush(stdin);
scanf("%s",&name);
searchuser(&(*T),name);
if(flag1==1)
{
printf("Enter Password:\n");
scanf("%s",&pass);
search(&(*T),pass);
}
printf("Try Again? (y/n)\n");
fflush(stdin);
scanf("%d",&choice);
}while(choice!='y');
free(T);
free(fp);
free(head);
}
- 我的代碼工作正常的循環,它的認證 成功,但隨後有時死機,或者我給出相同的 詳細信息(用戶名和密碼),並以某種方式認證失敗的代碼
一部分,我相信它的越野車:
//user authentication do{ printf("Enter Username:\n"); fflush(stdin); scanf("%s",&name); searchuser(&(*T),name); if(flag1==1) { printf("Enter Password:\n"); scanf("%s",&pass); search(&(*T),pass); } printf("Try Again? (y/n)\n"); fflush(stdin); scanf("%d",&choice); }while(choice!='y');
,如果我把這意味着運行循環
while choice=='y'
do..while(choice=='y')
停止if choice=='y'
這是有點兒奇怪
問題:身份驗證器無法正常工作後第一次嘗試
謝謝很多你的時間!
,在我跳出
建議您使用調試器來跟蹤程序的執行情況並檢查其狀態。這是調試此類問題的最佳方法(不能總是轉向SO來爲您調試)。 – kaylum
我已經嘗試了數週,沒有找到解決方案...我如何使用調試器? – Phill
嘗試使用fgets()而不是scanf()。在最後一個scanf()中,很可能會有一個不需要的換行符被結轉。在最後一個scanf()後面插入這行'fgets(stdin,garbage,sizeof(garbage))'。並聲明'char garbage [50];'。 –