• <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>

            清風(fēng)竹林

            ぷ雪飄絳梅映殘紅
               ぷ花舞霜飛映蒼松
                 ----- Do more,suffer less

            單詞統(tǒng)計(jì)

            要求:讀取一個(gè)文本,然后統(tǒng)計(jì)里面出現(xiàn)的單詞,打印每個(gè)單詞出現(xiàn)的次數(shù)。僅僅考慮英文單詞的情形,不考慮中文

            小乓練題:
            int main(int argc, char* argv[]) 
            {    
                
            using namespace std;

                ifstream infile(
            "c:\\a.txt",ios::binary );
                
            if(!infile)
                {
                    cout
            <<"Can not open sourse file!"<<endl;
                    
            return 0;
                }
                
            //ofstream outfile("out.txt");
                
            //if(!outfile)
                
            //{
                
            //    cout<<"Can not open destination file!"<<endl;
                
            //}
                
                
            int nLength = 0;
                
            char * pBuffer;

                
            // get length of file:
                infile.seekg (0, ios::end);
                nLength 
            = infile.tellg();
                infile.seekg (
            0, ios::beg);
                
                
                
            //read the file to the buffer
                pBuffer = new char[nLength];
                memset(pBuffer, 
            0, nLength);
                infile.read(pBuffer,nLength);
                infile.close();

                
            //copy the buffer to the string s
                string s = pBuffer;
                delete[] pBuffer;
                pBuffer 
            = NULL;


                
            string temp;

                vector
            <string> vecSubstr;
                vector
            <int> vecCount;

                
            int pre=0,next=0;

                
            while(next<nLength)
                {
                    pre
            =next;
                    
            //find the word
                    while((next<nLength)&&isalnum(s[next]))
                    {
                        next
            ++;
                    }
                    
            if(pre!=next)
                    {
                        
            //計(jì)算當(dāng)前的單詞個(gè)數(shù)
                        temp = s.substr(pre,next-pre);
                        cout
            <<temp<<endl;
                        
            //std::vector<std::string>::iterator iter = std::find(vecSubstr.begin(), vecSubstr.end(), temp);
                        
            //if (vecSubstr.end() != iter)
                        
            //{
                        
            //    std::cout<<temp<<std::endl;
                        
            //}else
                        
            //{
                        
            //    vecSubstr.push_back(temp);
                        
            //}

                        unsigned 
            int iPosition=0;
                        

                        
            while(iPosition<vecSubstr.size())
                        {
                            
                            
            if(vecSubstr[iPosition].compare(temp)==0)
                                
            break;
                                
                            iPosition
            ++;
                            
                        }
                        
                        
            if (iPosition==vecSubstr.size())
                        {
                            vecSubstr.push_back(temp);
                            vecCount.push_back(
            1);
                        }
                        
            else
                        {
                            vecCount[iPosition]
            ++;
                        }            
                        
                    }
                    next
            ++;
                }
                
            for (int j=0;j<vecSubstr.size();j++)
                {
                    cout
            <<vecSubstr[j]<<endl<<vecCount[j]<<endl;
                }

                
            //for(int i=0;i<substr.size();i++)
                
            //{
                
            //    cout<<substr[i]<<endl;
                
            //    cout<<count[i]<<endl;
                
            //}

                
            //delete[] pBuffer;
                
            //pBuffer = NULL;

                system(
            "pause");

                
            return 0;
            }

            C++代碼:

            int main(int argc, char* argv[])
            {
                
                
            // 文件路徑
                char* szPath = "C:\\text.txt";
                
                std::ifstream fin(szPath);
                
            if (!fin)
                {
                    std::cout
            <<"Can not open file"<<std::endl;
                    
            return -1;
                }
                
            // 通常我們這樣讀取一個(gè)文本文件的全文
                std::string strText = std::string(std::istreambuf_iterator<char>(fin), std::istreambuf_iterator<char>());

                typedef std::map
            <std::stringint> CountMap;
                CountMap counter;

                
            int nLength = strText.length();
                
            int nLeft = 0;
                
            int nRight = -1;

                
            while(nRight<nLength)
                {
                    nLeft 
            = nRight+1;
                    
            // 找到第一個(gè)是字母的位置
                    while (nLeft<nLength && !isalnum(strText[nLeft]))
                    {
                        
            ++nLeft;
                    }
                    nRight 
            = nLeft+1;
                    
            // 找到第一個(gè)非字母的位置
                    while (nRight<nLength && isalnum(strText[nRight]))
                    {
                        
            ++nRight;
                    }
                    
            // 取nRight-nLeft可以保證取到的是一個(gè)word,其中不會(huì)含有字符
                    if (nRight < nLength)
                    {
                        
            // 提取單詞
                        std::string strWord = strText.substr(nLeft, nRight - nLeft);
                        
            // 加入記數(shù)器
                        counter[strWord]+=1;    
                    }        
                }
                
                
            // 打印輸出
                for (CountMap::iterator iter = counter.begin(); counter.end()!=iter; ++iter)
                {
                    std::cout
            <<iter->first<<"\t\t"<<iter->second<<std::endl;
                }

                system(
            "pause");
                
            return 0;
            }

            python 代碼:

            import re

            filepath
            =r'c:/text.txt'
            with open(filepath) as file:
                text
            =file.read()
                text
            =re.split('\W+', text)
                d
            ={}
                
            for item in text:
                    d[item]
            =d.get(item, 0) +1 
                
            for key, value in d.items():
                    
            print('%s\t\t%s'%(key, value))

            小乓加油!














            posted on 2008-11-19 10:29 李現(xiàn)民 閱讀(870) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 小乓練題

            午夜天堂精品久久久久| 国产精品成人99久久久久| 狠狠色丁香婷婷综合久久来来去| 久久久久亚洲AV无码永不| 偷偷做久久久久网站| 久久伊人五月丁香狠狠色| 狠狠色噜噜色狠狠狠综合久久| 中文字幕无码久久精品青草| 无码国内精品久久人妻麻豆按摩 | 久久91精品国产91久久户| 亚洲人成网亚洲欧洲无码久久| 久久亚洲日韩看片无码| 精品国产乱码久久久久久人妻| 久久香蕉超碰97国产精品| 久久国产乱子伦精品免费强| 91精品国产高清久久久久久91| 久久久久久国产精品免费免费| 久久只有这精品99| 99久久国产精品免费一区二区| 欧美va久久久噜噜噜久久| 亚洲成色999久久网站| 久久精品一区二区影院| 一本一本久久aa综合精品| 久久精品国产亚洲av影院| 亚洲狠狠综合久久| 亚洲欧美一级久久精品| 久久亚洲精品无码AV红樱桃| 国产一级做a爰片久久毛片| 日本亚洲色大成网站WWW久久| 久久天天躁狠狠躁夜夜不卡 | 久久精品国产久精国产思思 | 久久久久久久国产免费看| 国产69精品久久久久9999APGF| 久久国产精品久久| 国产精品久久新婚兰兰| 久久久久国产精品| 日产精品久久久久久久| 99久久www免费人成精品| 亚洲午夜久久久久久久久电影网| 成人亚洲欧美久久久久| 久久精品九九亚洲精品|