| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1816 人关注过本帖
标题:流程图 流程图化简
只看楼主 加入收藏
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
-每比较一个关键字就要把文件全部读一编- 你错了,文件在整个搜索中只读一遍,文件中的每一个字符只读一遍,这样为的正是最大限度的节省内存。还有一种做法正如你上面说的先将单词一一从文件中提取出来,放入容器,事后再与关键词比较,这样就有一个内存开销的问题。 如果要真正统计关键词的出现次数,那么实现的步骤如下: 1)打开文件 2)导入一个String中 3)去除注释部分,得到一个新的String text 4)去除" "中的内容,因为" "中的内容只是一个字符串,不属于统计范畴,这样我们得到一个待处理的String text 5)以 空格为开始判断标志, 以空格为结束判断标志,逐一从text中提取单词, 并且对提取出来的单词进行及时地处理,这里的处理指将单词后紧跟的 * , &符号 去掉, 因为 像 int& a = b; 这样的语句是合法的. 每当得到一个单词后, 就与容器中的关键词比较, 如与该关键词一样, 则该关键词的次数加一. 6) text 遍历完毕, 输出所有关键词的出现次数. 7) 程序结束

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-03 22:26
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
程序写了一下,你看看行不行,把这段程序存为 sourcefile.cpp , 你可以通过文本编译器打开这个文件,并搜寻某个关键词,比较人为统计的结果和程序自己统计的结果,看看有没有差错。
程序代码如下:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;

class Keyword
{
    string strKeyword;
    int count;
public:
    Keyword(string k, int n = 0)
    {
        strKeyword = k;
        count = n;
    }
    void include(const string & word)
    {
        int pos = word.find(strKeyword);
        if(pos == 0)
        {
            if(strKeyword == word)
                count++;
            else if(word.length() > strKeyword.length())
            {
                if( isalnum(word.at(strKeyword.length())) == false)
                    count++;
            }
        }
        else if(pos != -1)
        {
            if( isalnum(word.at(pos-1)) == false )
                count++;
        }
            
    }
    /*
    void justForTest()
    {
      while(true)
        {
          break;
        }
        do
        {
          cout<<" just for test"<<endl;
            break;
        }while(true);
    }
    */
    void showResult()
    {
        cout<<"Count of keyword"<<"\""<<strKeyword<<"\": "<<count<<endl;
    }
};

class CountOfKeywords
{
    vector<Keyword> keywordVC;
    string filename;
    string text;
public:
    CountOfKeywords(string filename)
    {
        this->filename = filename;
        ifstream fin(filename.c_str());
      ostringstream outstr;
      string line;
        
        do
        {
          getline(fin, line);
            int loc = line.find("//");
            if(loc != string::npos)
          line.erase(line.begin() + loc, line.end());
            int locStringBegin;
        int locStringEnd;
            char ch[2] = {92, 34};
            bool found = false;
            do
            {
                found = false;
                locStringBegin = line.find(ch[0]);
                if(locStringBegin != string::npos)
                {
                   if(line.at(locStringBegin+1) ==ch[1])
                    {
                      line.at(locStringBegin) = ' ';
                      line.at(locStringBegin+1) = ' ';
                        found = true;
                    }
                }
            }while(found);
            
            do
            {
                locStringBegin = line.find("\"");
        if(locStringBegin != string::npos)
                {
              locStringEnd = line.find("\"", locStringBegin+1);
              if(locStringEnd != string::npos)
             line.erase(line.begin() + locStringBegin, line.begin() + locStringEnd +1);
                }
           }while(locStringBegin != string::npos && locStringEnd !=string::npos);
         
            outstr<<line<<endl;
        }while(!fin.eof());
      text = outstr.str();

        do
        {
            int posCommentBegin = text.find("/*");
            if(posCommentBegin != string::npos)
            {
               int posCommentEnd = text.find("*/", posCommentBegin);
                if(posCommentEnd != string::npos)
                {
                   text.erase(text.begin() +posCommentBegin, text.begin() + posCommentEnd+2);
                }
                else
                    break;
            }
            else
                break;
        }while(true);
    }
    string getText()
    {
        return text;
    }
    void addKeyword(string k)
    {
        keywordVC.push_back( Keyword(k) );
    }
    void calCountOfKeyword()
    {
        istringstream instr(text);
        string word;
        
        while(instr>>word)
        {
            for(int i = 0; i<keywordVC.size(); i++)
            {
                keywordVC.at(i).include(word);
            }
        }
        
    }
    void showResult()
    {
        cout<<"statistical result: "<<endl;
        for(int i = 0; i<keywordVC.size(); i++)
        {
            keywordVC.at(i).showResult();
        }
    }
};

