dedecms织梦内容管理系统    
首页 | java | C/C++ | PHP | 操作系统 | ajax | 脚本编程 | 安全技术 | 本站下载页 | 专题 | QQ群 | 测试中心 | 会员中心 | 积分规则
  当前位置:主页>脚本编程>python>文章内容
Python 与 C++ 程序的简单实例对比
来源:巧巧读书 作者:巧巧读书

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

  return find_cnt;
}

int main(){
string file_name, line;
vector<string> lines;
  lines.resize(10);
  
cout << "Input the file name:";
cin  >> file_name;

ifstream in_file;
try{
     in_file.open(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 == lines.size()){
     lines.resize(line_count * 2);
    }
  }while(in_file.eof()==0);

string tag_tok;
vector<int> colpos;
colpos.resize(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_file.close();
return 0;
}

这个程序用了30分钟。

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

Python  5 分钟 22行

C++       30 分钟 60多行

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


上一篇:收集的一些python程序,实用并且有趣   下一篇:网站入侵10大隐患
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·C语言数组排序小结
·Python初探
·收集的一些python程序,实用并且
·c++ 数组与指针
·C++中的内存管理(new、delete、
·Python企业应用的优缺点
·如何用C语言开发DSP嵌入式系统
·如何用C语言开发DSP嵌入式系统
·Linux下C语言编程
·学习arm的话先看哪本书?
·C++初学者应该关心的优秀图书一
·利用C语言小程序来解决大问题
  相关文章
·收集的一些python程序,实用并且
·Python初探
·Python企业应用的优缺点
  相关信息
copy right @ 百家拳软件项目研究室 2007 辽ICP备07011763