130242014076+陈旭+第3次实验
一、實驗目的
1.理解不同體系結構風格的具體內涵。
2.學習體系結構風格的具體實踐。
二、實驗環(huán)境
硬件: (依據具體情況填寫)
軟件:Java或任何一種自己熟悉的語言
三、實驗內容
?
“上下文關鍵字”KWIC(Key Word in Context,文本中的關鍵字)檢索系統接受有序的行集合:每一行是單詞的有序集合;每一個單詞又是字母的有序集合。通過重復地刪除航中第一個單詞,并把它插入行尾,每一行可以被“循環(huán)地移動”。KWIC檢索系統以字母表的順序輸出一個所有行循環(huán)移動的列表。
嘗試用不同的策略實現這個系統。選擇2-3種體系結構風格來實現。
四、實驗步驟:
? ? ?要求寫具體實現代碼,并根據實際程序,畫出程序的總體體系結構圖和算法結構圖,以及運行結果截圖。
1、體系結構圖:
2、簡述體系結構各部件的主要功能,實現思想。
? ? 例如:
上述的主程序/子程序的方法,將問題分解為輸入(Input)、移動(Shifter)、按字母表排序(Alphabetizer)、輸出(Output)。
Input:?將讀取到的每行的數據保存到實現LineStorage接口的數據結構中去
shifter:主函數調用該方法,該方法對characters中的每行的數據進行循環(huán)移位,并將移位得到的新行保存到實現LineStorage的數據結構中去
alphabetizer: 對circularShift中得到的行數據進行按字母順序排序
Output:output方法迭代調用alphabetizer里面的方法得到按字母順序排好序的行數據,并輸出
Characters:實現字符的處理。讀取一行就用Characters抽象數據類型將該行存放,直到文件讀完為止
3、寫出主要的代碼
/*** */ package adtstyle;import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException;import adtstyle.impl.*; /*** 用抽象數據類型設計KWIC* @author cx* @version 2017年11月17日*/ public class ADTKwic {/*** @param args*/public static void main(String[] args) {File fInput = new File("input.txt");File fOutput = new File("output.txt");Input input = new Input();Characters characters = new CharactersImpl();input.input(fInput, characters);CircularShift circularShift = new CircularShiftImpl();circularShift.setup(characters);Alphabetizer alphabetizer = new AlphabetizerImpl();alphabetizer.alph(circularShift);Output output = new Output();output.output(fOutput, alphabetizer);//將結果直接輸出try {FileReader frInput = new FileReader(fInput);FileReader frOutput = new FileReader(fOutput);BufferedReader brInput = new BufferedReader(frInput);BufferedReader brOutput = new BufferedReader(frOutput);System.out.println("-----------------------Solution 3: ADT------------------------");System.out.println("---------------Input---------------");while(brInput.ready()) {System.out.println(brInput.readLine());}System.out.println("--------------Output---------------");while(brOutput.ready()) {System.out.println(brOutput.readLine());}brInput.close();brOutput.close();frInput.close();frOutput.close();} catch (IOException e) {e.printStackTrace();}}} /*** */ package adtstyle;import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException;/*** * @author cx**/ public class Input {private FileReader fr;private BufferedReader br;/*** 每讀取一行就用Characters抽象數據類型將該行存放,直到文件讀完為止* @param fInput 輸入文件* @param characters 存放讀到的每行的數據*/public void input(File fInput, Characters characters) {try {fr = new FileReader(fInput);br = new BufferedReader(fr);while(br.ready()) {characters.addLine(br.readLine());}br.close();fr.close();} catch(IOException e) {e.printStackTrace();}} } /*** */ package adtstyle.impl;import java.util.ArrayList; import java.util.List;import adtstyle.LineStorage;/*** @author cx**/ public class LineStorageImpl implements LineStorage{private List<String[]> lines = new ArrayList<String[]>();public void addLine(String[] line) {lines.add(line);}public void addWord(int lineIndex, String word) {String[] curLine = lines.get(lineIndex);String[] newLine = new String[curLine.length + 1];for(int i = 0; i < newLine.length - 1; i++) {newLine[i] = curLine[i];}newLine[newLine.length - 1] = word;lines.set(lineIndex, newLine);}public void deleteLine(int lineIndex) {lines.remove(lineIndex);}public void deleteWord(int lineIndex, int wordIndex) {String[] curLine = lines.get(lineIndex);String[] newLine = new String[curLine.length - 1];for(int i = 0; i < wordIndex; i++) {newLine[i] = curLine[i];}for(int j = wordIndex; j < newLine.length; j++) {newLine[j] = curLine[j + 1];}lines.set(lineIndex, newLine);}public void deleteWord(int lineIndex) {deleteWord(lineIndex, lines.get(lineIndex).length - 1);}public String[] getLine(int lineIndex) {return lines.get(lineIndex);}public String getLineAsString(int lineIndex) {String[] curLine = lines.get(lineIndex);String newLine = "";for(int i = 0; i < curLine.length; i++) {newLine += curLine[i];if(i != curLine.length - 1)newLine += " ";}return newLine;}public int getLineCount() {return lines.size();}public String getWord(int lineIndex, int wordIndex) {return lines.get(lineIndex)[wordIndex];}public int getWordCount(int lineIndex) {return lines.get(lineIndex).length;}public void insertLine(int lineIndex, String[] line) {lines.add(lineIndex, line);}public void insertWord(int lineIndex, int wordIndex, String word) {String[] curLine = lines.get(lineIndex);String[] newLine = new String[curLine.length + 1];for(int i = 0; i < wordIndex; i++) {newLine[i] = curLine[i];}newLine[wordIndex] = word;for(int j = wordIndex + 1; j < newLine.length; j++) {newLine[j] = curLine[j - 1];}lines.set(lineIndex, newLine);}public void setLine(int lineIndex, String[] line) {lines.set(lineIndex, line);}public void setWord(int lineIndex, int wordIndex, String word) {String[] curLine = lines.get(lineIndex);curLine[wordIndex] = word;lines.set(lineIndex, curLine);}public void addLine(String line) {String[] words = line.split(" +|\t+");List<String> list = new ArrayList<String>();for(int i = 0; i < words.length; i++) list.add(words[i]);addLine(list.toArray(new String[0]));}public List<String[]> getAllLines() {return lines;}public void setAllLines(List<String[]> lines) {this.lines.clear();this.lines = lines;}} /*** */ package adtstyle.impl;import java.util.Comparator; import java.util.List;import adtstyle.Alphabetizer; import adtstyle.CircularShift; import adtstyle.LineStorage;/*** @author cx**/ public class AlphabetizerImpl implements Alphabetizer {private LineStorage lineStorage = new LineStorageImpl();public AlphabetizerImpl() {}public AlphabetizerImpl(LineStorage lineStorage) {this.lineStorage = lineStorage;}public LineStorage getLineStorage() {return lineStorage;}public void setLineStorage(LineStorage lineStorage) {this.lineStorage = lineStorage;}public void addLine(String[] line) {lineStorage.addLine(line);}public void alph(CircularShift circularShift) {//快速排序List<String[]> lines = circularShift.getAllLines();qSort(lines, 0, lines.size() - 1, new Comparator<String[]>() {public int compare(String[] o1, String[] o2) {if(o1[0].compareToIgnoreCase(o2[0]) == 0)return 0;else if(o1[0].compareToIgnoreCase(o2[0]) > 0)return 1;elsereturn -1;}}); setAllLines(lines); }public String[] getLine(int lineIndex) {return lineStorage.getLine(lineIndex);}public int getLineCount() {return lineStorage.getLineCount();}public void setLine(int lineIndex, String[] line) {lineStorage.setLine(lineIndex, line);}public String getLineAsString(int lineIndex) {return lineStorage.getLineAsString(lineIndex);}public List<String[]> getAllLines() {return lineStorage.getAllLines();}public void setAllLines(List<String[]> lines) {lineStorage.setAllLines(lines);}//下面是快速排序算法public static <T> void qSort(List<T> a, int p, int r, Comparator<? super T> c){if(p < r){int q = partition(a, p, r, c);qSort(a, p, q - 1, c);qSort(a, q + 1, r, c);}}private static <T> int partition(List<T> a, int p, int r, Comparator<? super T> c) {int i = p;int j = r + 1;while (true) {while (c.compare(a.get(++i), a.get(p)) <= 0)if (i == r)break;while (c.compare(a.get(p), a.get(--j)) <= 0)if (j == p)break;if (i >= j)break;swap(a, i, j);}swap(a, j, p);return j;} private static <T> void swap(List<T> a, int i, int j){T temp = a.get(i);a.set(i, a.get(j));a.set(j, temp);}} /*** */ package adtstyle;import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException;/*** @author cx**/ public class Output {private FileWriter fw;private BufferedWriter bw;/*** output方法迭代調用alphabetizer里面的方法得到按字母順序排好序的行數據,并輸出* @param fOutput 輸出文件* @param alphabetizer 方便output方法調用alphabetizer里面的方法*/public void output(File fOutput, Alphabetizer alphabetizer) {try {fw = new FileWriter(fOutput);bw = new BufferedWriter(fw);for(int i = 0; i < alphabetizer.getLineCount(); i++) {String line = alphabetizer.getLineAsString(i);bw.write(line);bw.write("\r\n");}bw.close();fw.close();} catch (IOException e) {e.printStackTrace();}} } /*** */ package adtstyle.impl;import adtstyle.Characters; import adtstyle.LineStorage;/*** @author cx**/ public class CharactersImpl implements Characters {private LineStorage lineStorage = new LineStorageImpl();public CharactersImpl() {}public CharactersImpl(LineStorage lineStorage) {this.lineStorage = lineStorage;}public LineStorage getLineStorage() {return lineStorage;}public void setLineStorage(LineStorage lineStorage) {this.lineStorage = lineStorage;}public void addLine(String[] line) {lineStorage.addLine(line);}public void deleteLine(int lineIndex) {lineStorage.deleteLine(lineIndex);}public String[] getLine(int lineIndex) {return lineStorage.getLine(lineIndex);}public String getLineAsString(int lineIndex) {return lineStorage.getLineAsString(lineIndex);}public int getLineCount() {return lineStorage.getLineCount();}public void insertLine(int lineIndex, String[] line) {lineStorage.insertLine(lineIndex, line);}public void setLine(int lineIndex, String[] line) {lineStorage.setLine(lineIndex, line);}public void addLine(String line) {lineStorage.addLine(line);}}4、顯示結果:
二、管道/過濾器的風格
1、體系結構圖:
?
2、簡述體系結構各部件的主要功能,實現思想。
管道/過濾器模式是將組件分散并組合,頭尾相連,由四個組件輸入、循環(huán)移動、排序、輸出構成。首先由輸入發(fā)起而后組件的輸出是下一個組件的輸入,類似流水線的一種操作。
3、寫出主要的代碼
? ? ??
/*** */ package pipestyle;import java.io.BufferedReader; import java.io.File; import java.io.FileReader;/*** 用管道/過濾器風格設計KWIC* @author CX* @version 2017年11月17日*/ public class PipeKwic {public static boolean is_completed = false;/*** @param args*/public static void main(String[] args) {try {File fInput = new File(System.getProperty("user.dir") + "\\input.txt");File fOutput = new File("output.txt"); // System.out.println(System.getProperty("user.dir"));Pipe in_cs = new Pipe();Pipe cs_al = new Pipe();Pipe al_ou = new Pipe();Input input = new Input(fInput, in_cs);// input.transform();CircularShift shift = new CircularShift(in_cs, cs_al);// shift.transform();Alphabetizer alpha = new Alphabetizer(cs_al, al_ou); // alpha.transform();Output output = new Output(al_ou, fOutput); // output.transform();input.start(); // run it ! shift.start();alpha.start();output.start();// 將結果直接輸出FileReader frInput = new FileReader(fInput);FileReader frOutput = new FileReader(fOutput);BufferedReader brInput = new BufferedReader(frInput);BufferedReader brOutput = new BufferedReader(frOutput);System.out.println("-----------------------Solution 1: Pipe/Filter------------------------");System.out.println("---------------Input---------------");while(brInput.ready()) {System.out.println(brInput.readLine());}System.out.println("--------------Output---------------");try {Thread.sleep(5000);} catch (Exception e) {e.printStackTrace();}while(brOutput.ready()) {System.out.println(brOutput.readLine());}brInput.close();brOutput.close();frInput.close();frOutput.close();} catch (Exception e) {e.printStackTrace();}}} /*** */ package pipestyle;import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException;/*** 按行讀取數據,傳遞給下一模塊* @author cx* @version 2017年11月17日*/ public class Input extends Filter {private FileReader fr;public Input(File fInput, Pipe output) {super(null, output);try {fr = new FileReader(fInput);} catch (FileNotFoundException e) {e.printStackTrace();}}/*** 按行從輸入文件fr中讀取數據,并將數據寫到輸出管道output中*/@Overrideprotected void transform() {BufferedReader br = new BufferedReader(fr);try {String line;while((line = br.readLine()) != null) { // System.out.println("input"); // System.out.println(line);output.write(line + '\n'); //加入換行是以便下個模塊的處理 }br.close();fr.close();output.closeWriter();} catch (IOException e) {e.printStackTrace();}}} /*** */ package pipestyle;import java.io.CharArrayWriter; import java.io.IOException;/*** 第一行數據到來后開始運作* 把原始數據行循環(huán)移位,將原始行和新的移位后的行輸出給下一模塊* @author cx* @version 2017年11月17日*/ public class CircularShift extends Filter {private static final String ignore = "a#$an#$and#$as#$is#$the#$of#$"; //一些噪音詞匯public CircularShift(Pipe input, Pipe output) {super(input, output);}/*** */@Overrideprotected void transform() {try {CharArrayWriter writer= new CharArrayWriter(); //緩沖當前行int c = -1;while((c = input.read()) != -1) {if(c == 10) { //回車,表示writer中取得了一行數據String curLine = writer.toString();//存儲從輸入管道中取得的當前行String[] words = curLine.split(" +|\t+"); //將當前行分解成多個單詞for(int i = 0; i < words.length; i++) {if(ignore.indexOf((words[i] + "#$").toLowerCase()) != -1)//去掉噪音詞匯打頭的行continue;String shift = "";for(int j = i; j < (words.length + i); j++) {shift += words[j % words.length];if (j < (words.length + i - 1))shift += " ";}shift += "\r\n";output.write(shift);writer.reset();}} else writer.write(c);}input.closeReader();output.closeWriter();} catch (IOException e) {e.printStackTrace();}}} /*** */ package pipestyle;import java.io.CharArrayWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List;/*** @author cx**/ public class Alphabetizer extends Filter {public Alphabetizer(Pipe input, Pipe output) {super(input, output);}/*** */@Overrideprotected void transform() {List<String> lines = new ArrayList<String>();CharArrayWriter writer = new CharArrayWriter();try {int c = -1;while((c = input.read()) != -1) {writer.write(c);// System.out.print((char) c);if(c == 10) {String curLine = writer.toString();lines.add(curLine);writer.reset();}}sort(lines);for(String s : lines) {output.write(s);}input.closeReader();output.closeWriter();} catch (IOException e) {e.printStackTrace();}}private void sort(List<String> lines) { //堆排序int size = lines.size();for (int i = (size / 2 - 1); i >= 0; i--)siftDown(lines, i, size);for (int i = (size - 1); i >= 1; i--) {Object tmp = lines.get(0);lines.set(0, lines.get(i));lines.set(i, (String) tmp);siftDown(lines, 0, i);}}private void siftDown(List<String> lines, int root, int bottom) {int max_child = root * 2 + 1;while (max_child < bottom) {if ((max_child + 1) < bottom)if (((String) lines.get(max_child + 1)).compareTo((String) lines.get(max_child)) > 0)max_child++;if (((String) lines.get(root)).compareTo((String) lines.get(max_child)) < 0) {Object tmp = lines.get(root);lines.set(root, lines.get(max_child));lines.set(max_child, (String) tmp);root = max_child;max_child = root * 2 + 1;} elsebreak;}}} /*** */ package pipestyle;import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException;/*** @author cx**/ public class Output extends Filter {private FileWriter fw;/*** @param input 輸入管道* @param output 輸出文件*/public Output(Pipe input, File fOutput) {super(input, null);try {fw = new FileWriter(fOutput);} catch (IOException e) {e.printStackTrace();}}/*** */@Overrideprotected void transform() {BufferedWriter bw = new BufferedWriter(fw);try {int c = -1;while((c = input.read()) != -1) {bw.write(c);}input.closeReader();bw.flush();bw.close();fw.close();} catch (IOException e) {e.printStackTrace();}}}4、顯示結果
?
轉載于:https://www.cnblogs.com/softcx/p/7857385.html
總結
以上是生活随笔為你收集整理的130242014076+陈旭+第3次实验的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 让DB2数据库更牢靠
- 下一篇: Project Structure详解