• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 2,  comments - 8,  trackbacks - 0

            題目描述: 
            一個(gè)文件中存有空格分開(kāi)的單詞 
            現(xiàn)在要將這個(gè)文件讀入到另外一個(gè)文件,保存格式如下: 
            live evil 
            search casher 
            .. 
            就是含有相同字母的單詞要放到一行。 
            用c實(shí)現(xiàn),可以使用fopen,fread,fwrite等 
            接口: 
            bool TransferDictionary(char * OriginalFile,char * NewFile);
              


            網(wǎng)上一些大俠給出的解法的問(wèn)題主要在于不能區(qū)分諸如ab,abb,babba等含有相同字母,但是字母出現(xiàn)次數(shù)不相同的情況

            思路如下:
            gHeadList是一個(gè)鏈表的頭,其中的每一個(gè)HeadNode的child是一個(gè)指向同一類(lèi)的word的鏈表(這些word含有相同的hashcode,即含有相同的字母種類(lèi)并且各個(gè)字母的出現(xiàn)次數(shù)也相同),hashcode成員則保存該類(lèi)word排序后的值;

            具體解法如下:


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

            bool    TransferDictionary(char * srcFile,char * destFile); 

            //從源文件中讀入數(shù)據(jù)
            char*    ReadFile(const char* fname);

            //將處理后的數(shù)據(jù)寫(xiě)到目的文件中
            bool    WriteFile(const char* fname);

            //得到一個(gè)word的hash碼,實(shí)際就是返回word按字母排序后的字符串
            char*    GetHashCode(char *word);

            //將word插入到表中正確的位置
            bool    InsertWord(char * word);

            //釋放空間
            void    Free();


            struct Node
            {
                char *word;
                Node *next;
            };
            struct HeadNode
            {
                char*        hashcode;
                Node*        child;
                HeadNode*    link;
            };

            HeadNode *gHeadList=NULL;

            void Free()
            {
                HeadNode *pHead=gHeadList,*pTmpHead=NULL;
                Node *pTmp=NULL;
                while(NULL != pHead)
                {    
                    Node *pNode=pHead->child;
                    while(NULL != pNode)
                    {
                        pTmp=pNode->next;
                        free(pNode->word);
                        free(pNode);
                        pNode=pTmp;
                    }
                    
                    pTmpHead=pHead->link;

                    free(pHead->hashcode);
                    free(pHead);
                    pHead=pTmpHead;
                }
            }

            bool WriteFile(const char* fname)
            {
                FILE *fp=NULL;
                fp = fopen(fname,"w");

                HeadNode* hTmp = gHeadList;
                Node *nTmp = NULL;

                while (hTmp != NULL)
                {
                    nTmp = hTmp->child;

                    if(nTmp == NULL )continue;

                    fwrite(nTmp->word,sizeof(char),strlen(nTmp->word),fp);
                    printf("\n%s",nTmp->word);

                    nTmp=nTmp->next;
                    while (nTmp!=NULL)
                    {
                        fwrite(" ",sizeof(char),1,fp);            
                        fwrite(nTmp->word,sizeof(char),strlen(nTmp->word),fp);

                        printf(" %s",nTmp->word);
                        
                        nTmp = nTmp->next;
                    }
                    fwrite("\n",sizeof(char),1,fp);

                    hTmp = hTmp->link;
                }
                return true;
            }

            bool InsertWord(char * word)
            {
                char *hashcode=GetHashCode(word);

                Node *pNode=(Node*)malloc(sizeof(Node));
                pNode->word=strdup(word);
                pNode->next=NULL;

                HeadNode *pHead=gHeadList;

                while(pHead != NULL)
                {
                    if(0 == strcmp(pHead->hashcode,hashcode))
                    {
                        Node *pTmp=pHead->child;
                        pHead->child=pNode;
                        pNode->next=pTmp;
                        return true;
                    }

                    pHead=pHead->link;
                }

                pHead=(HeadNode*)malloc(sizeof(HeadNode));    
                pHead->hashcode=strdup(hashcode);
                pHead->child=pNode;
                pHead->link=gHeadList;

                gHeadList=pHead;
                return true;
            }

            char* GetHashCode(char *word)
            {
                char value[26]={0};
                int len=strlen(word);
                if(len<=0)return NULL;

                char *hashcode=(char*)malloc(len+1);
                int i=0;
                for(i=0;i<len;i++)
                    value[word[i]-'a']+=1;

                int pos=-1;
                for(i=0;i<26;i++)
                {
                    for(int j=0;j<value[i];j++)
                        hashcode[++pos]='a'+i;
                }
                hashcode[len]='\0';

                return hashcode;
            }

            char* ReadFile(const char* fname)
            {
                FILE *fp = fopen(fname,"r");
                if (fp == NULL)
                {
                    perror("Open file failed!\n");
                    exit(0);
                }

                fseek(fp,0L,SEEK_END);
                long fsize = ftell(fp);
                if (fsize==0L)
                {
                    perror("The file is empty!\n");
                    exit(0);
                }
                char *file_buf = (char*)malloc((size_t)fsize+1);
                rewind(fp);

                int Ret = fread(file_buf,sizeof(char),fsize,fp);

                if (Ret == 0)
                {
                    return NULL;
                }

                file_buf[fsize] = '\0';

                return file_buf;
            }


            bool TransferDictionary(char * OriginalFile,char * NewFile)
            {
                char * buf=ReadFile(OriginalFile);
                if(buf == NULL)return false;

                const char *delimer=" ";
                char * word=strtok(buf,delimer);
                while(word != NULL)
                {
                    printf("Get a word:%s\n",word);

                    InsertWord(strlwr(word));

                    word=strtok(NULL,delimer);
                }

                WriteFile(NewFile);

                return true;
            }


            int main()
            {
                char OriginalFile[100],TargetFile[100];

                memset(OriginalFile,0,100);
                memset(TargetFile,0,100);

                printf("Enter the original file name:");
                scanf("%s",OriginalFile);
                printf("Enter the target file name:");
                scanf("%s",TargetFile);

                TransferDictionary(OriginalFile,TargetFile);

                Free();

                return 0;
            }
            posted on 2007-06-28 21:24 Darkblue 閱讀(2121) 評(píng)論(4)  編輯 收藏 引用

            FeedBack:
            # re: 一道微軟的Mini-Test筆試題(一)
            2007-06-29 09:44 | WarTalker
            閣下的代碼清晰明了,讀后受益匪淺。  回復(fù)  更多評(píng)論
              
            # re: 一道微軟的Mini-Test筆試題(一)
            2007-06-29 14:11 | SuperPlayeR
            本來(lái)看到嘩嘩一長(zhǎng)串代碼就準(zhǔn)備跳過(guò)的,看到1樓的評(píng)論說(shuō)寫(xiě)的不錯(cuò),又回頭認(rèn)真讀了一遍,確實(shí)不虛此行,受教了~//bow //thanks  回復(fù)  更多評(píng)論
              
            # re: 一道微軟的Mini-Test筆試題(一)
            2007-06-30 19:49 | 麥田守望者
            強(qiáng)啊! 不知道題目在哪里獲取的啊?  回復(fù)  更多評(píng)論
              
            # re: 一道微軟的Mini-Test筆試題(一)
            2007-07-03 16:15 | Darkblue
            @麥田守望者
            題目也是在網(wǎng)上看見(jiàn)的,可能是以前參加mini-test的前輩帶出來(lái)的吧
              回復(fù)  更多評(píng)論
              

            只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            <2007年6月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            常用鏈接

            留言簿(1)

            隨筆檔案

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 久久亚洲精品无码VA大香大香| 国产精品久久久久一区二区三区| 秋霞久久国产精品电影院| 精品免费久久久久国产一区| 一级a性色生活片久久无少妇一级婬片免费放 | 久久久女人与动物群交毛片| 国产一级做a爰片久久毛片| 久久久这里有精品中文字幕| 久久国产精品99国产精| 蜜臀久久99精品久久久久久| 久久久噜噜噜www成人网| 久久99精品久久久久久噜噜 | 色诱久久av| 国产成人无码久久久精品一| 欧美久久久久久| 国产亚洲美女精品久久久| 国产精品禁18久久久夂久| 久久综合色老色| 久久午夜无码鲁丝片午夜精品| 成人国内精品久久久久影院| 久久久久久久女国产乱让韩| 久久精品亚洲乱码伦伦中文| 久久99国产精品二区不卡| 久久久久99精品成人片直播| 亚洲日韩中文无码久久| 久久婷婷色香五月综合激情| 蜜桃麻豆www久久国产精品| 国产香蕉97碰碰久久人人| 精品综合久久久久久97超人 | 亚洲国产小视频精品久久久三级| 国产V亚洲V天堂无码久久久| 精品国产乱码久久久久久1区2区| 热re99久久6国产精品免费| 日韩人妻无码精品久久久不卡| 中文字幕日本人妻久久久免费 | 人妻无码精品久久亚瑟影视 | 久久综合一区二区无码| 久久免费视频一区| 狠狠色丁香久久婷婷综合蜜芽五月 | 久久综合精品国产二区无码|