资源简介

用C语言做的字典程序,中使用链表和结构体,涉及的知识点有结构体,指针,链表的添加,遍历,删除,释放,文件的打开,读取,关闭等。使用Xcode写的代码,可以在Xcode4.2中直接运行,但是需要更改字典数据文件的路径,也可以在终端下编译运行,也可以直接把main.C拷贝到vc下去运行

资源截图

代码片段和文件信息

#include 
#include 
#include 
#include 
#define TRANS_LEN 1024
#define WORD_LEN 32
struct Dict{
    char name[WORD_LEN];
    char trans[TRANS_LEN];
    /*单链表,存放下一个节点的指针*/
    struct Dict *next;
};
void cal_time(struct timeval *prev struct timeval *cur);
void cal_time(struct timeval *prev struct timeval *cur)
{
time_t s;
suseconds_t m;
s = cur->tv_sec - prev->tv_sec;
m = cur->tv_usec - prev->tv_usec;
m += s*1000000;
printf(“time:%f(ms)\n“ (double)m / 1000);
}

//插入数据节点
void insertDict(struct Dict *headchar *dictWordchar *dictTrans);
void insertDict(struct Dict *headchar *dictWordchar *dictTrans)
{
    //创建一个节点存储每一行数据
    struct Dict *node=malloc(sizeof(struct Dict));
    strcpy(node->namedictWord);
    strcpy(node->transdictTrans);
    node->next=NULL;
    
    //将节点添加到头节点和第一个节点之间
    node->next  = head->next;
    head->next = node;
}
//释放链表
void deleteDictlinkdelist(struct Dict *head){
    struct Dict *p = head;
    while (p) {
        struct Dict *tmp = p->next;
        free(p);
        p=tmp;
    }
    head->next=NULL;
}

/*使用结构体数组来存放字典数据*/
//struct Dict dicts[100000]={};

/*读取字典文件*/
/*读取字典文件-函数声明*/
void findTrans(struct Dict *head);
void findTrans(struct Dict *head){
    /*读取字典文件*/
    FILE *fp = fopen(“/dict.txt““r“);    
    if (fp!=NULL) {
        char dictLine[TRANS_LEN]={};
        /*读取字典的每一行*/
        int dict_i=0;
        while (fgets(dictLineWORD_LENfp)) {
            int i;
            int j;
            char dictWord[WORD_LEN]={};
            /*获得单词*/  
            for (i=0j=1;j<(strlen(dictLine)-1);i++j++) {
                dictWord[i]=dictLine[j];
            }
            memset(dictLine0WORD_LEN);
            fgets(dictLineTRANS_LENfp);
            char dictTrans[TRANS_LEN]={};
            /*获得单词的解释*/
            for (i=0j=6;j<(strlen(dictLine)-1);i++j++) {
                    dictTrans[i]=dictLine[j];
            }
 
            /*使用链表来存放字典数据*/
            insertDict(headdictWorddictTrans);
            
            /*使用结构体数组存放字典数据*/
            //strcpy(dicts[dict_i].namedictWord);
            //strcpy(dicts[dict_i].transdictTrans);
            dict_i++;
        }
    }else{
        printf(“file not found“);
    }
    //关闭打开的文件流
    fclose(fp);
}
int main (int argc const char * argv[])
{
    //创建头节点,不要忘记为节点分配内存,分配内存的时候使用sizeof(数据类型)
    struct Dict *head = malloc(sizeof(struct Dict));
    //调用函数,读取字典文件,并将字典数据分割后加载进链表
    struct timeval prev cur;
gettimeofday(&prev NULL);
    findTrans(head);
    gettimeofday(&cur NULL);
cal_time(&prev &cur);//统计词库加载时间
    //输入的单词
    char word[WORD_LEN]={};
    char trans[TRANS_LEN]={};
    while (1) {
        //遍历链表查找单词的释义
        struct Dict *p=head->next;
        printf(“请输入英文单词(输入:exit退出):\n“);
        scanf(“%s“word);
        if (strcmp(word“:exit“)==0) {
            break;
        }
        while (p) {
            if (strcmp(p->nameword)==0) {
                strcpy(transp->trans);
                printf(“%s\

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       6148  2013-04-18 19:58  ProjectDictionary\.DS_Store

     文件       6148  2013-04-18 19:58  ProjectDictionary\ProjectDictionary\.DS_Store

     文件    2007645  2011-04-19 15:06  ProjectDictionary\ProjectDictionary\dict.txt

     文件       3762  2013-04-18 20:34  ProjectDictionary\ProjectDictionary\main.c

     文件       3140  2013-04-15 09:32  ProjectDictionary\ProjectDictionary\ProjectDictionary.1

     文件       6711  2013-04-15 09:32  ProjectDictionary\ProjectDictionary.xcodeproj\project.pbxproj

     文件        162  2013-04-15 09:32  ProjectDictionary\ProjectDictionary.xcodeproj\project.xcworkspace\contents.xcworkspacedata

     文件      13286  2013-04-18 20:34  ProjectDictionary\ProjectDictionary.xcodeproj\project.xcworkspace\xcuserdata\qianfeng.xcuserdatad\UserInterfaceState.xcuserstate

     文件         91  2013-04-15 15:30  ProjectDictionary\ProjectDictionary.xcodeproj\xcuserdata\qianfeng.xcuserdatad\xcdebugger\Breakpoints.xcbkptlist

     文件       3160  2013-04-15 09:32  ProjectDictionary\ProjectDictionary.xcodeproj\xcuserdata\qianfeng.xcuserdatad\xcschemes\ProjectDictionary.xcscheme

     文件        489  2013-04-15 09:32  ProjectDictionary\ProjectDictionary.xcodeproj\xcuserdata\qianfeng.xcuserdatad\xcschemes\xcschememanagement.plist

     目录          0  2013-04-18 20:34  ProjectDictionary\ProjectDictionary.xcodeproj\project.xcworkspace\xcuserdata\qianfeng.xcuserdatad

     目录          0  2013-04-15 15:30  ProjectDictionary\ProjectDictionary.xcodeproj\xcuserdata\qianfeng.xcuserdatad\xcdebugger

     目录          0  2013-04-15 09:32  ProjectDictionary\ProjectDictionary.xcodeproj\xcuserdata\qianfeng.xcuserdatad\xcschemes

     目录          0  2013-04-15 09:32  ProjectDictionary\ProjectDictionary.xcodeproj\project.xcworkspace\xcuserdata

     目录          0  2013-04-15 10:42  ProjectDictionary\ProjectDictionary.xcodeproj\xcuserdata\qianfeng.xcuserdatad

     目录          0  2013-04-15 09:32  ProjectDictionary\ProjectDictionary.xcodeproj\project.xcworkspace

     目录          0  2013-04-15 09:32  ProjectDictionary\ProjectDictionary.xcodeproj\xcuserdata

     目录          0  2013-04-18 20:34  ProjectDictionary\ProjectDictionary

     目录          0  2013-04-15 09:32  ProjectDictionary\ProjectDictionary.xcodeproj

     目录          0  2013-04-18 14:51  ProjectDictionary

----------- ---------  ---------- -----  ----

              2050742                    21


评论

共有 条评论