《構建之法》練習題
大家經常玩成語接龍游戲,我們試一試英語的接龍吧:一個文本文件中有N 個不同的英語單詞, 我們能否寫一個程序,快速找出最長的能首尾相連的英語單詞鏈,每個單詞最多只能用一次。最長的定義是:最多單詞數量,和單詞中字母的數量無關。
例如, 文件里有:
Apple
Zoo
Elephant
Under
Fox
Dog
Moon
Leaf
Tree
Pseudopseudohypoparathyroidism
最長的相連英語單詞串為: apple - elephant – tree, 輸出到文件里面,是這樣的:
Apple
Elephant
Tree
追加:判斷文件不存在,文件無單詞,文件一個單詞,文件中無關聯的(文件一個單詞是其特殊情況)
在文本input.txt中取出單詞,在outout.txt顯示最長的相連英語單詞串
任務分解:先行判斷文件是否存在
不存在->顯示錯誤
存在->取出單詞存於內存中->判斷單詞數是否為一個
一個->顯示錯誤
不是一個->判斷相連英語單詞(持續)(有一個判斷是否重復的代碼判斷)->導出最長的相連英語單詞串
我的所有錯誤在控制台的輸出都注釋掉了,改成在文本里顯示
代碼:
package 六月六號;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test1 {
public static void main(String[] args) throws IOException
{
// TODO 自動生成的方法存根
String filename ="D://dada.txt";
File a=new File(filename);
File file=new File("D://output.txt");
long startTime = System.currentTimeMillis();
//judeFileExists(a);
if(judeFileExists(a))
{
danci(filename);
}
else
{
try {
FileWriter fw =new FileWriter(file);
fw.write("無此文件");
fw.flush();
fw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis(); //獲取結束時間
System.out.println("程序運行時間:" + (endTime - startTime) + "ms"); //輸出程序運行時間
}
public static void danci(String s) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(s));
StringBuffer sb = new StringBuffer();
String text = null;
while ((text = br.readLine()) != null) {
sb.append(text);// 將讀取出的字符追加到stringbuffer中
}
br.close(); // 關閉讀入流
String str = sb.toString().toLowerCase(); // 將stringBuffer轉為字符並轉換為小寫
String[] words = str.split("[^(a-zA-Z)]+"); // 非單詞的字符來分割,得到所有單詞
StringBuffer yao = new StringBuffer();
String b1=words[0];
yao.append(b1);
yao.append(" ");
//System.out.println(b1);
if(b1.equals(""))
{
System.out.println("文件為空");
yao.append("文件為空");
}
else
{
if(words.length==1)
{
System.out.println("只有一個單詞");
}
String end=b1.substring(b1.length()-1,b1.length());
// System.out.println(end);
for(int i=1;i<words.length;i++)
{
String start=words[i].substring(0,1);
if(end.equals(start))
{
if(judechong(yao,words[i]))
{}
else
{
end=words[i].substring(words[i].length()-1,words[i].length());
yao.append(words[i]);
yao.append(" ");
}
}
}
if(yao.toString().equals(words[0]+" "))
{
yao.append("無互聯語句");
System.out.println("無互聯詞");
}
}
// for( String a:words)
// {
// System.out.println(a);
// }
// System.out.println(yao.toString());
File file =new File("D://output.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileWriter fw =new FileWriter(file);
fw.write(yao.toString());
fw.flush();
fw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static boolean judechong(StringBuffer yao,String word)
{
String a=yao.toString();
boolean flag=false;
String[] words = a.split("[^(a-zA-Z)]+"); // 非單詞的字符來分割,得到所有單詞
for(int i=0;i<words.length;i++)
{
if(word.equals(words[i]))
{
flag=true;
}
}
return flag;
}
// 判斷文件是否存在
public static boolean judeFileExists(File file) {
if (file.exists()) {
System.out.println("文件存在");
return true;
} else {
System.out.println("文件不存在");
// try {
// file.createNewFile();
// } catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// }
return false;
}
}
}
優化分析:
如果文件過大怎樣解決:
打開文件后,改為一個單詞一個單詞的讀取的方式取代全部讀出來的方式。這時需要判斷單詞間隔和單詞首字母的大小寫。倆個函數或者二者寫在一起。
你的最長的相連英語單詞串可能會變得很長,此時也選擇找出一個寫進去一個的方式。
我暫時只能想到這個問題的解決和分析,還有什么問題,希望大家提出來一起解決。
