与程序的简单实例对比

阀门2021年09月06日

一位友正在学校做课程设计题目,要求在一个文件中找到给定单词出现的位置并统计出现次数。这是一个比较简单的文本处理问题, 于是, 我给他用 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_adlines()

tag_tok = \'\'

while tag_per() != \'Q\':

tag_tok = raw_input(\'Input the word you want to find(Q for quit):\')

if tag_per() != \'Q\':

count = 0

line_no = 0

for line in lines:

line_no = line_no + 1

inline_cnt = unt(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 ,vectorint 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 == ze())

size(cnt * 2);

}

}

return cnt;

}

int count_string(string source, string tag, vectorint colpos){

int find_cnt = 0;

find_cnt = BruteFind(tag.c_str(), ze(), source.c_str(),ze(),colpos);

return find_cnt;

}

int main(){

string file_name, line;

vectorstring lines;

size(10);

cout \"Input the file name:\";

cin file_name;

ifstream in_file;

try{

in_en(file_name.c_str());

if(!in_file)

throw(file_name);

}

catch(string file_name){

cout \"Fatal error: File not found.\"endl;

exit(1);

}

int line_count = 0;

do{

getline(in_file, lines[line_count]);

line_count ++;

if(line_count == ze()){

size(line_count * 2);

}

}while(in_f()==0);

string tag_tok;

vectorint colpos;

size(10);

do{

cout \"Input the word you want to find(Q for quit):\";

cin tag_tok;

if(tag_tok == \"Q\"){

break;

}

int count = 0, line_no = 0 , inline_count;

for(line_no = 0 ;line_no line_count ; line_no++){

inline_count = count_string(lines[line_no], tag_tok, colpos);

count += inline_count;

if(inline_count 0){

cout \"Find \" tag_tok \" \" inline_count \" time(s) in line \" line_no ;

cout \" , column pos is ( \";

for(int i = 0 ;i inline_count ;i++){

cout colpos \' \';

}

cout \" )\" endl;

cout lines[line_no] endl;

}

}

cout \"--------------------------------\" endl;

cout \"Total fount \" tag_tok \" \" count \" time(s)\" endl;

}while(tag_tok != \"Q\");

in_ose();

return 0;

}

这个程序用了30分钟。

从程序长度和编程时间上,粗略对比下:

Python 5 分钟 22行

C++ 30 分钟 60多行

从这个简单的例子中可以大体看到 脚本语言与C++语言中在开发时的差异了。

查看本文来源

白银白癜风哪里治疗好
拉萨包皮包茎治疗哪家好
治疗高血脂最好的办法
相关阅读
熬猪油时,多加5样,掌握3个技巧,又白又香不想腥味,吃1年也不坏

煮食油时,多加5所发,受制于3个精准,又红又木没甜味,吃饭1年也;也 各...

2024-12-29
这项笔试重启!都昌招85人!

根据工作商量和非典型肺炎诊疗有关立即,经研究工作,决定启动时上饶市2...

2024-12-17
广州城终“迎”首胜!U23球员违规事件大连人选择弃权:判0-3爆冷

北京时间段7年初6日,在上海申花与会的线或上开会中所,在此之前年初了第...

2024-11-21
温州又多一所新所学校!

仲夏二月,艳阳高照 崭新校四区,首次亮相 6月8日上午9点 衢州设计该学院教...

2024-11-06
相约厦门旅商职校,成就梦想:休闲体育服务与管理专业欢迎您

【相约惠州市北区观光旅游商务职业学校 成就憧憬】 招生工程新技术介绍:...

2024-11-04
2022年7月7号生肖快乐指数。

2022年7月7号月历好船运基准。 文/易鑫 壬寅年 丁未月 辛酉日 星期四 农历八...

2024-08-30
友情链接