int main()
{
    string strKw1 = "int";
    string strKw2 = "char";
    string strKw3 = "void";
    string strKw4 = "while";
    string strKw5 = "if";

    int intchar = 0;  // just for test
   
    CountOfKeywords cks = CountOfKeywords("sourcefile.cpp");
//    string text = cks.getText();
//    cout<<text<<endl;

    cks.addKeyword(strKw1);
    cks.addKeyword(strKw2);
    cks.addKeyword(strKw3);
    cks.addKeyword(strKw4);
  cks.addKeyword(strKw5);

    cks.calCountOfKeyword();
   
    cks.showResult();

    system("pause");
    return 0;
}

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-04 03:56
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 
呵呵星期天休息,偷懒了一下,马上拿去分析看看,这回可要。。。。。
先谢谢热情的斑竹了 !!!

http://kongfuziandlife. http://codeanddesign.
2005-09-05 13:43
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 
似乎 无可挑剔了,但是毕竟我还很嫩,等到我多学了东西之后,我还是能呵呵。。。。
这个程序对于一个正确的书写的cpp的源文件可以完成统计了,
我们的讨论告一段落了

http://kongfuziandlife. http://codeanddesign.
2005-09-06 08:41
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
好像很不服气的样子,

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-06 15:55
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 
不负你的期望,呵呵。我又仔细的分析了你的程序。指出如下的bug 斑竹的keyword::include()设计的不完善。可以在测试的源程序中加入 *intchar 测关键字 int试试看。虽然有点吹毛求辟,但是程序是要完美的!!我声名一个叫 intchar 的指针是合法的,然而测试 int 的时候,int 被计入了。不对,我不知道include()函数还有什么其他的缺陷,反正慢慢就会发现的。 其实这个问题我是想到了解决的办法的就是else if(pos != -1) { if( isalnum(word.at(pos-1)) == false &&isalnum(word.at(pos+keyWord.length()))==false) count++; } 但是当我加入这一行后出现以下的情况 重新编译后 第一次可以运行,输出正确的结果,再次执行后程序 不正常中断(abnormal 什么的)。 问题还是要斑竹解决。 另外由于我是初学者有几行代码还是没能理解(很不想承认阿!) ; char ch[2] = {92, 34}; bool found = false; do { found = false; locStringBegin = line.find(ch[0]); if(locStringBegin != string::npos) { if(line.at(locStringBegin+1) ==ch[1]) { line.at(locStringBegin) = ' '; line.at(locStringBegin+1) = ' '; found = true; } } }while(found) 什么作用阿?

http://kongfuziandlife. http://codeanddesign.
2005-09-07 13:35
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
// 再试试,这次把所有的Keyword 都统计了一下
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;

class Keyword
{
    string strKeyword;
  int count;
public:
  Keyword(string k, int n = 0)
  {
        strKeyword = k;
    count = n;
    }
  void include(const string & word)
  {
        int pos = word.find(strKeyword);
    if(pos == 0)
        {
            if(strKeyword == word)
                count++;
            else if(word.length() > strKeyword.length())
      {
                if( isalnum(word.at(strKeyword.length())) == false)
                    count++;
            }
        }
       else if(pos != -1)
        {
            if(pos + strKeyword.length() == word.length())
            {
                if(isalnum(word.at(pos-1)) == false)
                    count++;
            }
            else
            {
                if(isalnum(word.at(pos-1)) == false
                   && isalnum(word.at(pos +strKeyword.length())) == false)
                    count++;
            }
        }
    }
  /*
  void justForTest()
  {
      while(true)
    {
      break;
    }
    do
    {
      cout<<" just for test"<<endl;
      break;
    }while(true);
  }
  */
    void showResult()
  {
        cout<<"Count ofkeyword"<<"\""<<strKeyword<<"\":"<<count<<endl;
  }
};

