• java课程课后作业190606之计算最长英语单词链


    一个文本文件中有N 个不同的英语单词, 我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最多只能用一次。最长的定义是:最多单词数量,和单词中字母的数量无关。

    统一输入文件名称:input1.txt, input2.txt

    统一输出文件名称:output1.txt,output2.txt

    程序需要考虑下列异常状况:

    例如,文件不存在,你的程序会崩溃么,还是能优雅地退出并给用户提示信息?

    如果文件没有任何单词、只有一个单词、没有可以首尾相连的单词,程序应该如何输出?

     

    这里虽然我实现了这个功能,但是算法的复杂度过高,并不是理想的算法,所以就不在阐述,只给出下面的异常情况处理

    public class test {
        private static String WORDSUM = "";
        private static int num=0;
        static String[] TempSUM=null;
        public static void main(String[] args) throws Exception {
            
    
            // TODO Auto-generated method stub
            
            String[] temp={"apple","ant","ban","element","televetion","tv","ppt","nation"};
            
            File file = new File("C:/D/inmmmput.txt");
            boolean FileExists= judeFileExists(file);
            if(FileExists==false)
            {
                System.out.println("文件不存在............");
            }else {
                System.out.println("文件存在............");
                if(judeFileSize(file)==false)
                {
                    System.out.println("文件没有内容...........");
                }
                else {
                    ReadBook();
                    String Maxnumber=run(TempSUM);
                    WriteStringToFile(Maxnumber);
                }
    
            }
            
    
        }
        private static String run(String temp[]) {
            String MaxNumber=null;
            int Max=0;
            int get=0;
            if(temp.length>1)
            {
                for(int j=0;j<temp.length;j++)
                {
                    System.out.println("开始的字母"+temp[j]);
                    if(!temp[j].isEmpty())
                    {
                        String number=temp[j];
                        
                        String numberLast=temp[j].substring(temp[j].length()-1, temp[j].length());
                        for(int i=j+1;i<temp.length;i++)
                        {
                            
                            if(!temp[i].isEmpty())
                            {
                                System.out.println("读取的单词"+temp[i]);
                                System.out.println("读取的单词开头字母"+temp[i].substring(0, 1));
                                System.out.println("上一个的单词结尾字母"+numberLast);
                                if(temp[i].substring(0, 1).equals(numberLast))
                                {
                                    number=number+"--"+temp[i];
                                    System.out.println("匹配成功连接"+number);
                                }
                                numberLast=number.substring(number.length()-1, number.length());
                                get++;
                            }
                            System.out.println(number);
                            if(Max<=get)
                            {
                                MaxNumber=number;
                                Max=get;
                                System.out.println("目前值"+get);
                        }
                            get=0;
    }
                    }
    
                    System.out.println("最大值"+Max);
                }
                if(Max==1)
                {
                    System.out.println("没有首尾相连啊");
                }
                return MaxNumber;
            }
            else {
                System.out.println("只有一个哦。。");
                return "null";
            }
    
        }
        // 判断文件是否存在
        public static boolean judeFileExists(File file) throws IOException {
              if (file.exists()) {
                
                return true;
                } else {
                    
                    return false;
                }
        }
        //判断文件的大小
        private static boolean judeFileSize(File file) {
            if(file.length()<=0)
            {
                return false;
            }
            else {
                return true;
            }
            
        }
        public static void WriteStringToFile(String number) throws IOException {
            try {
                String filePath="C:/D/output.txt";
                File file = new File(filePath);
                if(judeFileExists(file))
                {
                    PrintStream ps = new PrintStream(new FileOutputStream(file));
                    ps.println(number);// 往文件里写入字符串
                    ps.close();
                    System.out.println("已经写入文件");
                }
                else {
                    System.out.println("输出文件不存在");
                }
    
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           
        }
        public static void ReadBook()throws Exception
        {
            String book=book("C:/D/input.txt");
            System.out.println(book);
        }
    
        private static String book(String file) throws IOException
        {
            // TODO Auto-generated method stub
            File f=new File(file);
            BufferedReader bf=new BufferedReader(new FileReader(f));
            String content="";
            int lenth=0;
            int x=0;
            StringBuilder sb=new StringBuilder();
            String[] sum= {""};
            while(content!=null)
            {
                WORDSUM=WORDSUM+content;
                content= bf.readLine();
                if(content==null)
                    break;
              
            }
            
            WORDSUM=WORDSUM.replace(".",",");
            WORDSUM=WORDSUM.replace(";",",");
            WORDSUM=WORDSUM.replace("!",",");
            WORDSUM=WORDSUM.replace("?",",");
            WORDSUM=WORDSUM.replace("-",",");
            WORDSUM=WORDSUM.replace("“",",");
            WORDSUM=WORDSUM.replace("”",",");
            WORDSUM=WORDSUM.replace("‘",",");
            WORDSUM=WORDSUM.replace("’",",");
            WORDSUM=WORDSUM.replace("(",",");
            WORDSUM=WORDSUM.replace(")",",");
            WORDSUM=WORDSUM.replace(":",",");
            WORDSUM=WORDSUM.replace("—",",");
            WORDSUM=WORDSUM.replace(" ",",");
            WORDSUM=WORDSUM.replace(" ",",");
            TempSUM = WORDSUM.split(",");
            
            return sb.toString();
        }
    
    }
  • 相关阅读:
    day63_django_html
    day62_django
    day20
    diango_自定义标签问题
    day64_django_orm
    day16_函数嵌套及对象
    day60_django
    pip 安装问题
    day13_文件操作
    文本溢出显示省略号(…) 小坦克
  • 原文地址:https://www.cnblogs.com/heiyang/p/11005940.html
Copyright © 2020-2023  润新知