130242014075+杨利城+第3次实验
一、实验目的
1.理解不同体系结构风格的具体内涵。
2.学习体系结构风格的具体实践。
二、实验环境
硬件: (依据具体情况填写)
软件:Java或任何一种自己熟悉的语言
三、实验内容
“上下文关键字”KWIC(Key Word in Context,文本中的关键字)检索系统接受有序的行集合:每一行是单词的有序集合;每一个单词又是字母的有序集合。通过重复地删除航中第一个单词,并把它插入行尾,每一行可以被“循环地移动”。KWIC检索系统以字母表的顺序输出一个所有行循环移动的列表。
尝试用不同的策略实现这个系统。选择2-3种体系结构风格来实现。
四、实验步骤:
(1)采用主/子程序的风格
1、体系结构图:
上述的主程序/子程序的方法,将问题分解为输入(Input)、移动(Shifter)、按字母表排序(Alphabetizer)、输出(Output)。
Input: 将读取到的每行的数据保存到实现LineStorage接口的数据结构中去
shifter:主函数调用该方法,该方法对characters中的每行的数据进行循环移位,并将移位得到的新行保存到实现LineStorage的数据结构中去
alphabetizer: 对circularShift中得到的行数据进行按字母顺序排序
Output:output方法迭代调用alphabetizer里面的方法得到按字母顺序排好序的行数据,并输出
Characters:实现字符的处理。读取一行就用Characters抽象数据类型将该行存放,直到文件读完为止
代码:
//输入 public static String[] input() { Scanner scanner = new Scanner(System.in); int len = scanner.nextInt(); String[] result = new String[len]; for (int i = 0; i < len; i++) { result[i] = scanner.next(); } return result; } //该方法对每行的数据进行循环移位 public static List<String[]> shifter(String[] a) { List<String[]> resultList = new ArrayList<String[]>(); String temp = null; for (int i = 0; i < a.length; i++) { resultList.add(a.clone()); temp = a[0]; for (int k = 0; k < a.length - 1; k++) { a[k] = a[k + 1]; if (k == a.length - 2) { a[a.length - 1] = temp; } } } return resultList; } public static List<String[]> join(List<String[]> a, List<String[]> b) { List<String[]> resultList = new ArrayList<String[]>(); for (int i = 0; i < a.size(); i++) { resultList.add(a.get(i)); } for (int i = 0; i < b.size(); i++) { resultList.add(b.get(i)); } return resultList; } //排序 public static List<String[]> removeRepetition(List<String[]> a) { for (int i = 0; i < a.size(); i++) { for (int j = i; j < a.size() - 1; j++) { if (a.get(i)[0].charAt(0) == a.get(j + 1)[0].charAt(0)) { a.remove(j + 1); } } } return a; } //根据首字母顺序进行排序 public static List<String[]> sort(List<String[]> a) { String[] temp = null; for (int i = 0; i < a.size(); i++) { for (int j = i; j < a.size() - 1; j++) { if (a.get(i)[0].charAt(0) > a.get(j + 1)[0].charAt(0)) { temp = a.get(i); a.set(i, a.get(j + 1)); a.set(j + 1, temp); } } } return a; } //输出 public static void ouput(String[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); }
结果:
(2)管道过滤器
体系结构图:
//按照字母表排序 public class Alphabetizer extends Filter { public Alphabetizer(Pipe input, Pipe output) { super(input, output); } public void transform() { List<String> lines = new ArrayList<String>(); CharArrayWriter writer = new CharArrayWriter(); try { int c = -1; while((c = input.read()) != -1) { writer.write(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(); } } public 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); } } public 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; } else break; } } }