// 再试试,这次把所有的Keyword 都统计了一下
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
class Keyword
{
string strKeyword;
int count;
public:
Keyword(string k, int n = 0)
{
strKeyword = k;
count = n;
}
void include(const string & word)
{
int pos = word.find(strKeyword);
if(pos == 0)
{
if(strKeyword == word)
count++;
else if(word.length() > strKeyword.length())
{
if( isalnum(word.at(strKeyword.length())) == false)
count++;
}
}
else if(pos != -1)
{
if(pos + strKeyword.length() == word.length())
{
if(isalnum(word.at(pos-1)) == false)
count++;
}
else
{
if(isalnum(word.at(pos-1)) == false
&& isalnum(word.at(pos +strKeyword.length())) == false)
count++;
}
}
}
/*
void justForTest()
{
while(true)
{
break;
}
do
{
cout<<" just for test"<<endl;
break;
}while(true);
}
*/
void showResult()
{
cout<<"Count ofkeyword"<<"\""<<strKeyword<<"\":"<<count<<endl;
}
};
class CountOfKeywords
{
vector<Keyword> keywordVC;
string filename;
string text;
public:
CountOfKeywords(string filename)
{
this->filename = filename;
ifstream fin(filename.c_str());
ostringstream outstr;
string line;
do
{
getline(fin, line);
int loc = line.find("//");
if(loc != string::npos)
line.erase(line.begin() + loc, line.end());
int locStringBegin;
int locStringEnd;
char ch[2] = {92, 34};
bool found = false;
do
{
found = false;
locStringBegin = line.find(ch[0]);
if(locStringBegin != string::npos)
{
if(line.at(locStringBegin+1) ==ch[1])
{
line.at(locStringBegin) = '';
line.at(locStringBegin+1) = ' ';
found = true;
}
}
}while(found);
do
{
locStringBegin = line.find("\"");
if(locStringBegin != string::npos)
{
locStringEnd = line.find("\"", locStringBegin+1);
if(locStringEnd != string::npos)
line.erase(line.begin() +locStringBegin, line.begin() + locStringEnd +1);
}
}while(locStringBegin != string::npos && locStringEnd!=string::npos);
outstr<<line<<endl;
}while(!fin.eof());
text = outstr.str();
do
{
int posCommentBegin = text.find("/*");
if(posCommentBegin != string::npos)
{
int posCommentEnd = text.find("*/", posCommentBegin);
if(posCommentEnd != string::npos)
{
text.erase(text.begin() +posCommentBegin,text.begin() + posCommentEnd+2);
}
else
break;
}
else
break;
}while(true);
}
string getText()
{
return text;
}
void addKeyword(string k)
{
keywordVC.push_back( Keyword(k) );
}
void calCountOfKeyword()
{
istringstream instr(text);
string word;
while(instr>>word)
{
for(int i = 0; i<keywordVC.size(); i++)
{
keywordVC.at(i).include(word);
}
}
}
void showResult()
{
cout<<"statistical result: "<<endl;
for(int i = 0; i<keywordVC.size(); i++)
{
keywordVC.at(i).showResult();
}
}
};
int main()
{
string strKw[63] = {"asm", "auto", "bool", "break", "case",
"catch", "char", "class", "const", "const_cast",
"continue", "default", "delete", "do","double",
"dynamic_cast", "else", "enum", "explicit","export",
"extern", "false", "float", "for", "friend",
"goto", "if", "inline", "int", "long",
"mutable", "namespace", "new", "operator","private",
"protected", "public", "register","reinterpret_cast", "return",
"short", "signed", "sizeof", "static","static_cast",
"struct", "switch", "template", "this","throw",
"true", "try", "typedef", "typeid", "typename",
"union", "unsigned", "using", "virtual","void",
"volatile", "wchar_t", "while"};
int *intchar = NULL;
// just for test
CountOfKeywords cks = CountOfKeywords("test01.cpp");
//将该文件存为 test01.cpp
for(int i = 0; i<63; i++)
{
cks.addKeyword(strKw[i]);
}
cks.calCountOfKeyword();
cks.showResult();
system("pause");
return 0;
}