1. 程式人生 > >130242014034-林偉領-第3次實驗

130242014034-林偉領-第3次實驗

目的 com 選擇 有序集合 .com true tput lis 分解

一、實驗目的

1.理解不同體系結構風格的具體內涵。

2.學習體系結構風格的具體實踐。

二、實驗環境

硬件: (依據具體情況填寫)

軟件:Java或任何一種自己熟悉的語言

三、實驗內容

“上下文關鍵字”KWIC(Key Word in Context,文本中的關鍵字)檢索系統接受有序的行集合:每一行是單詞的有序集合;每一個單詞又是字母的有序集合。通過重復地刪除航中第一個單詞,並把它插入行尾,每一行可以被“循環地移動”。KWIC檢索系統以字母表的順序輸出一個所有行循環移動的列表。

嘗試用不同的策略實現這個系統。選擇2-3種體系結構風格來實現。

四、實驗步驟:

采用主/子程序的風格

1、體系結構圖:

技術分享圖片

2、簡述體系結構各部件的主要功能,實現思想。

上述的主程序/子程序的方法,將問題分解為輸入(Input)、循環移動(Circular Shifter)、按字母表排序(Alphabetizer)、輸出(Output)。

Input: 將讀取到的每行的數據保存到inList

Circular Shifter:主函數調用該方法,該方法對characters中的每行的數據進行循環移位,並將移位得到的新行保存到outList

alphabetizer: 對circularShift中得到的行數據outList進行按字母順序排序

Output:output方法遍歷輸出經過alphabetizer方法處理後的outList

3、寫出主要的代碼

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import java.util.Scanner;
 4 
 5 /**
 6  * KWIC(Key Word in Context,文本中的關鍵字)
 7  * 采用主/子程序風格
 8  * @author L
 9  *
10  */
11 public class KWIC {
12 
13     private List<String[]> inList = new ArrayList<String[]>();
14     private
List<String[]> outList = new ArrayList<String[]>(); 15 public static void main(String[] args) { 16 KWIC test = new KWIC(); 17 test.input(); 18 test.CircularShifter(); 19 test.Alphabetizer(); 20 test.output(); 21 } 22 /** 23 * 輸入 24 */ 25 private void input() { 26 Scanner scanner = new Scanner(System.in); 27 System.out.println("========input========"); 28 while (true) { 29 String temp; 30 temp = scanner.nextLine(); 31 if (!temp.equals("!q")) { 32 inList.add(temp.split(" ")); 33 } 34 else{ 35 break; 36 } 37 } 38 } 39 /** 40 * 循環位移 41 */ 42 private void CircularShifter() { 43 ArrayList<String[]> tempList = new ArrayList<String[]>(); 44 tempList.addAll(inList); 45 String temp; 46 for (int i = 0; i < tempList.size(); i++) { 47 for (int j = 0;j < tempList.get(i).length; j++) { 48 outList.add(tempList.get(i).clone()); 49 temp = tempList.get(i)[0]; 50 for (int k = 0;k < tempList.get(i).length-1; k++) { 51 tempList.get(i)[k] = tempList.get(i)[k+1]; 52 if(k == tempList.get(i).length-2){ 53 54 tempList.get(i)[tempList.get(i).length-1]=temp; 55 } 56 } 57 } 58 } 59 } 60 /** 61 * 按字母表順序排序 62 */ 63 private void Alphabetizer(){ 64 String[] temp; 65 for(int i = 0;i<outList.size();i++){ 66 for(int j = i;j<outList.size()-1;j++){ 67 if(outList.get(i)[0].toLowerCase().charAt(0)>outList.get(j+1)[0].toLowerCase().charAt(0)){ 68 temp = outList.get(i); 69 outList.set(i, outList.get(j+1)); 70 outList.set(j+1, temp); 71 } 72 } 73 } 74 } 75 /** 76 * 輸出 77 */ 78 private void output(){ 79 System.out.println("========onput========"); 80 for(int i = 0;i<outList.size();i++){ 81 for (int j = 0;j<outList.get(i).length;j++){ 82 System.out.print(outList.get(i)[j]+" "); 83 } 84 System.out.println(""); 85 } 86 } 87 }

采用管道過濾器風格

1、體系結構圖:

技術分享圖片

2、簡述體系結構各部件的主要功能,實現思想。

上述的管道過濾器的方法,將問題分解為輸入(Input)、循環移動(Circular Shifter)、按字母表排序(Alphabetizer)、輸出(Output)。

Input: 將讀取到的每行的數據保存到inList,調用Circular Shifter方法

Circular Shifter:主函數調用該方法,該方法對characters中的每行的數據進行循環移位,並將移位得到的新行保存到outList,調用alphabetizer方法

alphabetizer: 對circularShift中得到的行數據outList進行按字母順序排序,調用Output方法

Output:output方法遍歷輸出經過alphabetizer方法處理後的outList

3、寫出主要的代碼

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import java.util.Scanner;
 4 
 5 /**
 6  * KWIC(Key Word in Context,文本中的關鍵字)
 7  * 采用管道過濾器風格
 8  * @author L
 9  *
10  */
11 public class KWIC2 {
12 
13     private List<String[]> inList = new ArrayList<String[]>();
14     private List<String[]> outList = new ArrayList<String[]>();
15     public static void main(String[] args) {
16         KWIC2 test = new KWIC2();
17         test.input();
18     }
19     private void input() {
20         Scanner scanner = new Scanner(System.in);
21         System.out.println("========input========");
22         while (true) {
23             String temp;
24             temp = scanner.nextLine();
25             if (!temp.equals("!q")) {
26                 inList.add(temp.split(" "));
27             }
28             else{
29                 break;
30             }
31         }
32         CircularShifter();
33     }
34 
35     private void CircularShifter() {
36         ArrayList<String[]> tempList = new ArrayList<String[]>();
37         tempList.addAll(inList);
38         String temp;
39         for (int i = 0; i < tempList.size(); i++) {
40             for (int j = 0;j < tempList.get(i).length; j++) {
41                 outList.add(tempList.get(i).clone());
42                 temp = tempList.get(i)[0];
43                 for (int k = 0;k < tempList.get(i).length-1; k++) {
44                     tempList.get(i)[k] = tempList.get(i)[k+1];
45                     if(k == tempList.get(i).length-2){
46                         
47                         tempList.get(i)[tempList.get(i).length-1]=temp;
48                     }
49                 }                
50             }
51         }
52         Alphabetizer();
53     }
54     private void Alphabetizer(){
55         String[] temp;
56         for(int i = 0;i<outList.size();i++){
57             for(int j = i;j<outList.size()-1;j++){
58                 if(outList.get(i)[0].toLowerCase().charAt(0)>outList.get(j+1)[0].toLowerCase().charAt(0)){
59                     temp = outList.get(i);
60                     outList.set(i, outList.get(j+1));
61                     outList.set(j+1, temp);
62                 }
63             }
64         }
65         output();
66     }
67     private void output(){
68         System.out.println("========onput========");
69         for(int i = 0;i<outList.size();i++){
70             for (int j = 0;j<outList.get(i).length;j++){
71             System.out.print(outList.get(i)[j]+" ");
72             }
73             System.out.println("");
74         }
75     }
76 }

顯示結果:

技術分享圖片

130242014034-林偉領-第3次實驗