回 帖 发 新 帖 刷新版面

主题:求如何在文本编辑器中实现统计字母数功能

一个可以读、存、写的文本编辑器,支持TXT格式,不支持中文输入。请问如何实现一个功能,统计文本中各字母的数量,比如,字母“a”有多少个,"b"有多少个。

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

在文本编辑器中实现统计字母功能,文本编辑器部分代码如下:
case WM_COMMAND:
     ...
     switch (LOWORD (wParam))
     {....
     case IDM_FORMAT_COUNT:
          //然后在这里就是我求的统计功能函数啦
     return 0;
     ...
     }

求救啊~~
     

回复列表 (共8个回复)

沙发

#include <iostream.h>
#include <string>
#include <fstream>
#include <map>
using namespace std;

map<char,int> mapStatistic;

int main(int argc, char* argv[])
{
    for(char c='a';c<='z';c++)
    {
        mapStatistic[c]=0;
    }

    for(char C='A';C<='Z';C++)
    {
        mapStatistic[C]=0;
    }
    
    ifstream in("ConsoleTest.cpp");
    string line;
    while(getline(in,line))
    {
        const char *p=line.c_str();
        while(*p!='\0')
        {
            if(::isalpha(*p))
            {
                mapStatistic[*p]++;
            }
            p++;
        }
    }

    map<char,int>::iterator iter;
    for(iter=mapStatistic.begin();iter!=mapStatistic.end();iter++)
    {
        cout<<iter->first<<" "<<iter->second<<endl;
    }
    return 0;
}

板凳

问题是我要整合进文本编辑器里去,相当于一个功能,而不是一个单独的程序啊

3 楼

高手你还在么?

ifstream in("ConsoleTest.cpp");

这句,怎样把ConsoleTest.cpp变成一个变量可以随时传文件名进去而不是特定一个文件名呢。
[em1]

4 楼

你可以把我写的这些代码改成一个函数,文件名就是这个函数的参数
比如这个函数叫void CharStatistic(string strFileName)

5 楼

我试了一下午都不行。你给我的代码有时编译通过,有时又有9个警告。我只晕。警告如下:
d:\program files\microsoft visual studio\vc98\include\xtree(182) : warning C4786: 'std::_Tree<char,std::pair<char const ,int>,std::map<char,int,std::less<char>,std::allocator<int> >::_Kfn,std::less<char>,std::allocator<int> >::~_Tree<char,std::pair<
char const ,int>,std::map<char,int,std::less<char>,std::allocator<int> >::_Kfn,std::less<char>,std::allocator<int> >' : identifier was truncated to '255' characters in the debug information
d:\program files\microsoft visual studio\vc98\include\xtree(162) : warning C4786: 'std::_Tree<char,std::pair<char const ,int>,std::map<char,int,std::less<char>,std::allocator<int> >::_Kfn,std::less<char>,std::allocator<int> >::_Tree<char,std::pair<c
har const ,int>,std::map<char,int,std::less<char>,std::allocator<int> >::_Kfn,std::less<char>,std::allocator<int> >' : identifier was truncated to '255' characters in the debug information
d:\program files\microsoft visual studio\vc98\include\xtree(236) : warning C4786: '__ehhandler$?insert@?$_Tree@DU?$pair@$$CBDH@std@@U_Kfn@?$map@DHU?$less@D@std@@V?$allocator@H@2@@2@U?$less@D@2@V?$allocator@H@2@@std@@QAE?AU?$pair@Viterator@?$_Tree@DU
?$pair@$$CBDH@std@@U_Kfn@?$map@DHU?$less@D@std@@V?$allocator@H@2@@2@U?$less@D@2@V?$allocator@H@2@@std@@_N@2@ABU?$pair@$$CBDH@2@@Z' : identifier was truncated to '255' characters in the debug information
d:\program files\microsoft visual studio\vc98\include\xtree(236) : warning C4786: '__unwindfunclet$?insert@?$_Tree@DU?$pair@$$CBDH@std@@U_Kfn@?$map@DHU?$less@D@std@@V?$allocator@H@2@@2@U?$less@D@2@V?$allocator@H@2@@std@@QAE?AU?$pair@Viterator@?$_Tre
e@DU?$pair@$$CBDH@std@@U_Kfn@?$map@DHU?$less@D@std@@V?$allocator@H@2@@2@U?$less@D@2@V?$allocator@H@2@@std@@_N@2@ABU?$pair@$$CBDH@2@@Z$0' : identifier was truncated to '255' characters in the debug information
d:\program files\microsoft visual studio\vc98\include\utility(21) : warning C4786: 'std::pair<std::_Tree<char,std::pair<char const ,int>,std::map<char,int,std::less<char>,std::allocator<int> >::_Kfn,std::less<char>,std::allocator<int> >::iterator,bo
ol>::pair<std::_Tree<char,std::pair<char const ,int>,std::map<char,int,std::less<char>,std::allocator<int> >::_Kfn,std::less<char>,std::allocator<int> >::iterator,bool>' : identifier was truncated to '255' characters in the debug information

