- 论坛徽章:
- 0
|
关于单词的头文件如下:
#ifndef __WORD_H__
#define __WORD_H__
typedef struct trans{
char *trans;
struct trans *next;
}Trans,*Ptrans;
typedef struct word{
char *word;
Ptrans head_trans;
struct word *next;
}Word,*Pword;
Pword create_head();
Pword create_word(char *);
int create_trans(Pword,char *);
int print_trans(Pword);
#endif
关于单词的.C文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"word.h"
Pword create_head()//创建空的单词
{
Pword p = (Pword)malloc(sizeof(Word));
if (p == NULL)
{
printf("creat word error\n");
return NULL;
}
p->word = NULL;
p->head_trans = NULL;
p->next = NULL;
return p;
}
Pword create_word(char *buff)//创建单词
{
Pword p = (Pword)malloc(sizeof(Word));
if(p == NULL)
{
printf("pword malloc error\n");
return NULL;
}
p->word = (char*)malloc(sizeof(char) * (strlen(buff) + 1));
strcpy(p->word,buff);
p->next = NULL;
p->head_trans = NULL;
return p;
}
int create_trans(Pword pword,char *buff)//创建单词的一个解释
{
int i = strlen(buff);
Ptrans p = pword->head_trans;
while(p != NULL)
{
p = p->next;
}
p = (Ptrans)malloc(sizeof(Trans));
p->trans = (char*)malloc(sizeof(char)*(i + 1));
strcpy(p->trans,buff);
p->next = NULL;
return 0;
}
int print_trans(Pword pword)//打印单词所有解释
{
if(pword == NULL)
{
printf("pword error\n");
return -1;
}
int i = 1;
Ptrans p = pword->head_trans;
while(p != NULL)
{
printf("解释%d:%s\n",i,p->trans);
i++;
p = p->next;
}
return 0;
}
从词典中读取建立哈希链表如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"hash.h"
Phash create_hash()
{
Phash p = (Phash)malloc(sizeof(Hash));
int i = 0;
for(i = 0; i < 27; i++)
{
p->hash = create_head();
}
return p;
}
int hash_number(char *word)
{
int i = word[0] - 'a';
if(i < 0 || i > 25)
{
return 26;
}
return i;
}
int read_txt(Phash p)
{
FILE *fp = fopen(FILE_PATH,"r");
if(fp == NULL)
{
perror("open error");
return -1;
}
char a[100] = {0};
char b[1000] = {0};
char c[500] = {0};
int i = 0;
int j = 0;
int k = 0;
fgetc(fp);
// while(feof(fp) == 0)
// {
i = 0;
do{
a = fgetc(fp);
i++;
}while(a[i-1] != '\n');
a[i-1] = '\0';
Pword pword = create_word(a);
Ptrans ptrans = (Ptrans)malloc(sizeof(Trans));
int n = hash_number(a);
for(i = 0; i < 6; i++)
{
fgetc(fp);
}
fgets(b,1000,fp);
i = strlen(b);
b = '\0';
k = 0;
for(j = 0; j < i+1; j++)
{
if(b[j] == '@')
{
c[k] = '\0';
create_trans(pword,c);
k = 0;
}
else
{
c[k] = b[j];
k++;
}
}
create_trans(pword,c);
print_trans(pword);
#if 0
insert_list(p->hash[n],pword);
#endif
fgetc(fp);
// }
return 0;
}
问题就是在建立哈希链表的时候,红色代码执行时并没有打印出单词的解释,就是蓝色部分p等于 NULL了,没有进入while循环不知道为什么,按理说是要进入循环的,这是为什么??create_trans是可以执行的,求高人解答
|
|