Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 哈希表没有';t通过它返回正确的值';s指数_C_File_Hash_Hashmap - Fatal编程技术网

C 哈希表没有';t通过它返回正确的值';s指数

C 哈希表没有';t通过它返回正确的值';s指数,c,file,hash,hashmap,C,File,Hash,Hashmap,我有此csv文件包含联系人: NAME, NUMBER, ADDRESS, EMAIL Kevin Mahendra, +62 812-XXXX-XXXX, Jln.Anggrek Merah 3, kevinitsnovember@gmail.com Adwi Lanang, +62 821-XXXX-XXXX, Jln.Ruhui Rahayu, adwilanang@gmail.com Wasis Sirutama, +62 813-XXXX-XXXX, Jln.Pramuka 6 25,

我有此csv文件包含联系人:

NAME, NUMBER, ADDRESS, EMAIL
Kevin Mahendra, +62 812-XXXX-XXXX, Jln.Anggrek Merah 3, kevinitsnovember@gmail.com
Adwi Lanang, +62 821-XXXX-XXXX, Jln.Ruhui Rahayu, adwilanang@gmail.com
Wasis Sirutama, +62 813-XXXX-XXXX, Jln.Pramuka 6 25, wasisnaruto@gmail.com
Alief Dean, +62 811-XXXX-XXXX, Jln.Padat Karya, aliefdean@gmail.com
Baharudin Nuri, +62 813-XXXX-XXXX, Jln.Ruhui Rahayu 1, baharudin008@yahoo.com
Yonggi Wijaya, +62 853-XXXX-XXXX, Jln.PM Noor Pondok S, yonggiwijaya@gmail.com
Artha Yoga, +62 822-XXXX-XXXX, Jln.A.Yani Gg.1, arthayoga97@gmail.com
Rusydi Nashier, +62 858-XXXX-XXXX, Jln.Perjuangan No.90, rusydinashier@gmail.com
Andre Pieters, +62 822-XXXX-XXXX, Jln.Villa Tamara No.1, azzahz@gmail.com
Paco Corleone, +62 816-XXXX-XXXX, Jln.Anggrek Merah 3, pacocorleone@gmail.com
这是我的C代码:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Number of buckets for TABLE
#define N 26

#define MAX 50

typedef struct Contact
{
    char name[MAX];
    char number[MAX];
    char address[MAX];
    char email[MAX];
    struct Contact* next;
} 
Contact;

void searching_contact(FILE *file);
void load_hash_table(FILE **file);
unsigned int hash(const char *name);

// Hash table
Contact *table[N];

int main(void)
{  
    // OPEN CSV FILE AS append and read mode
    FILE *file = fopen("contacts.csv", "r");
    if (file == NULL)
    {
        printf("Error open file!\n");
        return 1;
    }

    searching_contact(file);

    fclose(file);
    return 0;
}    

void searching_contact(FILE *file)
{

    char name[MAX];

    printf("Search Name: ");
    scanf("%[^\n]%*c", name);
    fflush(stdin);

    // Load csv file into hash table first    
    load_hash_table(&file);

    // Get index number by calling hash function
    int hashIndex = hash(name);
    printf("\n\nIndex number we get from searching: %i\n", hashIndex);
    
    // Point to table that may contain the person
    Contact *cursor = table[hashIndex];
    
    // This will always print the last person
    printf("The name on the table: %s\n", cursor->name);

    // Keep traversing linked list in table
    while (cursor != NULL)
    {
        // If the person found, print the contact information
        if (strcmp(name, cursor->name) == 0)
        {
            printf("%s %s %s %s", cursor->name, cursor->number, cursor->address, cursor->email);
        }
        else
        {
            // If not the person, but in the same table, go to the next linked list
            cursor = cursor->next;
        }
    }
    printf("Not found!\n");
}

// FUNCTION TO LOAD CSV FILE INTO HASH TABLE
void load_hash_table(FILE **file)
{
    Contact *new = malloc(sizeof(Contact));
    if (new == NULL)
        exit(1);

    /*
        "%[^,], "
        Empty space or space after above sign will remove spaces or newline (\n) on each string
        Just because, when we try to use hash function, the spaces or newline will also include
        And we want to remove them so when user searching by name it will produce same hash index
    */
    while(fscanf(*file, "%[^,], %[^,], %[^,], %[^\n] ", new->name, new->number, new->address, new->email) == 4)
    {   
        // Skip header from CSV file   
        if (strcmp("NAME", new->name) == 0)
            continue;    
    
        // Get index number from hash function with People name as input
        int index = hash(new->name);

        // Try to print name and it's index in csv file for debugging
        printf("%s\n", new->name);
        printf("%i\n", index);
        
        /* 
            Create linked list point to WHAT inside Table[index]
            For very first struct, it points to NULL, then store it in Table
            Next struct, with the same Index number, it will point the first one
        */   
        new->next = table[index];
        table[index] = new;

        // Malloc for next fscanf
        Contact *new = malloc(sizeof(Contact));
        if (new == NULL)
            exit(1);
    }
}

