我现在做的程序,是input一个filename参数。然后要看这个file有多少子文件,因为是php file,看每一行包括require .... ("...php")这个""里的就是子文件。
现在做到这步,但是问题是子文件下面还会有子文件,就不知道怎么处理了。高手帮忙给点思路啊?
另外我还要算这个文件的depth,比如input的是a 文件,有b,c2个子文件,同时b又有d子文件,那么a的depth就是2,因为最远的一个路径是a->b->d,是2层。
各位高手,特别是java_2000,火龙果帮帮我啦,先谢谢各位了!

解决方案 »

  1.   

    现在试了下,用递归那找到每个子文件了,但是算depth还有点困难,继续求助!!
      

  2.   

    你递归的时候,传一个depth参数就行了。每次都增加1,比如伪代码function(int depth){
      ...
       function(depth+1);
      ...
    }调用的时候
    function(1);第一层就是1,每次递归都会增加1个
      

  3.   

    好像我说的不是很清楚,因为我这个程序对php分析的时候,原始的input file的每一行都可能有一次递归,只要match了RE。
    我现在就是要找递归次数最多的那一次。
      

  4.   

    int maxDepth=1;void search(String file, int depth){
      if(depth>maxDepth){
        maxDepth = depth;
      }
      // 下面开始解析
      String[] includeFiles = parseFile(file);
      for(String includeFile : includeFiles){
        search(includefile,depth+1);
      }
    }
      

  5.   

    我的问题是一个tree一样的结构。
    老紫竹,你的答案好像是算tree一共有多少个node,但是我想要知道的是这个tree最深有几层。好像有点不一样啊。
      

  6.   

    这个tree一共有多少node我现在能算出来,但是不知道用什么算法可以算出最深的是几层。哎,好像越说越说不清了。
      

  7.   

    你看错了!你仔细看看,那里是 depth+1
    不是
    depth = depth+1;
    search(file,depth);区别很大!
      

  8.   

    不好意思,能麻烦你看看我的代码么?我是这样写的。好像和你的有点类似。public List findChildren(String filename) {
         //int dep = 0;
            List list = readFile(filename);
            List requirelist = new ArrayList();
            File f = new File("");
         String str1 = f.getAbsolutePath();
         for(int i=0; i<list.size(); i++) {
                String   str   =   list.get(i).toString();
                Pattern p = Pattern.compile("(require|include).*[\"](.*.php)[\"]");
                Matcher m = p.matcher(str);
                while(m.find()){
                 String fn = str1 + m.group(2);
                 requirelist.add(fn);
                }  
            }    
            for(int i=0; i<requirelist.size(); i++)
         {
         findChildren(requirelist.get(i).toString());
         }
            return requirelist;
        }我的requirelist是一个list,包括所有的子文件。这个方法能找到depth么?我还是有点不理解你的方法啊。我先再去试试,如果能做出来最好了。
    先谢了!