count.obj - 0 error(s), 9 warning(s)

然后我下午也把他改成一个函数加去后编译又出现这样的错:
Compiling...
PopCount.cpp
d:\c++project\poppad3\popcount.cpp(7) : error C2220: warning treated as error - no object file generated
d:\c++project\poppad3\popcount.cpp(7) : warning C4786: 'std::reverse_bidirectional_iterator<std::_Tree<char,std::pair<char const ,int>,std::map<char,int,std::less<char>,std::allocator<int> >::_Kfn,std::less<char>,std::allocator<int> >::iterator,std:
:pair<char const ,int>,std::pair<char const ,int> &,std::pair<char const ,int> *,int>' : identifier was truncated to '255' characters in the debug information
d:\c++project\poppad3\popcount.cpp(7) : warning C4786: 'std::reverse_bidirectional_iterator<std::_Tree<char,std::pair<char const ,int>,std::map<char,int,std::less<char>,std::allocator<int> >::_Kfn,std::less<char>,std::allocator<int> >::const_iterato
r,std::pair<char const ,int>,std::pair<char const ,int> const &,std::pair<char const ,int> const *,int>' : identifier was truncated to '255' characters in the debug information
d:\c++project\poppad3\popcount.cpp(7) : warning C4786: 'std::pair<std::_Tree<char,std::pair<char const ,int>,std::map<char,int,std::less<char>,std::allocator<int> >::_Kfn,std::less<char>,std::allocator<int> >::iterator,std::_Tree<char,std::pair<char
 const ,int>,std::map<char,int,std::less<char>,std::allocator<int> >::_Kfn,std::less<char>,std::allocator<int> >::iterator>' : identifier was truncated to '255' characters in the debug information
d:\c++project\poppad3\popcount.cpp(7) : warning C4786: 'std::pair<std::_Tree<char,std::pair<char const ,int>,std::map<char,int,std::less<char>,std::allocator<int> >::_Kfn,std::less<char>,std::allocator<int> >::const_iterator,std::_Tree<char,std::pai
r<char const ,int>,std::map<char,int,std::less<char>,std::allocator<int> >::_Kfn,std::less<char>,std::allocator<int> >::const_iterator>' : identifier was truncated to '255' characters in the debug information
Error executing cl.exe.

PopCount.obj - 1 error(s), 4 warning(s)

要不我把整个代码发给你你帮我整~

[em2]

6 楼

帮我看下改成的函数代码对不

#include <iostream>
#include <string>
#include <fstream>
#include <map>
using namespace std;

void CharStatistic(string strFileName)
{
    map<char,int> mapStatistic;
    
    for (char c = 'a'; c <= 'z'; c++)
    {
        mapStatistic[c] = 0;
    }

    for (char C = 'A'; C <= 'Z'; C++)
    {
        mapStatistic[C] = 0;
    }

    ifstream in(strFileName);
    string line;
    while(getline(in,line))
    {
        const char *p = line.c_str();
        while(*p != '\0')
        {
            if (::isalpha(*p))
            {
                mapStatistic[*p]++;
            }
            p++;
        }

        map<char,int>::iterator iter;
        for(iter = mapStatistic.begin(); iter != mapStatistic.end(); iter++)
        {
            cout<<iter->first<<" "<<iter->second<<endl;
        }
    }
}


--------------------Configuration: PopPad3 - Win32 Debug--------------------
Compiling...
PopCount.cpp
D:\c++project\PopPad3\PopCount.cpp(21) : error C2664: '__thiscall std::basic_ifstream<char,struct std::char_traits<char> >::std::basic_ifstream<char,struct std::char_traits<char> >(const char *,int)' : cannot convert parameter 1 from 'class std::bas
ic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

PopCount.obj - 1 error(s), 0 warning(s)

7 楼

#include <iostream.h>
#include <string>
#include <fstream>
#include <map>
using namespace std;

map<char,int> mapStatistic;

void CharStatistic(const char* strFileName)
{
    for(char c='a';c<='z';c++)
    {
        mapStatistic[c]=0;
    }
    
    for(char C='A';C<='Z';C++)
    {
        mapStatistic[C]=0;
    }
    
    ifstream in(strFileName);
    string line;
    while(getline(in,line))
    {
        const char *p=line.c_str();
        while(*p!='\0')
        {
            if(::isalpha(*p))
            {
                mapStatistic[*p]++;
            }
            p++;
        }
    }
    
    map<char,int>::iterator iter;
    for(iter=mapStatistic.begin();iter!=mapStatistic.end();iter++)
    {
        cout<<iter->first<<" "<<iter->second<<endl;
    }
}

int main(int argc, char* argv[])
{
    CharStatistic("ConsoleTest.cpp");
    
    return 0;
}


不好意思,那个函数的参数改成cosnt char *就没问题了
警告是map的,我也没办法

8 楼

d:\c++project\poppad3\popcount.cpp(43) : error C2220: warning treated as error - no object file generated
这个错误是什么意思?

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

我来回复

您尚未登录,请登录后再回复。点此登录或注册