Java 使用相互递归对两个文件进行二进制比较;希望使用一种方法,调用自身。怎么用?

Java 使用相互递归对两个文件进行二进制比较;希望使用一种方法,调用自身。怎么用?,java,recursion,Java,Recursion,我制作了一个纵横字谜(和类似的)益智工具,它可以在字典中搜索与模式匹配的内容,例如pa?ern,pa*,pa?ern*,等等,在哪里?和*与Windows中的通配符具有相同的含义(?匹配任意字母;*匹配任意数量的字母[包括0]) 我在网上找到了两本文字词典,一本有108000多个单词,另一本有80000多个单词。我假设较小的将是较大的一个子集。甚至不接近 因此,我编写了一个Java程序来比较和打印一本词典中的任何单词,但另一本词典中没有。(原来小的比大的有24000多个单词;大的有52000多个

我制作了一个纵横字谜(和类似的)益智工具,它可以在字典中搜索与模式匹配的内容,例如pa?ernpa*pa?ern*,等等,在哪里?和*与Windows中的通配符具有相同的含义(?匹配任意字母;*匹配任意数量的字母[包括0])

我在网上找到了两本文字词典,一本有108000多个单词,另一本有80000多个单词。我假设较小的将是较大的一个子集。甚至不接近

因此,我编写了一个Java程序来比较和打印一本词典中的任何单词,但另一本词典中没有。(原来小的比大的有24000多个单词;大的有52000多个,但小的没有。所以我现在通过另一个程序,拥有一本132000多个单词的字典。)

我使用了相互递归来实现这一点:

-When current words match, continue; nothing to print here. 
-When current word from dictionary A is "<" than current word in B, then...
     * print and read A until finding word ">=" current word in B.
       --If '=', they match; continue without printing.
       --If '>', then reverse the process: 
       * print and read B until finding word ">=" current word in A.
...

通过将两个
扫描器作为参数传递给方法,您得到了正确的开始,这样您就可以通过切换参数递归调用方法。然后你使用了一个不是参数之一的
扫描仪(
sca
)犯了错误。

多亏@ajb的快速响应和一个良好的夜晚(…好吧,几小时)睡眠,我得到了我想要的。修复并不像将
扫描仪
sca
更改为
a
那么简单,但我没有注意到的正是这个问题

我没有注意到这一点,因为编码太草率了。我有太多全局变量,尤其是
sca
scb
。如果他们是本地人,我昨晚就发现了这个问题。

下面是调用自身的方法。“--------------------”行是与ajb指出的逻辑错误相关的更改

  private static int read_untilGE_(Scanner a, String sa, long ma, 
                                   Scanner b, String sb, long mb){
    String indent;
    boolean shouldPrint;
    if (ma == mapa){indent = inda; shouldPrint = showa;}
    else           {indent = indb; shouldPrint = showb;}

    if(shouldPrint) System.out.println(indent + sa); // --------------------

    while(a.hasNext()){                              // --------------------

      sa = a.next();                                 // --------------------

      if(sa.equals(sb))        return 0;  // a = b
      if(sa.compareTo(sb) > 0) break;     // a > b
      if(shouldPrint) System.out.println(indent + sa); 
    } 
    read_untilGE_(b, sb, mb, a, sa, ma);
    return 0;
  }
以下是节目的其余部分:

package filecomparison;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

public class BinaryCompare {

  static  long     mapa, mapb;
  static  String   inda, indb;
  static  Boolean  showa = true,  showb = true;

  private static void drain(String msg, Scanner sc){
    int k = 0;
    System.out.println("\n\nDraining " + msg);
    String s;
      while(sc.hasNext()){
        s = sc.next();
        System.out.println(s);
        ++k;
      }
      System.out.println("\nExtras at end: " + k);
  }

  public static void main(String[] args) throws IOException {

    Scanner  sca,                  scb;
    String   reca,                 recb;

    Path a = Paths.get("C:\\newdic.txt");
    Path b = Paths.get("C:\\WordHelp.dick");

    sca = new Scanner(a);   scb = new Scanner(b);    
    mapa = a.hashCode(  );  mapb = b.hashCode();
    inda = "";              indb = "\t\t\t";

    System.out.println("Extra words in:\n" + inda + " " + a + "\n" + indb + b );

    while(sca.hasNext() && scb.hasNext()){

        reca = sca.next(); 
        recb = scb.next(); 

        int compareResult = reca.compareTo(recb);

        if(     compareResult < 0) read_untilGE_(sca, reca, mapa, scb, recb, mapb);  // a < b
        else if(compareResult > 0) read_untilGE_(scb, recb, mapb, sca, reca, mapa);  // a > b
                                                                     // recursion makes a = b at this point        
    }
    if(sca.hasNext())drain(a.toFile().toString(), sca);    
    if(scb.hasNext())drain(b.toFile().toString(), scb);
    sca.close();          scb.close();
  } // end main
}
包文件比较;
导入java.io.IOException;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.util.Scanner;
公共类二进制比较{
静态长mapa,mapb;
静态字符串inda,indb;
静态布尔值showa=true,showb=true;
专用静态空隙排放(字符串消息、扫描仪sc){
int k=0;
System.out.println(“\n\n打印”+msg);
字符串s;
while(sc.hasNext()){
s=sc.next();
系统输出打印项次;
++k;
}
System.out.println(“\nExtras在末尾:“+k”);
}
公共静态void main(字符串[]args)引发IOException{
扫描仪sca、scb;
字符串reca,recb;
路径a=路径.get(“C:\\newdic.txt”);
路径b=路径.get(“C:\\WordHelp.dick”);
sca=新扫描仪(a);scb=新扫描仪(b);
mapa=a.hashCode();mapb=b.hashCode();
inda=“”;indb=“\t\t\t”;
System.out.println(“输入的额外单词:\n”+inda+“”+a+“\n”+indb+b);
而(sca.hasNext()&&scb.hasNext()){
reca=sca.next();
recb=scb.next();
int compareResult=reca.compareTo(recb);
if(compareResult<0)read_untilGE_(sca、reca、mapa、scb、recb、mapb);//a0)read_untilGE(scb、recb、mapb、sca、reca、mapa);//a>b
//递归在这一点上使a=b
}
if(sca.hasNext())drain(a.toFile().toString(),sca);
if(scb.hasNext())排水管(b.toFile().toString(),scb);
sca.close();scb.close();
}//末端总管
}

“我非常喜欢递归,因为它是我们思考的方式”——你指的“我们”是谁?如果你认为这是整个人类的想法,甚至是所有程序员的子集,我认为你是完全错误的。对我来说,有些问题是自然递归的,有些则不是。我没想到西班牙宗教法庭!因此,我没有对这一说法给予足够的考虑。我只是高兴地发现,我已经知道了如何通过应用递归来获得结果,而且走得太远了。我真的想说“递归是我想象中的许多程序员解决这个特殊问题的方式”,而不仅仅是其他几个人。更具体地说,非Java程序员(即不使用FileVisitor接口)会发现递归使遍历Windows文件树相对简单。我想看到一个非递归的解决方案,或者我的问题。拿出舒适的椅子…我们的主要武器是惊讶…惊讶和恐惧…恐惧和惊讶。。。。我们的两个武器是恐惧和惊讶…和无情的效率。。。。我们的三大武器是恐惧、惊讶和无情的效率……以及对教皇近乎狂热的忠诚。。。。我们的四个…不。。。在我们的武器中。。。。在我们的武器中…有恐惧、惊讶等元素。。。。我会再来的……谢谢你的提示,@ajb。我希望我自己也看到过;很明显,你已经指出了!我实现了一个与您的观察结果一致的更改,并且没有出现错误,程序以EOF结束,但它只有大约90%的准确率,所以我还有更多的工作要做,但也许您的提示会让我得到我需要的——明天。我累死了。让我补充一句,我非常感谢@ajb仅仅给出一个提示,而不是像我要求的那样为我编写方法。
  private static int read_untilGE_(Scanner a, String sa, long ma, 
                                   Scanner b, String sb, long mb){
    String indent;
    boolean shouldPrint;
    if (ma == mapa){indent = inda; shouldPrint = showa;}
    else           {indent = indb; shouldPrint = showb;}

    if(shouldPrint) System.out.println(indent + sa); // --------------------

    while(a.hasNext()){                              // --------------------

      sa = a.next();                                 // --------------------

      if(sa.equals(sb))        return 0;  // a = b
      if(sa.compareTo(sb) > 0) break;     // a > b
      if(shouldPrint) System.out.println(indent + sa); 
    } 
    read_untilGE_(b, sb, mb, a, sa, ma);
    return 0;
  }
package filecomparison;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

public class BinaryCompare {

  static  long     mapa, mapb;
  static  String   inda, indb;
  static  Boolean  showa = true,  showb = true;

  private static void drain(String msg, Scanner sc){
    int k = 0;
    System.out.println("\n\nDraining " + msg);
    String s;
      while(sc.hasNext()){
        s = sc.next();
        System.out.println(s);
        ++k;
      }
      System.out.println("\nExtras at end: " + k);
  }

  public static void main(String[] args) throws IOException {

    Scanner  sca,                  scb;
    String   reca,                 recb;

    Path a = Paths.get("C:\\newdic.txt");
    Path b = Paths.get("C:\\WordHelp.dick");

    sca = new Scanner(a);   scb = new Scanner(b);    
    mapa = a.hashCode(  );  mapb = b.hashCode();
    inda = "";              indb = "\t\t\t";

    System.out.println("Extra words in:\n" + inda + " " + a + "\n" + indb + b );

    while(sca.hasNext() && scb.hasNext()){

        reca = sca.next(); 
        recb = scb.next(); 

        int compareResult = reca.compareTo(recb);

        if(     compareResult < 0) read_untilGE_(sca, reca, mapa, scb, recb, mapb);  // a < b
        else if(compareResult > 0) read_untilGE_(scb, recb, mapb, sca, reca, mapa);  // a > b
                                                                     // recursion makes a = b at this point        
    }
    if(sca.hasNext())drain(a.toFile().toString(), sca);    
    if(scb.hasNext())drain(b.toFile().toString(), scb);
    sca.close();          scb.close();
  } // end main
}