zoukankan      html  css  js  c++  java
  • 课堂测验-计算最长英语单词链

    设计思想:先将单词都从文本中找出来,然后再建立两个数组,第一个放每一个单词的首字母,第二个放每一个单词的尾字母,然后根据这两个是否相同,放进一个int数组中,调出来,就可以

    代码实现:

      1 package txt读入;
      2 
      3 import java.io.BufferedWriter;
      4 import java.io.File;
      5 import java.io.FileReader;
      6 import java.io.FileWriter;
      7 import java.io.IOException;
      8 
      9 public class Max {
     10 public static void main(String[] args)throws IOException {
     11     Word word=new Word();                                      //单词的链头
     12     Word lian,xin;                                             
     13     String str="";
     14     File f1=new File("E:\578095023\FileRecv\软件工程\课堂测试\飘英文版.txt");      
     15     if(!f1.exists())
     16     {
     17         System.out.println("文件不存在");
     18     }
     19     else
     20         {FileReader f=new FileReader("E:\578095023\FileRecv\软件工程\课堂测试\飘英文版.txt");                //读取英文文件
     21     
     22     
     23     char[] c=new char[1];                                 //每次读取一个字母
     24     int b=0;
     25     boolean exist=false;                              //判断单词是否存在于  word 链中
     26    
     27     BufferedWriter writer2 = new BufferedWriter(new FileWriter(new File("E:\\578095023\\FileRecv\\软件工程\\课堂测试\\output1.txt"),true));
     28 
     29     
     30     File writeName=new File("E:\\578095023\\FileRecv\\软件工程\\课堂测试\\output1.txt");
     31     String S1="";
     32     String S2="";
     33     writeName.createNewFile();
     34   
     35     int num1=0;
     36     int num2=0;
     37     int[] Num1=new int[1000000];
     38     int[] Num2=new int[1000000];
     39     
     40     
     41     
     42     while((b=f.read(c))!=-1)                              //每次读取一个字母直到最后
     43     {
     44         int i1=1;
     45         //如果字符为  换行、空格、单引号、双引号、逗号、句号  则为一个单词的结束及另一个单词的开始
     46         if(String.valueOf(c).equals("
    ")||String.valueOf(c).equals("
    ")||String.valueOf(c).equals(" ")||String.valueOf(c).equals(",")||String.valueOf(c).equals(".")||String.valueOf(c).equals(""")||String.valueOf(c).equals("'"))
     47         {
     48             lian=word;
     49             while(lian!=null)            
     50             {
     51                 if(lian.value.equalsIgnoreCase(str))           //如果单词在单词链中存在,则单词个数++
     52                 {
     53                     lian.geshu++;exist=true;break;
     54                 }
     55                 else
     56                 {
     57                     if(i1==1)
     58                     {
     59                         
     60                     
     61                     Num1[num1]=num1;
     62                     
     63                     String ss=str.substring(0, 1);
     64                     
     65                     S1=S1+ss;
     66                     num1=num1+1;
     67                     }
     68                     
     69                     if(i1==1)
     70                     {
     71                         Num2[num2]=num2;
     72                         String ss=str.substring(str.length()-1, str.length());
     73                         
     74                         S2=S2+ss;
     75                         num2=num2+1;
     76                     }
     77                     
     78                     
     79                     i1=i1+1;
     80                     lian=lian.next;
     81                 }
     82             }
     83             if(exist==false)                        //如果不存在,则在单词链中添加
     84             {
     85                 xin=new Word(str,1);
     86                 xin.next=word.next;
     87                 word.next=xin;
     88                 str="";
     89             }
     90             else
     91             {
     92                 exist=false;
     93                 str="";
     94             }
     95         }
     96         else                                      //单词
     97         {
     98             str+=String.valueOf(c);
     99         }
    100         
    101     }
    102     
    103     if(num1==1)
    104     {
    105         System.out.println("只有一个单词");
    106     }
    107     else {
    108     
    109         if(num1==0)
    110         {
    111             System.out.println("没有单词");
    112         }
    113         
    114         
    115         else {
    116         
    117         
    118     System.out.println(S1);
    119     System.out.println(S2);
    120     
    121     System.out.println(num1);
    122     for(int im=0;im<num1;im++)
    123     {
    124         String s1=S1.substring(im, im+1);
    125         int ii=1;
    126         for(int in=0;in<num1;in++)
    127         {
    128             
    129             String s2=S2.substring(in, in+1);
    130             
    131             int n1=Num1[im];
    132             int n2=Num2[im];
    133             
    134             if(!s1.equals(s2))
    135             {
    136                 System.out.println("没有首尾相连单词");
    137             }
    138                 
    139                 
    140             if(s1.equals(s2))
    141             {
    142                 
    143                 
    144                 
    145                 
    146                 int N=20;
    147                 for(int i=1;i<=10;i++)                   
    148                 {
    149                     xin=new Word("",0);
    150                     lian=word.next;
    151                     //找到单词链中个数最多的
    152                     while(lian!=null)
    153                     {
    154                         
    155                             xin=lian;
    156                         
    157                         lian=lian.next;
    158                     }
    159                     int m=n1-1;
    160                     if(i==n1-1)
    161                     {
    162                         
    163                          System.out.println("第"+i+"个 :"+xin.value+"个数:"+xin.geshu);
    164                     
    165                          try  {
    166                              
    167                              BufferedWriter writer = new BufferedWriter(new FileWriter(new File("E:\\578095023\\FileRecv\\软件工程\\课堂测试\\output1.txt"),true));
    168                              writer.write("
    "+xin.value);
    169                              writer.close();
    170 
    171                                 
    172                             }
    173                          catch (IOException e) {
    174                             e.printStackTrace();
    175                         }
    176             
    177                     
    178                     }
    179 //                    if(i==m)
    180 //                    {
    181 //                         System.out.println("第"+i+"个 :"+xin.value+"个数:"+xin.geshu);
    182 // try  {
    183 //                             
    184 //                             BufferedWriter writer = new BufferedWriter(new FileWriter(new File("E:\\578095023\\FileRecv\\软件工程\\课堂测试\\output1.txt"),true));
    185 //                             writer.write("
    "+xin.value);
    186 //                             writer.close();
    187 //
    188 //                                
    189 //                            }
    190 //                         catch (IOException e) {
    191 //                            e.printStackTrace();
    192 //                        }
    193 //                    
    194 //                    }
    195 //                    if(i==m)
    196 //                    {
    197 //                         System.out.println("第"+i+"个 :"+xin.value+"个数:"+xin.geshu);
    198 // try  {
    199 //                             
    200 //                             BufferedWriter writer = new BufferedWriter(new FileWriter(new File("E:\\578095023\\FileRecv\\软件工程\\课堂测试\\output1.txt"),true));
    201 //                             writer.write("
    "+xin.value);
    202 //                             writer.close();
    203 //
    204 //                                
    205 //                            }
    206 //                         catch (IOException e) {
    207 //                            e.printStackTrace();
    208 //                        }
    209 //                    
    210 //                    }
    211                     //输出单词链中个数最多的
    212                    
    213                     lian=word;
    214                     //删除单词链中单词个数最多的
    215                     while(lian.next!=null)
    216                     {
    217                         if(lian.next.value.equalsIgnoreCase(xin.value))
    218                         {
    219                             lian.next=lian.next.next;
    220                             break;
    221                         }
    222                         lian=lian.next;
    223                     }
    224                 }
    225                 
    226             }
    227         }
    228         
    229         ii=ii+1;
    230         
    231         
    232     }
    233     
    234     
    235     
    236 //    int N=20;
    237 //    for(int i=1;i<=N;i++)                   
    238 //    {
    239 //        xin=new Word("",0);
    240 //        lian=word.next;
    241 //        //找到单词链中个数最多的
    242 //        while(lian!=null)
    243 //        {
    244 //            if(lian.geshu>xin.geshu)
    245 //            {
    246 //                xin=lian;
    247 //            }
    248 //            lian=lian.next;
    249 //        }
    250 //        //输出单词链中个数最多的
    251 //        System.out.println("第"+i+"个 :"+xin.value+"个数:"+xin.geshu);
    252 //        lian=word;
    253 //        //删除单词链中单词个数最多的
    254 //        while(lian.next!=null)
    255 //        {
    256 //            if(lian.next.value.equalsIgnoreCase(xin.value))
    257 //            {
    258 //                lian.next=lian.next.next;
    259 //                break;
    260 //            }
    261 //            lian=lian.next;
    262 //        }
    263 //    }
    264         }
    265     }
    266     
    267         }
    268 }
    269 }

    代码实现:

  • 相关阅读:
    Android-内存泄漏
    性能adb命令
    设置Nginx开机自启动
    【转 记录】python中的encode以及decode
    SVN 提交回滚
    Linux基础
    CentOS6.X关闭防火墙
    SCP用法
    Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置(转)
    (转,记录用)jQuery页面加载初始化的3种方法
  • 原文地址:https://www.cnblogs.com/smartisn/p/10989038.html
Copyright © 2011-2022 走看看