m0_59857812 2021-07-02 08:47 采纳率: 36.4%
浏览 76
已采纳

c语言中文件中单词排序

img

  • 写回答

3条回答 默认 最新

  • qfl_sdu 2021-07-02 09:52
    关注

    代码如下:如有帮助,请采纳一下,谢谢。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct WordNode
    {
        char word[30];
        int nmb;
        struct WordNode* next;
        WordNode(){next = 0;}
    };
    
    class WordCount
    {
    public:
        WordCount(){head = 0;}
        ~WordCount()
        {
            WordNode* node = 0;
            while(head)
            {
                node = head->next;
                delete head;
                head = node;
            }
        }
        WordNode* findNode(char *p);     //查找单词
        void insertNode(WordNode* node); //插入单词
        void addCount(WordNode* node);  //单词的数量加1
        void display();   //显示
    private:
        struct WordNode* head;
    };
    
    //判断是否为空
    int isEmpty(char* p)
    {
        while(*p)
        {
            if(*p > 0x20 )
                return 0;
            p++;
        }
        return 1;
    }
    //查找单词
    WordNode* WordCount::findNode(char* p)
    {
        struct WordNode* node = head;
        while(node)
        {
            if (strcmp(p,node->word) == 0)
            {
                return node;
            }else
            {
                node = node->next;
            }
        }
        return 0;
    }
    //插入单词
    void WordCount::insertNode(struct WordNode* node)
    {
        //printf(" >> insert node:%s\n",node->word);
        struct WordNode* P;
        struct WordNode* pre;
        if (head == 0)
        {
            head = node;
            return;
        }
        P = head;
        pre = head;
        while(P)
        {
            if (strcmp(P->word,node->word) < 0 )  //降序排列
            {
                if(P == head)
                {
                    node->next = head;
                    head = node; //插入头部
                }else
                {
                    pre->next = node;
                    node->next = P;
                }
                break;
            }else
            {
                pre = P;
                P = P->next;
                //插入尾部
                if(P==0)
                {
                    pre->next = node;
                    break;
                }
            }
        }
    }
    //单词的数量加1
    void WordCount::addCount(struct WordNode* node)
    {
        node->nmb += 1;
        //printf("   >> %s :%d\n",node->word,node->nmb);
    }
    
    //显示
    void WordCount::display() 
    {
        struct WordNode* node = head;
        while(node)
        {
            printf("%s\t%d\n",node->word,node->nmb);
            //cout << node->word << "\t" << node->nmb << endl;
            node = node->next;
        }
    }
    bool isctrl(char ch)
    {
        if (ch < 0x1F || ch == 0x7F)
        {
            return true;
        }
        return false;
    }
    //删除空格,并将大写转变成小写
    void toLower(char tt[])
    {
        int i=0,j=0;
        while(tt[i] != '\0')
        {
            if( (tt[i] != ' ') && (!isctrl(tt[i])) )
            {
                if(tt[i] >= 'A' && tt[i] <= 'Z')
                    tt[j++] = tt[i] + 32;
                else
                    tt[j++] = tt[i];
            }
            i++;
        }
    }
    //buf是存储文件的缓冲区,lSize是文件大小
    char* textFileRead(char* filename,int *lSize)
    {
        char* buf;
        FILE *pf = fopen(filename,"r");
        fseek(pf,0,SEEK_END);
        *lSize = ftell(pf);
        // 用完后需要将内存free掉
        rewind(pf); 
        buf = new char[*lSize+1];
        *lSize = fread(buf,sizeof(char),*lSize,pf);
        buf[*lSize] = '\0';
        return buf;
    }
    
    int main()
    {
        int size,i=0,j= 0;
        char text[50] = {0};
        WordCount cc;
        struct WordNode* node = 0;
        char* buf = textFileRead("textfile.txt",&size);
        
        if(size <= 0)
        {
            printf("文件打开失败,或者为空\n");
            return 0;
        }
        
        while( i <= size)
        {
            if (i == size)
            {
                text[j] = '\0';
                toLower(text);
                if( isEmpty(text) )
                {
                    j = 0;
                    i++;
                    continue;
                }
                node = 0;
                node = cc.findNode(text);
                if (node)
                {
                    cc.addCount(node);
                }else
                {
                    node = (struct WordNode*)malloc(sizeof(WordNode));
                    strcpy(node->word ,text);
                    node->nmb = 1;
                    node->next = 0;
                    cc.insertNode(node);
                }
                break;
            }else
            {
                //此处不考虑中间含有.的单词,入地名,缩写等等
                if (buf[i] == ' ' || buf[i] == '\r' || buf[i] == ',' || buf[i] == '.' || buf[i] == '\n'|| buf[i] == '!' || isctrl(buf[i])|| buf[i] == ';')
                {
                    text[j] = '\0';
                    toLower(text);
                    if(isEmpty(text))
                    {
                        j = 0;
                        i++;
                        continue;
                    }
                    node = 0;
                    node = cc.findNode(text);
                    if (node)
                    {
                        //printf("find ,add count\n");
                        cc.addCount(node);
                    }else
                    {
                        //printf("not find,create\n");
                        node = (struct WordNode*)malloc(sizeof(WordNode));//new WordNode;
                        strcpy(node->word,text);
                        node->nmb = 1;
                        node->next = 0;
                        cc.insertNode(node);
                    }
                    j = 0;
                }else
                {
                    text[j++] = buf[i];
                }
                i++;
            }
        }
    
        cc.display();
    
        
    
    
        return 0;
    }
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建