用C语言写一个身份证识别省区!

qq_38241541 qq_38241541 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这个是保存6位省区的编码txt文件,
现在就是不知道怎么把这个内容,每行信息形成hash表,怎么查找
typedef struct _hash_map_t {
size_t size;
listnode_t** key;
listnode_t** value;
} hash_map_t;
这是哈希的结构体
struct _listnode_t {
struct _listnode_t *next;

union {
void* data;
struct _list_t *list;
const char *str;
long key;
}nodeData;
};
typedef struct _listnode_t listnode_t;
这是结点的
struct _list_t {
size_t size; /* 节点数量 /
listnode_t *head; /链表头结点指针*/
listnode_t tail; /链表尾结点指针*/
};
typedef struct _list_t list_t;
这是链表的
这是老师给的代码,我不知道为什么hash结构体里面二级指针value是什么功能,能不能解释下这个到底怎么形成hash表,然后查找对应省区,输出对应省区字符串?跪求各位大神帮忙
这是hashmap.C
/*
* hashMap.c
* 通用散列表的实现
* 将键-值(key-value)对存入散列表,键(key)必须为空值结束的字符串,值(value)可以为任意类型的数据
*/
#include "hashMap.h"
#include "list.c"

typedef struct _hash_map_t {
size_t size;
listnode_t** key;
listnode_t** value;
} hash_map_t;

/* Hash a string, return a hash key */
static ulong hash_string(const char *s, int len) {
ulong h = 0;
int i = 0;
assert(s);
if (len < 0)
len = (s ? (int) strlen(s) : 0);
while (i++ < len) {
h = 17 * h + *s++;
}
return h;
}

static void _free_map_key(listnode_t* node) {
listnode_t *old;
while (node) {
old = node;
node = node->next;

free(old->nodeData.data);
free(old);
}
}

static void _free_map_value(listnode_t* node, pfcb_hmap_value_free pfunc) {
listnode_t *old;
while (node) {
old = node;
node = node->next;

if (pfunc)
(*pfunc)(old->nodeData.data);
free(old);
}
}

/*=============================================================================
公用函数
=============================================================================*/

/* 使用前创建 */
void hashMapCreate(HashMap *hashMap, int size) {
(*hashMap) = (hash_map_t*) malloc(sizeof (hash_map_t));
(*hashMap)->size = size;
(*hashMap)->key = (listnode_t**) calloc(size, sizeof (listnode_t*));
(*hashMap)->value = (listnode_t**) calloc(size, sizeof (listnode_t*));
}


/* 用完后清除 */
extern void hashMapDestroy(HashMap hashMap, pfcb_hmap_value_free pfunc) {
/*请实现该函数*/



}

/* 向散列表插入键-值对(key-value),值为调用程序分配的内存指针 */
void hashMapInsert(HashMap hashMap, const char* key, int key_len, void* value) {
/*请实现该函数*/
//hashMap->size=key_len;
// hashMap.key=key;
// hashMap.value=value;


}

/* 用指定的字符串(键)在散列表中查找对应的值 */
void* hashMapSearch(HashMap hashMap, const char *key) {
/*请实现该函数*/
//hashMap.key


}

这是hashmap.h
/* 
* File: hashMap.h
* Author: zhangtaihong
*
* Created on 2013年8月7日, 下午12:24
*/

#ifndef HASHMAP_H
#define HASHMAP_H

#ifdef __cplusplus
extern "C" {
#endif

#include "unistd.h"

/* 静态散列表主桶的数量 */
#define HASHMAP_SIZE 1024

/* 指向_hash_map_t结构的不透明指针,隐藏_hash_map_t的实现细节 */
typedef struct _hash_map_t* HashMap;

typedef void(*pfcb_hmap_value_free)(void* value);

/* 调用程序实现的清除值函数的例子
void my_hmap_free_value(void* pv)
{
free(pv);
}*/


/* 使用前先创建,如:
* HashMap hashMap;
* hashMapCreate(&hashMap, HASHMAP_SIZE);
* assert (hashMap); //如果hashMap==NULL,说明内存不足
* void* mydata=malloc(n);
* hashMapInsert(hm, "shanghai", -1, mydata);
...
* 程序结束前清除散列表
* hashMapDestroy(hashMap, my_hmap_free_value);
*/
extern void hashMapCreate(HashMap *hashMap, int size);

/* 清除散列表 */
extern void hashMapDestroy(HashMap hashMap, pfcb_hmap_value_free pfunc);

/* 向散列表插入键-值对(key-value),值为调用程序分配的内存指针 */
extern void hashMapInsert(HashMap hashMap, const char* key, int key_len/* -1 使用strlen函数 */, void* value);

/* 用指定的可以字符串在散列表中查找键对应的值 */
extern void* hashMapSearch(HashMap hashMap, const char *key);


#ifdef __cplusplus
}
#endif

#endif /* HASHMAP_H */

这是list.h
/* 
* File: list.h
* Author: zhangtaihong
* 通用顺序链表及节点结构 -- 可以存储任意类型的数据
* Created on 2013年8月7日, 下午12:28
*/

#ifndef LIST_H
#define LIST_H

#ifdef __cplusplus
extern "C" {
#endif

#include "unistd.h"

struct _listnode_t {
struct _listnode_t *next;

union {
void* data;
struct _list_t *list;
const char *str;
long key;
}nodeData;
};
typedef struct _listnode_t listnode_t;

struct _list_t {
size_t size; /* 节点数量 */
listnode_t *head; /*链表头结点指针*/
listnode_t *tail; /*链表尾结点指针*/
};
typedef struct _list_t list_t;

/* list_destroy()要调用的回调函数的声明, 若为NULL,表示不用回调函数 */
typedef void(*pfcb_list_node_free)(listnode_t* node);

/* 被调用程序实现的节点数据清理函数的例子
void myListNodeFree(listnode_t *node)
{
free(node->nodeData.data);
}*/

/* 向链表添加一个节点 */
extern void listAppendNode(list_t *in_list, listnode_t *in_node);

/* 删除链表的第一个节点,返回新的链表 */
extern listnode_t* listRemoveHead(list_t *in_list);

/* 删除链表中的所有节点,但链表本身依然保留 */
extern void listRemoveAll(list_t *in_list, pfcb_list_node_free pfunc /* NULL for no use or a key node */);

/* 返回链表的一个拷贝 */
extern list_t* listCopy(list_t in_list);

/* 合并两个链表,将second合并到first,不清除second */
extern void list_concat(list_t *first, list_t *second);

/* 创建一个新节点,不为输入的node_data分配内存 */
extern listnode_t* listNodeCreate(void* node_data);

/* 创建一个新节点,整型键值 */
extern listnode_t* listKeyCreate(long node_key);

/* 在队内存创建一个空链表 */
extern list_t* listCreate();

/* 从对内存中清除一个链表
* 被调用程序负责清理节点数据,即该函数会调用节点清理函数(pfunc)
*/
extern void listDestroy(list_t *in_list, pfcb_list_node_free pfunc /* NULL for no use or a key node */);

/* 获取链表中节点的个数 */
extern size_t listSize(const list_t* in_list);

/* 按索引下标获取节点,索引下标从0开始,下标0的节点为头节点 */
extern listnode_t* listNodeAt(const list_t* in_list, int index);
#ifdef __cplusplus
}
#endif

#endif /* LIST_H */
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
qq_38241541
C语言 70,040 成员 243,237 内容