zoukankan      html  css  js  c++  java
  • 关于计算最长英语单词链的问题

    要求:

    例如, 文件里有:

    Apple

    Zoo

    Elephant

    Under

    Fox

    Dog

    Moon

    Leaf

    Tree

    最长的相连英语单词串为: apple - elephant – tree,

    输出到文件里面,是这样的:

    Apple Elephant Tree

    package test_use_p;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    
    public class Action {
        public static void main(String[] args) throws IOException {
            String[] st = new String[20];
            String[] sf = new String[20];
            String[] ss = new String[20];
            
            String url = "input.txt";
            
            st = readWordT(url);
            sf = readWordF(url);
            ss = readWord(url);
            
            //BufferedWriter out = new BufferedWriter(new FileWriter("output.txt"));
            FileWriter writer = new FileWriter("output.txt");
            BufferedWriter out = new BufferedWriter(writer);
    
            int t = 0;
            int f = 0;
            int temp = 0;
            try {
                for(f = temp;f<20;f++) {
                    for(t = temp;t<20;t++) {
                        if( sf[f].equals(st[t]) ) {
                            System.out.println(ss[++f]);                        
                            out.write(ss[++f]);
                        }
                    }
                }
            }
            catch (Exception e){
                
            }
            
        }
        
        
        
        
        public static void read(int temp,String[] st,String[] sf,String[] ss) {
            int t = 0;
            int f = 0;
            
            for(f = temp;f<20;f++) {
                //System.out.println("fffffffffffffffff"+f);
                for(t = temp;t<20;t++) {
                    //System.out.println("ttttttttttttttttttttt"+t);
                    if( sf[f].equals(st[t]) ) {
                        System.out.println(ss[++f]);
                        temp = t;
                        
                    }
                }
            }
        }
        
        
        @SuppressWarnings("resource")
        public static String[] readWordT(String url) throws IOException {
            String[] s = new String[20];
            int i = 0;
            
            
            BufferedReader br = new BufferedReader(new FileReader(url));
            BufferedReader brspace = new BufferedReader(new FileReader(url));
            int tempc, tempspace;//记录是否为字母或空格
            char c1, c2;//分别记录第一个单词的末尾,第二个单词的开头
            String s1, s2;
            
            
            brspace.read();//多读到下一个
            while( (tempc = br.read()) != -1) { 
                tempspace = brspace.read();
                
                if( (tempc >= 97 && tempc <= 122) && tempspace == 32) {//尾字母
                    c1 = (char)tempc;
                    s1 = String.valueOf(c1);
                    //System.out.println(c1);
                }
                
                if(tempc == 32 && (tempspace >= 97 && tempspace <= 122) ) {//头字母
                    c2 = (char)tempspace;
                    s2 = String.valueOf(c2);
                    //System.out.println(c2);
                    
                    s[i] = s2;//记录到头字符串数组中
                    i++;
                }
                
            }
            
            
            return s;        
        }
        
        
        @SuppressWarnings("resource")
        public static String[] readWordF(String url) throws IOException {
            String[] s = new String[20];
            int i = 0;
            
            
            BufferedReader br = new BufferedReader(new FileReader(url));
            BufferedReader brspace = new BufferedReader(new FileReader(url));
            int tempc, tempspace;//记录是否为字母或空格
            char c1, c2;//分别记录第一个单词的末尾,第二个单词的开头
            String s1, s2;
            
            
            brspace.read();//多读到下一个
            while( (tempc = br.read()) != -1) { 
                tempspace = brspace.read();
                
                
                if( (tempc >= 97 && tempc <= 122) && tempspace == 32) {//尾字母
                    c1 = (char)tempc;
                    s1 = String.valueOf(c1);
                    //System.out.println(c1);
                    
                    
                    s[i] = s1;//记录到末尾字符串数组中
                    i++;
                }
                
                if(tempc == 32 && (tempspace >= 97 && tempspace <= 122) ) {//头字母
                    c2 = (char)tempspace;
                    s2 = String.valueOf(c2);
                    //System.out.println(c2);             
                }
                
            }
            
            
            return s;        
        }
        
        
        @SuppressWarnings("resource")
        public static String[] readWord(String url) throws IOException {
            String[] s = new String[20];
            int i = 0;    
            
            BufferedReader br = new BufferedReader(new FileReader(url));
            
            int tempc;//记录是否为字母或空格
            char c1;//分别记录第一个单词的末尾,第二个单词的开头
            String s1;
            String ss = "";
            
            while( (tempc = br.read()) != -1) {   
                if((tempc >= 97 && tempc <= 122)) {
                    c1 = (char)tempc;
                    s1 = String.valueOf(c1);                              
                    ss = ss.concat(s1);                
                } 
                else if(tempc == 32) {
                    
                    s[i] = ss;
                    i++;
                    //System.out.println(ss);
                    ss = "";
                }
            }    
            return s;        
        }
        
        
    }
  • 相关阅读:
    c++中多态性、dynamic_cast、父类指针、父类对象、子类指针、子类对象
    Makefile写法
    verilog 随笔
    VHDL设计时参数定义的方法 例子
    Linux ln命令
    linux下使用tar命令
    Linux find命令详解
    图解红外遥控的发射和接收原理
    实战 SSH 端口转发
    数字签名是什么?
  • 原文地址:https://www.cnblogs.com/leity/p/11008611.html
Copyright © 2011-2022 走看看