class CountOfKeywords
{
  vector<Keyword> keywordVC;
  string filename;
  string text;
public:
  CountOfKeywords(string filename)
  {
        this->filename = filename;
    ifstream fin(filename.c_str());
        ostringstream outstr;
        string line;
   
        do
    {
            getline(fin, line);
            int loc = line.find("//");
      if(loc != string::npos)
                line.erase(line.begin() + loc, line.end());
            int locStringBegin;
      int locStringEnd;
      char ch[2] = {92, 34};
      bool found = false;
      do
      {
                found = false;
                locStringBegin = line.find(ch[0]);
        if(locStringBegin != string::npos)
        {
                   if(line.at(locStringBegin+1) ==ch[1])
          {
                       line.at(locStringBegin) = '';
            line.at(locStringBegin+1) = ' ';
            found = true;
          }
        }
      }while(found);
            
      do
      {
              locStringBegin = line.find("\"");
        if(locStringBegin != string::npos)
        {
          locStringEnd = line.find("\"", locStringBegin+1);
          if(locStringEnd != string::npos)
                       line.erase(line.begin() +locStringBegin, line.begin() + locStringEnd +1);
                }
           }while(locStringBegin != string::npos && locStringEnd!=string::npos);
            outstr<<line<<endl;
        }while(!fin.eof());
        text = outstr.str();
   
        do
    {
            int posCommentBegin = text.find("/*");
      if(posCommentBegin != string::npos)
      {
               int posCommentEnd = text.find("*/", posCommentBegin);
        if(posCommentEnd != string::npos)
                {
                   text.erase(text.begin() +posCommentBegin,text.begin() + posCommentEnd+2);
        }
        else
                    break;
            }
      else
                break;
        }while(true);
    }
  string getText()
  {
        return text;
  }
   
    void addKeyword(string k)
  {
        keywordVC.push_back( Keyword(k) );
  }
  void calCountOfKeyword()
  {
        istringstream instr(text);
    string word;
        
        while(instr>>word)
    {
            for(int i = 0; i<keywordVC.size(); i++)
      {
                keywordVC.at(i).include(word);
            }
        }   
    }
  void showResult()
  {
        cout<<"statistical result: "<<endl;
    for(int i = 0; i<keywordVC.size(); i++)
    {
            keywordVC.at(i).showResult();
        }
    }
};

int main()
{
    string strKw[63] = {"asm", "auto", "bool", "break", "case",
                        "catch", "char", "class", "const", "const_cast",
                                         "continue", "default", "delete", "do","double",
                                         "dynamic_cast", "else", "enum", "explicit","export",
                                         "extern", "false", "float", "for", "friend",
                                         "goto", "if", "inline", "int", "long",
                                         "mutable", "namespace", "new", "operator","private",
                                         "protected", "public", "register","reinterpret_cast", "return",
                                         "short", "signed", "sizeof", "static","static_cast",
                                         "struct", "switch", "template", "this","throw",
                                         "true", "try", "typedef", "typeid", "typename",
                                         "union", "unsigned", "using", "virtual","void",
                                         "volatile", "wchar_t", "while"};
   
    int *intchar = NULL;  // just for test
   
    CountOfKeywords cks = CountOfKeywords("test01.cpp");  //将该文件存为 test01.cpp

    for(int i = 0; i<63; i++)
        {
            cks.addKeyword(strKw[i]);
        }
        
    cks.calCountOfKeyword();
   
    cks.showResult();

    system("pause");
    return 0;
}

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-07 19:52
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 
呵呵,等待着我的进一步测试巴。

http://kongfuziandlife. http://codeanddesign.
2005-09-08 18:10
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 
不过我16楼提的问题,有部分没解决阿???

http://kongfuziandlife. http://codeanddesign.
2005-09-08 18:11
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
什么没解决啊?

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-08 18:40
快速回复:流程图 流程图化简
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.125284 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved