C - 哈希表无法正常工作

标签 c hashtable

这是我的代码:

#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');
    

    如果我把 do..while(choice=='y')这意味着运行循环 while choice=='y'它停止了if choice=='y'这有点奇怪

  • Issue: Authenticator doesn't work properly after 1st try



    非常感谢您的时间!

    最佳答案

    几个问题让我想到了:

    struct node **T;
    ...
    T=(struct node**)malloc(m*sizeof(struct node));
    

    你打算为T成为 struct node 的序列(在这种情况下 T 的类型是错误的),或者指向 struct node 的指针序列(在这种情况下,sizeof 的论点是错误的)?

    如果你写的话,它会更干净(而且更不容易出错)
    T = malloc( m * sizeof *T ); 
    

    那么这只是决定T的类型的问题。 .就个人而言,我希望您分配 struct node 的序列。 , 所以很可能会被宣布
    struct node *T = malloc(  m * sizeof *T );
    

    但是,您的其余代码似乎确实假设 T是指向 struct node 的指针序列,所以在那种情况下,那将是
    struct node **T = malloc( m * sizeof *T );
    

    这就是这个成语的美妙之处——你唯一需要改变的是T的类型。 . malloc call 本身不需要改变。如果 T 的类型是 struct node * ,然后 sizeof *T相当于sizeof (struct node) .如果 Tstruct node ** ,然后 sizeof *T相当于sizeof (struct node *) .

    然后是这个:
    initializeHashTable(&(*T));
    

    这真的应该是
    intializeHashTable( T );
    

    还,
    while(!feof(fp))
    

    总是错的 - feof直到您尝试读取文件末尾之后才会返回 true,因此您会经常循环一次。您应该检查输入操作的结果:
    while ( fscanf(fp,"%s %s",name,pass) == 2 )
    {
       insert(T,name,pass);
    }
    

    同样,论点 &(*T)应该只是 T .

    至于你的输入:
    printf("Enter Username:\n");
    fflush(stdin);
    scanf("%s",&name);
    
    fflush这里不需要调用 - 如果在输出操作之后立即有输入操作,则暗示刷新。

    编辑

    不好意思说我看错了fflush调用 - 出于某种原因,我将其读作 fflush( stdout) ,这意味着您要确保在调用 scanf 之前将输出写入控制台.

    调用fflush在输入流上是错误的,这样做的行为是未定义的。它不会清除任何未读输入的输入流(MSVC 除外,这是因为 Microsoft 决定对现有的不良行为进行编码)。

    失去fflush完全调用。

    结束编辑
    scanf电话应该是
    scanf( "%s", name );
    
    &这里不需要 - name将从“char 的 30 元素数组”类型隐式转换为“指向 char 的指针”。 pass 相同.

    编辑

    正如所写,这些 scanf调用是不安全的 - 如果您输入的字符串比目标缓冲区长,scanf会很高兴地将这些额外的字符写入该缓冲区之后的内存中,从而导致从损坏的数据到段错误的任何事情。你需要确保你没有读到太多的字符。您可以使用字段宽度说明符来执行此操作,例如
    scanf( "%29s", name );  // leave at least one element for the 0 terminator
    

    或使用 fgets :
    fgets( name, sizeof name, stdin );
    
    fgets如果有空间, call 将读取尾随换行符并将其存储到缓冲区中,因此您需要做一些额外的工作:
    char *newline = strchr( name, '\n' );
    if ( *newline )
      *newline = 0;
    

    如果缓冲区中没有换行符,那么您将需要在下一次读取之前清除输入流:
    while ( getchar() != '\n' )
      ; // empty loop
    

    结束编辑
      scanf("%d",&choice);
    }while(choice!='y');
    

    您使用了错误的格式说明符来读取 choice - 你在告诉 scanf期望十进制整数输入,而不是字符。 'y'与格式不匹配,所以 scanf实际上并没有从输入流中读取它,并且 choice没有更新。你应该把它改成
      choice = getchar();
    } while ( choice != 'y' );
    

    还有很多其他问题,但从这些开始。

    关于C - 哈希表无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44145235/

    相关文章:

    java - 常用元数据Hashmap

    c# - float 问题 : C++ to C# migration

    c++ - 在某些编译器中使用 {0} 的数组初始化失败,但第一个元素除外

    c - 我如何正确地找出结构数组的大小?

    c - 进行 64 位无符号整数除法时如何管理 65 位分配?

    perl - 理解推送到哈希和 ||=[] 构造。 (在boilerplate.t 中生成)

    c# - 在添加字典之前检查字典中是否存在键的最佳方法?

    java - 无法在 Java 中导入 Hashtable

    c++ - python c 或 c++ 的速度优化哪个是更好的扩展语言

    c++ - 用数组和 C++ 字符串实现哈希算法 - 段错误(核心转储)