// Hash function that will return index number from Table
unsigned int hash(const char *name)
{
    // TODO
    unsigned long hash = 5381;
    int c;

    while ((c = toupper(*name++)))
    {
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
    }
    return hash % N;
}
#包括
#包括
#包括
#包括
//表的存储桶数
#定义n26
#定义最大值50
类型定义结构触点
{
字符名[MAX];
字符数[MAX];
字符地址[MAX];
字符电子邮件[MAX];
结构联系人*下一步;
} 
接触;
无效搜索联系人(文件*文件);
无效加载\u哈希表(文件**文件);
无符号整数散列(常量字符*名称);
//哈希表
联系人*表[N];
内部主(空)
{  
//以附加和读取模式打开CSV文件
FILE*FILE=fopen(“contacts.csv”、“r”);
if(file==NULL)
{
printf(“打开文件时出错!\n”);
返回1;
}
查找联系人(文件);
fclose(文件);
返回0;
}    
无效搜索联系人(文件*文件)
{
字符名[MAX];
printf(“搜索名称:”);
scanf(“%[^\n]%*c”,名称);
fflush(stdin);
//首先将csv文件加载到哈希表中
加载\u散列\u表(&文件);
//通过调用哈希函数获取索引号
int hashIndex=hash(名称);
printf(“\n\n我们从搜索中获得的索引号:%i\n”,hashIndex);
//指向可能包含此人的表格
Contact*cursor=表[hashIndex];
//这将始终打印最后一个人
printf(“表上的名称:%s\n”,光标->名称);
//保持遍历表中的链表
while(光标!=NULL)
{
//如果找到此人,请打印联系人信息
if(strcmp(名称,光标->名称)==0)
{
printf(“%s%s%s%s”,光标->名称,光标->编号,光标->地址,光标->电子邮件);
}
其他的
{
//如果不是此人,而是在同一个表中,请转到下一个链接列表
光标=光标->下一步;
}
}
printf(“未找到!\n”);
}
//函数将CSV文件加载到哈希表中
无效加载\u哈希\u表(文件**文件)
{
联系人*new=malloc(sizeof(联系人));
if(new==NULL)
出口(1);
/*
"%[^,], "
空空格或以上符号后的空格将删除每个字符串上的空格或换行符(\n)
这是因为,当我们尝试使用哈希函数时,空格或换行符也将包括
我们希望删除它们,这样当用户按名称搜索时,它将生成相同的哈希索引
*/
而(fscanf(*文件,“%[^,]”,“%[^,]”,“%[^,]”,“[^\n]”,新建->名称,新建->编号,新建->地址,新建->电子邮件)==4)
{   
//从CSV文件跳过头文件
如果(strcmp(“名称”,新建->名称)==0)
继续;
//从以人名为输入的哈希函数中获取索引号
int index=hash(新建->名称);
//尝试在csv文件中打印名称及其索引以进行调试
printf(“%s\n”,新建->名称);
printf(“%i\n”,索引);
/* 
创建链接列表指向表内的内容[索引]
对于第一个结构,它指向NULL,然后将其存储在表中
下一个结构,使用相同的索引号,它将指向第一个
*/   
新建->下一步=表[索引];
表[索引]=新的;
//下一个fscanf的Malloc
联系人*new=malloc(sizeof(联系人));
if(new==NULL)
出口(1);
}
}
//将从表中返回索引号的哈希函数
无符号整数散列(常量字符*名称)
{
//待办事项
无符号长散列=5381;
INTC;
而((c=toupper(*name++)))
{
hash=((hash两个问题:

  • 主要问题是有两个
    Contact*new=malloc(sizeof(Contact))
    行。一个在循环内部,一个在循环外部。它们是两个不同的变量。
    while
    循环条件使用循环外部的变量。因此,
    fscanf
    为每个循环写入相同的内存。一种修复方法是使第二个实例只
    new=malloc(sizeof(Contact));
    。请注意,由于最后分配的节点丢失,此循环存在内存泄漏-留给您作为练习进行修复

  • 搜索\u contact
    有一个infinete循环作为
    如果(strcmp(name,cursor->name)==0)
    块缺少一个
    中断

  • 两个问题:

  • 主要问题是有两个
    Contact*new=malloc(sizeof(Contact))
    行。一个在循环内部,一个在循环外部。它们是两个不同的变量。
    while
    循环条件使用循环外部的变量。因此,
    fscanf
    为每个循环写入相同的内存。一种修复方法是使第二个实例只
    new=malloc(sizeof(Contact));
    。请注意,由于最后分配的节点丢失,此循环存在内存泄漏-留给您作为练习进行修复

  • 搜索\u contact
    有一个infinete循环作为
    如果(strcmp(name,cursor->name)==0)
    块缺少一个
    中断


  • 非常感谢!这很有效!非常感谢!这很有效!