diff --git a/group11/1178243325/DataStructure/build.gradle b/group11/1178243325/DataStructure/build.gradle index 5c3694d8ec..9c6bc859e6 100644 --- a/group11/1178243325/DataStructure/build.gradle +++ b/group11/1178243325/DataStructure/build.gradle @@ -1,10 +1,20 @@ + apply plugin: 'java' apply plugin: 'eclipse' jar { + from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }} manifest { - attributes 'Main-Class' : 'com.coding.Main' + attributes 'Main-Class' : 'com.Main' } } +repositories { + mavenCentral() +} + +dependencies { + compile 'junit:junit:4.12' + compile 'dom4j:dom4j:1.6.1' +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/Main.java b/group11/1178243325/DataStructure/src/main/java/com/Main.java new file mode 100644 index 0000000000..c5d56779c1 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/Main.java @@ -0,0 +1,53 @@ +package com; + +import com.coderising.litestruts.*; +import com.coderising.array.*; +public class Main { + public static void main(String[] args) { + int[] array = {1, 2, 3, 4, 5}; + System.out.print("reverseArray测试:"); + ArrayUtil.reverseArray(array); + for (int i : array) + System.out.print(i + " "); + System.out.print("\nremoveZero测试:"); + + int[] oldArray = {1, 3, 4, 5, 0, 0, 8 , 0, 9}; + oldArray = ArrayUtil.removeZero(oldArray); + for (int i : oldArray) { + System.out.print(i + " "); + } + + System.out.print("\nmerge测试:"); + int[] a1 = {3, 5,8}; + int[] a2 = {4, 5, 6,7}; + int[] arrays = ArrayUtil.merge(a1, a2); + for (int i : arrays) + System.out.print(i + " "); + + System.out.print("\ngrow测试:"); + + int[] growArray = ArrayUtil.grow(a1, 5); + for (int i : growArray) + System.out.print(i + " "); + + System.out.print("\nfibonacci测试"); + int[] fArray = ArrayUtil.fibonacci(1); + System.out.print(fArray); + System.out.println(); + fArray = ArrayUtil.fibonacci(15); + for (int i : fArray) + System.out.print(i + " "); + System.out.print("\ngetPrimes测试:"); + int[] primesArray = ArrayUtil.getPrimes(23); + for (int i : primesArray) + System.out.print(i + " "); + System.out.print("\ngetPerfectNumbers测试:"); + int[] pArray = ArrayUtil.getPerfectNumbers(100); + for (int i : pArray) + System.out.print(i + " "); + System.out.print("\njoin测试:"); + int[] jArray = new int[]{2, 3, 8}; + System.out.print(ArrayUtil.join(jArray, "-")); + Struts.test(); + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..f94d5d01c4 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java @@ -0,0 +1,229 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static void reverseArray(int[] origin){ + if (origin == null) { + return; + } + + int length = origin.length; + int[] temp = new int[length]; + for (int i = 0; i < length; i++) + temp[i] = origin[i]; + for (int i = length - 1, j = 0; i >= 0 && j < length; i--, j++) + origin[j] = temp[i]; + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray){ + if (oldArray == null) { + return null; + } + + int zeroCount = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) + zeroCount++; + } + int[] newArray = new int[oldArray.length-zeroCount]; + for (int i = 0, j = 0; i < oldArray.length && j < newArray.length; i++) { + if (oldArray[i] != 0) { + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + if (array1 == null && array2 == null) + return null; + int index1 = 0, index2 = 0; + int[] array3 = new int[array1.length + array2.length]; + int index = 0; + while (index1 != array1.length && index2 != array2.length) { + if (array1[index1] < array2[index2]) { + array3[index++] = array1[index1++]; + } else if (array1[index1] > array2[index2]) { + array3[index++] = array2[index2++]; + } else if (array1[index1] == array2[index2]){ + array3[index++] = array1[index1++]; + index2++; + } + } + + if (index1 == array1.length && index2 != array2.length) { + for (int i = index2; i < array2.length; i++) + array3[index++] = array2[i]; + } else if (index2 == array2.length && index1 != array1.length) { + for (int i = index1; i < array1.length; i++) { + array3[index++] = array1[i]; + } + } + + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) + newArray[i] = array3[i]; + return newArray; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + if (size <= 0) + return null; + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if (max < 1) + return null; + if (max == 1) + return null; + int[] array = new int[max]; + int i = 0; + int value = fibonaccis(i+1); + while ( value < max) { + array[i++] = value; + value = fibonaccis(i+1); + } + int[] newArray = new int[i]; + for (int j = 0; j < newArray.length; j++) { + newArray[j] = array[j]; + } + return newArray; + } + + private static int fibonaccis(int n) { + if (n <=0) + return 0; + if (n == 1 || n ==2 ) + return 1; + return fibonaccis(n-1)+fibonaccis(n-2); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + if (max <= 1) { + return null; + } + int[] array = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + if (i == 2 || i == 3 || i == 5 || i == 7) + array[index++] = i; + if (i%2 !=0 && i%3 != 0 && i%5 != 0 && i%7 != 0) + array[index++] = i; + } + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = array[i]; + } + + return newArray; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + if (max <= 0) + return null; + int[] array = new int[max]; + int index = 0; + for (int i = 1; i < max; i++) { + if (isPerfectNumber(i)) + array[index++] = i; + } + + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) + newArray[i] = array[i]; + + return newArray; + } + + private static boolean isPerfectNumber(int n) { + int sum = 0; + int i = 1; + while (i < n) { + if (n%i == 0) + sum += i; + i++; + } + if (sum == n) + return true; + return false; + } + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + if (array == null) + return null; + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (i == array.length-1) + str.append(array[i]); + else + str.append(array[i] + seperator); + } + return str.toString(); + } + + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp new file mode 100644 index 0000000000..5ff0f0622e Binary files /dev/null and b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp differ diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..a44153bf00 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml*/ + String targetClassName = XmlUtil.parseXML("struts.xml", actionName); + + /* + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java new file mode 100644 index 0000000000..46c673daf9 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java @@ -0,0 +1,33 @@ +package com.coderising.litestruts; + +import java.io.*; +import java.util.*; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.dom4j.io.XMLWriter; +public class XmlUtil { + + public static String parseXML(String filePath, String actionName) { + try { + File file = new File(filePath); + SAXReader reader = new SAXReader(); + Document doc = reader.read(file); + Element root = doc.getRootElement(); + for (Iterator iter = root.elementIterator("action"); iter.hasNext();) { + Element element = (Element)iter.next(); + Attribute nameAttr = element.attribute("name"); + if (nameAttr.getValue().equals(actionName)) { + Attribute classAttr = element.attribute("class"); + return classAttr.getValue(); + } + } + } catch (Exception e) { + e.printStackTrace(); + System.out.println("parse error"); + } + return null; + } + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..ae0ce37fd8 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java b/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java deleted file mode 100644 index 3260081ca1..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.coding; - -import com.coding.basic.*; -public class Main { - public static void main(String[] args) { - ArrayList list = new ArrayList(); - - list.add(0, "2xxx"); - list.add(1, "we"); - list.add(2, "sss"); - list.add("xing"); - list.remove(2); - System.out.println(list.get(2)); - Iterator iterator = list.iterator(); - while(iterator.hasNext()) { - System.out.println(iterator.next()); - } - System.out.println(list.size()); - - LinkedList llist = new LinkedList(); - llist.add("hu"); - llist.add("zhao"); - llist.add(2,"xing"); - llist.addFirst("身骑白马"); - llist.addLast("德州小老虎"); - llist.add(5, "sf"); - llist.remove(5); - llist.removeFirst(); - llist.removeLast(); - for (int i = 2; i >=0; i--) - System.out.print(llist.get(i)); - System.out.println(llist.size()); - - Iterator literator = llist.iterator(); - while(literator.hasNext()) { - System.out.println(literator.next()); - } - - Stack stack = new Stack(); - stack.push(1); - stack.push(2); - stack.push(3); - stack.push(4); - System.out.println(stack.peek()); - while(!stack.isEmpty()) - System.out.println(stack.pop()); - - Queue queue = new Queue(); - queue.enQueue(1); - queue.enQueue(2); - queue.enQueue(3); - System.out.println(queue.size()); - while (!queue.isEmpty()) { - System.out.println(queue.deQueue()); - } - - } -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java index 266eff3d56..1cf38aee30 100644 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -9,24 +9,25 @@ public class BinaryTreeNode { public Object getData() { return data; } + public void setData(Object data) { this.data = data; } + public BinaryTreeNode getLeft() { return left; } + public void setLeft(BinaryTreeNode left) { this.left = left; } + public BinaryTreeNode getRight() { return right; } + public void setRight(BinaryTreeNode right) { this.right = right; } - public BinaryTreeNode insert(Object o){ - return null; - } - } diff --git a/group11/1178243325/DataStructure/struts.xml b/group11/1178243325/DataStructure/struts.xml new file mode 100644 index 0000000000..ae0ce37fd8 --- /dev/null +++ b/group11/1178243325/DataStructure/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + +