diff --git a/group23/601689050/.gitattributes b/group23/601689050/.gitattributes new file mode 100644 index 0000000000..bdb0cabc87 --- /dev/null +++ b/group23/601689050/.gitattributes @@ -0,0 +1,17 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/group23/601689050/4LRU&JVM/JVM/ClassFileLoader.java b/group23/601689050/4LRU&JVM/JVM/ClassFileLoader.java deleted file mode 100644 index 6a7ab61bc0..0000000000 --- a/group23/601689050/4LRU&JVM/JVM/ClassFileLoader.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.loader; - -import java.io.*; -import java.util.ArrayList; -import java.util.Dictionary; -import java.util.List; - - - -public class ClassFileLoader { - - private List clzPaths = new ArrayList<>(); - - public byte[] readBinaryCode(String className) throws ClassNotFoundException { - - String fileName = className.replace('.',File.separatorChar)+".class"; - InputStream is = null; - try{ - is = new FileInputStream(fileName); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - int length = 0; - while((length = is.read(buffer)) != -1){ - baos.write(buffer,0,length); - } - return baos.toByteArray(); - } catch (IOException e){ - e.printStackTrace(); - } finally { - if(is!=null){ - try{ - is.close(); - } catch (IOException e){ - e.printStackTrace(); - } - } - } - return null; - } - - - - - public void addClassPath(String path) { - - StringBuilder str = new StringBuilder(path); - - } - - - - public String getClassPath(){ - - String path = Thread.currentThread().getContextClassLoader().getResource("/").getPath(); - - return path; - } - - - - - -} diff --git a/group23/601689050/4LRU&JVM/JVM/ClassFileloaderTest.java b/group23/601689050/4LRU&JVM/JVM/ClassFileloaderTest.java deleted file mode 100644 index eae77d6dd4..0000000000 --- a/group23/601689050/4LRU&JVM/JVM/ClassFileloaderTest.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.test; - -import com.loader.ClassFileLoader; -import org.junit.Assert; -import org.junit.Test; -import org.junit.Before; -import org.junit.After; - - -public class ClassFileloaderTest { - - - public static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; - public static String path2 = "C:\\temp"; - - - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testClassPath(){ - - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - loader.addClassPath(path2); - - String clzPath = loader.getClassPath(); - - Assert.assertEquals(path1+";"+path2,clzPath); - - } - - @Test - public void testClassFileLength() throws ClassNotFoundException { - - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - - String className = "EmployeeV1"; - - byte[] byteCodes = loader.readBinaryCode(className); - - // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 - Assert.assertEquals(1056, byteCodes.length-1); - - } - - - @Test - public void testMagicNumber() throws ClassNotFoundException { - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - String className = "EmployeeV1"; - byte[] byteCodes = loader.readBinaryCode(className); - byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; - - - String acctualValue = this.byteToHexString(codes); - - Assert.assertEquals("cafebabe", acctualValue); - } - - - - - - - private String byteToHexString(byte[] codes ){ - StringBuffer buffer = new StringBuffer(); - for(int i=0;i clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + className = className.replace('.', File.separatorChar) +".class"; + + for(String path : this.clzPaths){ + + String clzFileName = path + File.separatorChar + className; + byte[] codes = loadClassFile(clzFileName); + if(codes != null){ + return codes; + } + } + + return null; + + + + } + + private byte[] loadClassFile(String clzFileName) { + + File f = new File(clzFileName); + + try { + + return IOUtils.toByteArray(new FileInputStream(f)); + + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + + + public void addClassPath(String path) { + if(this.clzPaths.contains(path)){ + return; + } + + this.clzPaths.add(path); + + } + + + + public String getClassPath(){ + return StringUtils.join(this.clzPaths,";"); + } + + +} diff --git a/group23/601689050/4weekLRU&JVM/JVM/test/loader/ClassFileLoaderTest.java b/group23/601689050/4weekLRU&JVM/JVM/test/loader/ClassFileLoaderTest.java new file mode 100644 index 0000000000..8e4e172f25 --- /dev/null +++ b/group23/601689050/4weekLRU&JVM/JVM/test/loader/ClassFileLoaderTest.java @@ -0,0 +1,86 @@ +package test.loader; + +import loader.ClassFileLoader; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ClassFileLoaderTest { + static String path1 = "D:\\JavaProject\\4week_minijvm\\out\\production\\4week_minijvm"; + static String path2 = "D:\\temp"; + + + + @Before +public void before() throws Exception { +} + +@After +public void after() throws Exception { +} + +/** +* +* Method: readBinaryCode(String className) +* +*/ +public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + +} + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "test.loader.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "test.loader.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i tokens = parser.parse(this.expr); + Stack opStack = new Stack<>(); + Stack numStack = new Stack(); + for(Token token : tokens){ + if(token.isOpretor()){ + if(opStack.isEmpty()){ + opStack.push(token); + }else { + while(!opStack.isEmpty() && !token.hasHigherPriority(opStack.peek())){ + Token prevOperator = opStack.pop(); + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + Float result = calculate(prevOperator.toString(),f1,f2); + numStack.push(result); + } + opStack.push(token); + } + } + if(token.isNumber()){ + numStack.push(new Float(token.getIntValue())); + } + } + while (!opStack.isEmpty()){ + Token token = opStack.pop(); + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + numStack.push(calculate(token.toString(),f1,f2)); + } + return numStack.pop().floatValue(); + + } + + private Float calculate(String op, float f1, float f2) { + if(op.equals("+")){ + return f1 + f2; + } + if(op.equals("-")){ + return f1 - f2; + } + if(op.equals("*")){ + return f1 * f2; + } + if(op.equals("/")){ + return f1 / f2; + } + return null; + } +} diff --git a/group23/601689050/6weekInfixExpr/InfixExprTest.java b/group23/601689050/6weekInfixExpr/InfixExprTest.java new file mode 100644 index 0000000000..bf2f8e91a9 --- /dev/null +++ b/group23/601689050/6weekInfixExpr/InfixExprTest.java @@ -0,0 +1,50 @@ +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by Lxx on 2017/4/28. + */ +public class InfixExprTest { + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); + { + InfixExpr expr = new InfixExpr("2+3*4+5"); + Assert.assertEquals(19.0, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); + Assert.assertEquals(100.0, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("3*20/2"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("20/2*3"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("10-30+50"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("10-2*3+50"); + Assert.assertEquals(54, expr.evaluate(), 0.001f); + } + + } +} diff --git a/group23/601689050/6weekInfixExpr/Stack.java b/group23/601689050/6weekInfixExpr/Stack.java new file mode 100644 index 0000000000..8a15be4366 --- /dev/null +++ b/group23/601689050/6weekInfixExpr/Stack.java @@ -0,0 +1,50 @@ +import java.util.ArrayList; +import java.util.EmptyStackException; + +/** + * Created by Lxx on 2017/4/28. + */ +public class Stack { + public ArrayList array; + public int count; + public Stack(){ + array = new ArrayList(); + } + public void push (Object o){ + count ++ ; + array.add(o); + } + public Object pop(){ + count -- ; + Object o = array.get(count); + array.remove(o); + return o; + } + public Object peek(){ + if(count == 0){ + throw new EmptyStackException(); + } + return array.get(array.size()-1); + } + public boolean isEmpty(){ + return array.size() == 0; + } + public int size(){ + return count; + } + public String toString(){ + StringBuilder string = new StringBuilder(); + Stack s = new Stack(); + int size = this.size(); + for(int i = 0;i OPERATORS = Arrays.asList("+","-","*","/"); + public static final Map priorities = new HashMap<>(); + static { + priorities.put("+",1); + priorities.put("-",1); + priorities.put("*",2); + priorities.put("/",2); + } + static final int OPERATOR = 1; + static final int NUMBER = 2; + String value; + int type; + public Token(int type,String value){ + this.type = type; + this.value = value; + } + public boolean isNumber(){ + return type == NUMBER; + } + public boolean isOpretor(){ + return type == OPERATOR; + } + public int getIntValue(){ + return Integer.valueOf(value).intValue(); + } + public String toString(){ + return value; + } + public boolean hasHigherPriority(Token t){ + if (!this.isOpretor() && !t.isOpretor()){ + throw new RuntimeException(); + } + return priorities.get(this.value) - priorities.get(t.value) > 0; + } +} diff --git a/group23/601689050/6weekInfixExpr/TokenParser.java b/group23/601689050/6weekInfixExpr/TokenParser.java new file mode 100644 index 0000000000..3e966cc0a3 --- /dev/null +++ b/group23/601689050/6weekInfixExpr/TokenParser.java @@ -0,0 +1,47 @@ +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Lxx on 2017/4/28. + */ +public class TokenParser { + public List parse(String expr){ + List tokens = new ArrayList<>(); + int i = 0; + while (i < expr.length()) { + + char c = expr.charAt(i); + if (isOperator(c)) { + + Token t = new Token(Token.OPERATOR,String.valueOf(c)); + tokens.add(t); + i++; + }else if(Character.isDigit(c)){ + int nextOperatorIndex = indexOfNextOperator(i,expr); + String value = expr.substring(i,nextOperatorIndex); + Token t = new Token(Token.NUMBER,value); + tokens.add(t); + i = nextOperatorIndex; + }else { + System.out.println("输入值不符合要求"); + i++; + } + } + return tokens; + } + + private int indexOfNextOperator(int i, String expr) { + while(Character.isDigit(expr.charAt(i))){ + i++; + if(i == expr.length()){ + break; + } + } + return i; + } + + private boolean isOperator(char c) { + String sc = String.valueOf(c); + return Token.OPERATORS.contains(sc); + } +}