• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    Python與C++ 程序的簡單實例對比

    發布: 2007-7-04 20:06 | 作者: admin | 來源:  網友評論 | 查看: 75次 | 進入軟件測試論壇討論

    領測軟件測試網 一位網友正在學校做課程設計題目,要求在一個文件中找到給定單詞出現的位置并統計出現次數。這是一個比較簡單的文本處理問題, 于是, 我給他用 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++語言中在開發時的差異了。

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>