forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from eloiseSJTU/master
合并老师的代码
- Loading branch information
Showing
558 changed files
with
29,399 additions
and
2,807 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
group02/562768642/src/com/github/orajavac/coding2017/basic/stack/expr/InfixExpr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.github.orajavac.coding2017.basic.stack.expr; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import com.github.orajavac.coding2017.basic.stack.Stack; | ||
import com.github.orajavac.coding2017.jvm.util.Util; | ||
|
||
public class InfixExpr { | ||
|
||
String expr = null; | ||
|
||
private final String ADD = "+"; | ||
|
||
private final String SUB = "-"; | ||
|
||
private final String MUL = "*"; | ||
|
||
private final String DIV = "/"; | ||
|
||
private Map<String,Integer> operator = new HashMap<String,Integer>(); | ||
|
||
private Stack oper = new Stack(); | ||
|
||
private Stack num = new Stack(); | ||
|
||
public InfixExpr(String expr) { | ||
this.expr = expr; | ||
this.operator.put(this.ADD,1); | ||
this.operator.put(this.SUB,1); | ||
this.operator.put(this.MUL,2); | ||
this.operator.put(this.DIV,2); | ||
} | ||
|
||
public float evaluate() { | ||
Object[] obj = Util.parseOperNumToArray(this.expr); | ||
String key = null; | ||
for (int i=0;i<obj.length;i++){ | ||
key = obj[i].toString(); | ||
if (this.operator.containsKey(key)){ | ||
pushOper(key); | ||
}else{ | ||
num.push(key); | ||
} | ||
} | ||
//处理栈里剩余运算符和数字 | ||
int len = oper.length(); | ||
while (len!=0){ | ||
operation(oper.pop(),num.pop(),num.pop()); | ||
len--; | ||
} | ||
//System.out.println("运算式结果: "+Float.parseFloat(num.pop().toString())); | ||
return Float.parseFloat(num.pop().toString()); | ||
} | ||
|
||
public void pushOper(Object key){ | ||
//即将放入运算符与栈顶运算符比较 3-4*5+2 | ||
int y = oper.length(); | ||
for (int i=0;i<y;i++){ | ||
Object op = oper.pop(); | ||
int level1 = this.operator.get(op); | ||
int level2 = this.operator.get(key); | ||
if (level1 > level2){ //2+3*4+5 栈顶运算符大于即将入压运算符 | ||
operation(op,num.pop(),num.pop()); | ||
}else if (level1 == level2){ //3-20+2 栈顶运算符等于即将入压运算符 | ||
operation(op,num.pop(),num.pop()); | ||
}else{ | ||
oper.push(op); //把刚刚弹出,再压入栈 | ||
oper.push(key); | ||
} | ||
} | ||
if (oper.length() == 0){ | ||
oper.push(key); | ||
} | ||
} | ||
|
||
public void operation(Object oper,Object num1,Object num2){ | ||
Integer result = null; | ||
if(oper.equals("*")){ | ||
result = Integer.parseInt(num1.toString()) * Integer.parseInt(num2.toString()); | ||
num.push(result); | ||
} | ||
if(oper.equals("/")){ | ||
result = Integer.parseInt(num2.toString()) / Integer.parseInt(num1.toString()); | ||
num.push(result); | ||
} | ||
if(oper.equals("+")){ | ||
result = Integer.parseInt(num1.toString()) + Integer.parseInt(num2.toString()); | ||
num.push(result); | ||
} | ||
if(oper.equals("-")){ | ||
result = Integer.parseInt(num2.toString()) - Integer.parseInt(num1.toString()); | ||
num.push(result); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
group02/562768642/src/com/github/orajavac/coding2017/basic/stack/expr/InfixExprTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.github.orajavac.coding2017.basic.stack.expr; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
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+12*5-40/2+6"); | ||
Assert.assertEquals(106.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("3-4*5+2"); | ||
Assert.assertEquals(-15, expr.evaluate(), 0.001f); | ||
} | ||
|
||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
group02/562768642/src/com/github/orajavac/coding2017/jvm/attr/AttributeInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.github.orajavac.coding2017.jvm.attr; | ||
|
||
public abstract class AttributeInfo { | ||
|
||
public static final String CODE = "Code"; | ||
public static final String CONST_VALUE = "ConstantValue"; | ||
public static final String EXCEPTIONS = "Exceptions"; | ||
public static final String LINE_NUM_TABLE = "LineNumberTable"; | ||
public static final String LOCAL_VAR_TABLE = "LocalVariableTable"; | ||
public static final String STACK_MAP_TABLE = "StackMapTable"; | ||
int attrNameIndex; | ||
int attrLen ; | ||
public AttributeInfo(int attrNameIndex, int attrLen) { | ||
|
||
this.attrNameIndex = attrNameIndex; | ||
this.attrLen = attrLen; | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
group02/562768642/src/com/github/orajavac/coding2017/jvm/attr/CodeAttr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.github.orajavac.coding2017.jvm.attr; | ||
|
||
import com.github.orajavac.coding2017.jvm.clz.ClassFile; | ||
import com.github.orajavac.coding2017.jvm.loader.ByteCodeIterator; | ||
|
||
public class CodeAttr extends AttributeInfo{ | ||
|
||
private int maxStack ; | ||
private int maxLocals ; | ||
private int codeLen ; | ||
private String code; | ||
public String getCode() { | ||
return code; | ||
} | ||
|
||
//private ByteCodeCommand[] cmds ; | ||
//public ByteCodeCommand[] getCmds() { | ||
// return cmds; | ||
//} | ||
private LineNumberTable lineNumTable; | ||
private LocalVariableTable localVarTable; | ||
private StackMapTable stackMapTable; | ||
|
||
public CodeAttr(int attrNameIndex, int attrLen, int maxStack, int maxLocals, int codeLen,String code /*ByteCodeCommand[] cmds*/) { | ||
super(attrNameIndex, attrLen); | ||
this.maxStack = maxStack; | ||
this.maxLocals = maxLocals; | ||
this.codeLen = codeLen; | ||
this.code = code; | ||
//this.cmds = cmds; | ||
} | ||
|
||
public void setLineNumberTable(LineNumberTable t) { | ||
this.lineNumTable = t; | ||
} | ||
|
||
public void setLocalVariableTable(LocalVariableTable t) { | ||
this.localVarTable = t; | ||
} | ||
|
||
public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter){ | ||
int attrNameIndex = iter.nextU2ToInt(); | ||
int attrLen = iter.nextU4ToInt(); | ||
int maxStack = iter.nextU2ToInt(); | ||
int maxLocals = iter.nextU2ToInt(); | ||
int codeLen = iter.nextU4ToInt(); | ||
String code = iter.nextUxToHexString(codeLen); | ||
CodeAttr codeAttr = new CodeAttr(attrNameIndex,attrLen,maxStack,maxLocals,codeLen,code); | ||
int exceptionTableLen = iter.nextU2ToInt(); | ||
if (exceptionTableLen>0){ | ||
String exTable = iter.nextUxToHexString(exceptionTableLen); | ||
} | ||
int subAttrCount = iter.nextU2ToInt(); | ||
for (int x=1;x<=subAttrCount;x++){ | ||
int subAttrIndex = iter.nextU2ToInt(); | ||
String subAttrName = clzFile.getConstantPool().getUTF8String(subAttrIndex); | ||
iter.back(2); | ||
if(AttributeInfo.LINE_NUM_TABLE.equalsIgnoreCase(subAttrName)){ | ||
LineNumberTable t = LineNumberTable.parse(iter); | ||
codeAttr.setLineNumberTable(t); | ||
}else if (AttributeInfo.LOCAL_VAR_TABLE.equalsIgnoreCase(subAttrName)){ | ||
LocalVariableTable t = LocalVariableTable.parse(iter); | ||
codeAttr.setLocalVariableTable(t); | ||
}else if (AttributeInfo.STACK_MAP_TABLE.equalsIgnoreCase(subAttrName)){ | ||
StackMapTable t = StackMapTable.parse(iter); | ||
codeAttr.setStackMapTable(t); | ||
}else{ | ||
throw new RuntimeException("need code to process"); | ||
} | ||
} | ||
return null; | ||
} | ||
private void setStackMapTable(StackMapTable t) { | ||
this.stackMapTable = t; | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.