130242014056 王鈺鵬 實驗三
阿新 • • 發佈:2017-11-19
nbsp urn () 讀取數據 實現 文本 wlan exceptio cep
軟件體系結構的第3次實驗(軟件體系結構風格之應用)
一、實驗目的
1.理解不同體系結構風格的具體內涵。
2.學習體系結構風格的具體實踐。
二、實驗環境
硬件: (依據具體情況填寫)
軟件:Java或任何一種自己熟悉的語言
三、實驗內容
“上下文關鍵字”KWIC(Key Word in Context,文本中的關鍵字)檢索系統接受有序的行集合:每一行是單詞的有序集合;每一個單詞又是字母的有序集合。通過重復地刪除航中第一個單詞,並把它插入行尾,每一行可以被“循環地移動”。KWIC檢索系統以字母表的順序輸出一個所有行循環移動的列表。
四、實驗步驟:
1、體系結構圖:
2、簡述體系結構各部件的主要功能,實現思想。
上述的主程序/子程序的方法,將問題分解為輸入(Input)、移動(Shifter)、按字母表排序(Alphabetizer)、輸出(Output)。
Input : 主函數調用該方法,讀取數據,輸出到控制臺。
Shifter : 主函數調用該方法,實現移位。
Alphabetizer : 主函數調用該方法,對行數據進行按字母順序排序
Output : 主函數調用該方法,輸出到控制臺
3、寫出主要的代碼
package com.newland.bomc.zy; /** * Created by hasee on 2017/11/19. */ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class KWIC { private static BufferedReader input_file; private ArrayList<String> kwicList; public static void main(String[] args) { new KWIC("E://kwic.txt"); } public KWIC (String filename) { kwicList = new ArrayList<String>(); String line=""; fileopen(filename); while (line!= null) { line= readline(); if (line !=null) { parseLine(line, kwicList); } } //Collections.sort(kwicList); display ( kwicList ); } public static void fileopen(String InputFilename) { try { input_file = new BufferedReader(new FileReader(InputFilename)); } catch (IOException e) { System.err.println(("File not open" + e.toString())); System.exit(1); } } //讀取文件 public static String readline() { String line =""; try { line = input_file.readLine(); } catch (Exception e) { e.getStackTrace(); } return line; } public void parseLine(String line,ArrayList<String> list) { StringTokenizer tokener = new StringTokenizer(line); String token = new String(); int index; ArrayList<String> tokens = new ArrayList<String>(); int count = tokener.countTokens(); for (int j = 0; j < count; j++) {//將一行解析,並且將解析的word加入ArrayList中 token = tokener.nextToken(); tokens.add(token); } //對ArrayList中的字進行循環移位,得出最後結果 for (int i = 0; i < count; i++) { index=i; StringBuffer linebuffer = new StringBuffer(); for (int j = 0; j < count; j++) { if (index >= count) index = 0; linebuffer.append ( tokens.get(index) ); linebuffer.append (" "); index++; } line = linebuffer.toString(); kwicList.add(line); } } public static void display(ArrayList<String> List) { System.out.println("----Output is----"); for (int count = 0; count < List.size(); count++) { System.out.println (List.get (count) ); } } }
130242014056 王鈺鵬 實驗三