Skip to content

Commit

Permalink
Merge pull request #7 from eloiseSJTU/master
Browse files Browse the repository at this point in the history
合并老师的代码
  • Loading branch information
miniyk2012 authored Apr 17, 2017
2 parents 4fd8d2a + eb7b3fb commit c0e03cd
Show file tree
Hide file tree
Showing 558 changed files with 29,399 additions and 2,807 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ private static class Node {

private Node first;// 链表头
private Node last;// 链表尾


public LRUPageFrame(){
init();
}

public LRUPageFrame(int capacity) {

this.capacity = capacity;

init();
}

public void init(){
first = new Node();
last = new Node();
last.flag = "last"; //用来标识最后一个节点是链表尾,在这里链表尾并不算做内存页
Expand Down Expand Up @@ -112,6 +119,21 @@ public void addFirst(Object pageNum){
length++;
}

public void add(Object pageNum){
Node node = first.next;
while (node.next!=null){
node = node.next;
}
Node e = new Node();
e.pageNum = pageNum;
Node prev = node.prev;
prev.next = e;
e.prev = prev;
e.next = node;
node.prev = e;
length++;
}

public String toString(){
StringBuilder buffer = new StringBuilder();
Node node = first;
Expand Down Expand Up @@ -188,4 +210,36 @@ public void remove(Object obj,Object o){
nodef = nodef.next;
}
}

/**
* Stack 使用
* @return
*/
public int getLength(){
return length;
}

/**
* Stack 使用 pop
* @return
*/
public Object remove(){
length--;
Node n = last.prev;
last.prev = n.prev;
n.prev.next = last;
return n.pageNum;
}

/**
* Stack 使用 pop
* @return
*/
public Object removeLow(){
length--;
Node n = first.next;
first.next = n.next;
n.next.prev = first;
return n.pageNum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,21 @@ public Stack(int capacity){
l = new LRUPageFrame(capacity);
}

public Stack(){
l = new LRUPageFrame();
}

public void push(Object o){
l.addFirst(o);
//l.addFirst(o);
l.add(o);
}

public Object pop(){
return l.remove();
}

public Object popLow(){
return l.removeLow();
}

public String toString(){
Expand All @@ -29,4 +42,8 @@ public void remove(Object obj){
public Object getFirstNode(){
return l.getFirstNode();
}

public int length(){
return l.getLength();
}
}
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);
}
}
}
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);
}

}
}
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;
}

}
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;

}

}
Loading

0 comments on commit c0e03cd

Please sign in to comment.