一位网友正在学校做课程设计题目,要求在一个文件中找到给定单词出现的位置并统计出现次数。这是一个比较简单的文本处理问题, 于是, 我给他用 python 写了一个,并打赌在5分钟内用不到30行程序解决问题。
下面是程序:
if __name__=='__main__':
file_name = raw_input('Input the file you want to find in:')
try:
in_file = open(file_name,'r')
lines = in_file.readlines()
tag_tok = ''
while tag_tok.upper() != 'Q':
tag_tok = raw_input('Input the Word you want to find(Q for quit):')
if tag_tok.upper() != 'Q':
count = 0
line_no = 0
for line in lines:
line_no = line_no + 1
inline_cnt = line.count(tag_tok)
count = count + inline_cnt
if inline_cnt > 0:
print 'Find %s %d time(s) in line :%d'%(tag_tok,inline_cnt,line_no)
print line
print '---------------------------------'
print 'Total fount %s %d time(s)'%(tag_tok, count)
except:
print "Can't open file %s"%(file_name)
但是,这个网友还不满足非要一个 C++的程序,理由是他们老师不会python , 正好我也想试试用C++解决和python做下对比:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int BruteFind(const char *x, int m, const char *y, int n ,vector<int>& colpos) {
int i, j, cnt=0;
/* Searching */
for (j = 0; j <= n - m; ++j) {
for (i = 0; i < m && x == y[i + j]; ++i);
if (i >= m){
colpos[cnt++] = j;
if(cnt == colpos.size())
colpos.resize(cnt * 2);
}
}
return cnt;
}
int count_string(string source, string tag, vector<int>& colpos){
int find_cnt = 0;
find_cnt = BruteFind(tag.c_str(), tag.size(), source.c_str(),source.size(),colpos);