锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
灝忎箵緇冮錛?br>
{
if (argc==1)
{
std::cout<<"鐢ㄦ硶: wubi <闇瑕佹煡璇㈢紪鐮佺殑姹夊瓧>"<<std::endl;
return 0;
}
// 鏂囦歡璺緞
char* szPath = "wubi.txt";
std::ifstream fin(szPath);
if (!fin)
{
std::cout<<"鏃犳硶鎵撳紑浜旂瑪緙栫爜鏂囦歡錛屾枃浠跺悕wubi.txt錛岃緗簬wubi.exe鍚岀洰褰曚腑"<<std::endl;
return -1;
}
// 閫氬父鎴戜滑榪欐牱璇誨彇涓涓枃鏈枃浠剁殑鍏ㄦ枃
std::string strText = std::string(std::istreambuf_iterator<char>(fin), std::istreambuf_iterator<char>());
typedef std::map<std::string, std::string> WubiMap;
WubiMap wubiMap;
int nLength = strText.length();
int nLeft = 0;
int nRight = -1;
// 璇誨叆浜旂瑪緙栫爜
std::string strWord;
std::string strCode;
while(nRight < nLength)
{
nLeft = nRight+1;
nRight = nLeft+1;
//tab鍓嶇殑閮ㄥ垎涓簁ey:strWord
while (nRight<nLength && strText[nRight] != '\t')
{
nRight++;
}
if(nRight < nLength)
{
strWord = strText.substr(nLeft, nRight - nLeft);
}
nLeft = nRight + 1;
nRight = nLeft + 1;
//鍥炶濺鍓嶇殑涓篊ode
while (nRight<nLength && strText[nRight] != '\n')
{
nRight++;
}
if(nRight < nLength)
{
strCode = strText.substr(nLeft, nRight - nLeft);
wubiMap[strWord]= strCode;
}
}
// 寰幆鏌ユ壘鐩稿叧姹夊瓧鐨勭紪鐮?/span>
std::string strLine=argv[1];
nLength= strLine.length();
for (int i=0; i<nLength; ++i)
{
// 鍒ゆ柇鏄眽瀛楃殑鍓嶅崐閮ㄥ垎
if (strLine[i]<0)
{
strWord= strLine.substr(i, 2);
std::cout<<strWord<<"\t"<<wubiMap[strWord]<<std::endl;
++i;
}else
{
std::cout<<strLine[i]<<"\t"<<"鏃犲搴旂紪鐮?/span>"<<std::endl;
}
}
return 0;
}
紺轟緥錛?br>D:\>wubi 鎴戞槸涓鏈功
鎴?nbsp; trnt
鏄?nbsp; jghu
涓 ggll
鏈?nbsp; sgd
涔?nbsp; nnhy
闄勶細python瑙e喅鏂規涓涓細
wbx={
'宸?/span>':'aaaa',
'寮?/span>':'aad',
'璋?/span>':'yywf',
'璦':'yyyy',
'寰?/span>':'ttgg',
'涓?/span>':'gii'}
if len(sys.argv)==1:
print('wubi <闇瑕佹煡鎵句簲絎旂紪鐮佺殑姹夊瓧>')
sys.exit(1)
argv=sys.argv[1:]
for line in argv:
for c in line:
if c in wbx:
print(' %s %s'%(c, wbx[c]))
elif ord(c)<128 or c in '錛屻傦紒~“”錛?/span>':
print(' %s'%c)
else:
print(' %s -->緙栫爜緙哄け'%c)
闄勶細浜旂瑪緙栫爜琛ㄤ竴寮狅細 wubi.7z
灝忎箵鍔犳補 :D
]]>
灝忎箵緇冮錛?br>
{
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)
{
//璁$畻褰撳墠鐨勫崟璇嶄釜鏁?/span>
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++浠g爜錛?br>
{
// 鏂囦歡璺緞
char* szPath = "C:\\text.txt";
std::ifstream fin(szPath);
if (!fin)
{
std::cout<<"Can not open file"<<std::endl;
return -1;
}
// 閫氬父鎴戜滑榪欐牱璇誨彇涓涓枃鏈枃浠剁殑鍏ㄦ枃
std::string strText = std::string(std::istreambuf_iterator<char>(fin), std::istreambuf_iterator<char>());
typedef std::map<std::string, int> CountMap;
CountMap counter;
int nLength = strText.length();
int nLeft = 0;
int nRight = -1;
while(nRight<nLength)
{
nLeft = nRight+1;
// 鎵懼埌絎竴涓槸瀛楁瘝鐨勪綅緗?/span>
while (nLeft<nLength && !isalnum(strText[nLeft]))
{
++nLeft;
}
nRight = nLeft+1;
// 鎵懼埌絎竴涓潪瀛楁瘝鐨勪綅緗?/span>
while (nRight<nLength && isalnum(strText[nRight]))
{
++nRight;
}
// 鍙杗Right-nLeft鍙互淇濊瘉鍙栧埌鐨勬槸涓涓獁ord錛屽叾涓笉浼氬惈鏈夊瓧絎?/span>
if (nRight < nLength)
{
// 鎻愬彇鍗曡瘝
std::string strWord = strText.substr(nLeft, nRight - nLeft);
// 鍔犲叆璁版暟鍣?/span>
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 浠g爜錛?br>
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))
灝忎箵鍔犳補錛?br>
]]>