Skip to content

Commit fbf6bb4

Browse files
authored
Merge pull request #60 from lzbferrari/master
JVM第三周作业
2 parents dbbff09 + 7717f0a commit fbf6bb4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2490
-181
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package me.lzb.common.utils;
2+
3+
/**
4+
* Created by LZB on 2017/4/14.
5+
*/
6+
public class ByteUtils {
7+
8+
public static String byteToHexString(byte[] codes ){
9+
StringBuffer buffer = new StringBuffer();
10+
for(int i=0;i<codes.length;i++){
11+
byte b = codes[i];
12+
int value = b & 0xFF;
13+
String strHex = Integer.toHexString(value);
14+
if(strHex.length()< 2){
15+
strHex = "0" + strHex;
16+
}
17+
buffer.append(strHex);
18+
}
19+
return buffer.toString();
20+
}
21+
22+
public static int byteToInt(byte[] codes){
23+
String s1 = byteToHexString(codes);
24+
return Integer.valueOf(s1, 16).intValue();
25+
}
26+
}

group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/utils/FileUtils.java renamed to group24/1148285693/learning2017/common/src/main/java/me/lzb/common/utils/FileUtils.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.lzb.utils;
1+
package me.lzb.common.utils;
22

33
import java.io.*;
44

@@ -15,17 +15,25 @@ public class FileUtils {
1515
* @return true false
1616
*/
1717
public static boolean isFileExist(String path, String name) {
18-
File file = new File(path + "\\" + name);
18+
return isFileExist(path + File.separator + name);
19+
}
20+
21+
public static boolean isFileExist(String f) {
22+
File file = new File(f);
1923
return file.exists();
2024
}
2125

26+
2227
/**
2328
* 读取文件为二进制数组
2429
* @param clzFileName 文件路径
2530
* @return 数组
2631
* @throws IOException
2732
*/
2833
public static byte[] readByteCodes(String clzFileName) throws IOException {
34+
if(!isFileExist(clzFileName)){
35+
return null;
36+
}
2937

3038
File f = new File(clzFileName);
3139

group24/1148285693/learning2017/mini-jvm/src/main/java/me/lzb/utils/StringUtils.java renamed to group24/1148285693/learning2017/common/src/main/java/me/lzb/common/utils/StringUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package me.lzb.utils;
1+
package me.lzb.common.utils;
22

33
/**
44
* Created by LZB on 2017/4/4.
55
*/
66
public class StringUtils extends org.apache.commons.lang3.StringUtils {
7+
78
}

group24/1148285693/learning2017/learning-basic/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,10 @@
1818

1919

2020
<dependencies>
21+
<dependency>
22+
<groupId>me.lzb</groupId>
23+
<artifactId>common</artifactId>
24+
<version>1.0</version>
25+
</dependency>
2126
</dependencies>
2227
</project>

group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/ArrayList.java renamed to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/ArrayList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.lzb.datastructure;
1+
package me.lzb.basic;
22

33
/**
44
* 简易ArrayList

group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/ArrayUtil.java renamed to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/ArrayUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.lzb.datastructure;
1+
package me.lzb.basic;
22

33
public class ArrayUtil {
44

group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/BinaryTreeNode.java renamed to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/BinaryTreeNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.lzb.datastructure;
1+
package me.lzb.basic;
22

33
/**
44
* 左边比父节点小,右边比父节点大
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package me.lzb.basic;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Stack;
6+
7+
/**
8+
* Created by LZB on 2017/4/15.
9+
*/
10+
public class InfixExpr {
11+
12+
13+
private String expr;
14+
15+
public InfixExpr(String expr) {
16+
this.expr = expr;
17+
}
18+
19+
public float evaluate() {
20+
21+
List<Node> list = processExpr();
22+
23+
Stack<String> symbolStack = new Stack<>();
24+
Stack<Float> numberStack = new Stack<>();
25+
26+
boolean calLevel2 = false;
27+
for (Node n : list) {
28+
if (n.isNumber) {
29+
numberStack.push(n.number);
30+
if (calLevel2) {
31+
calculate(symbolStack, numberStack, false);
32+
calLevel2 = false;
33+
}
34+
} else {
35+
symbolStack.push(n.symbol);
36+
37+
if (n.isLevel2()) {
38+
calLevel2 = true;
39+
}
40+
}
41+
}
42+
43+
44+
Stack<Float> tn = new Stack<>();
45+
int nsize = numberStack.size();
46+
for (int i = 0; i < nsize; i++) {
47+
tn.push(numberStack.pop());
48+
}
49+
50+
numberStack = tn;
51+
52+
53+
Stack<String> ts = new Stack<>();
54+
int ssize = symbolStack.size();
55+
for (int i = 0; i < ssize; i++) {
56+
ts.push(symbolStack.pop());
57+
}
58+
59+
symbolStack = ts;
60+
61+
62+
while (!symbolStack.isEmpty()) {
63+
calculate(symbolStack, numberStack, true);
64+
}
65+
66+
67+
return numberStack.pop();
68+
}
69+
70+
71+
72+
private List<Node> processExpr() {
73+
List<Node> list = new ArrayList<>();
74+
char[] array = this.expr.toCharArray();
75+
String number = "";
76+
for (int i = 0; i < array.length; i++) {
77+
if (Character.isDigit(array[i])) {
78+
number = number + String.valueOf(array[i]);
79+
} else {
80+
Node num = new Node(Float.valueOf(number), null, true, -1);
81+
number = "";
82+
int calLevel = "+-".indexOf(array[i]) >= 0 ? 1 : 2;
83+
Node sym = new Node(0, String.valueOf(array[i]), false, calLevel);
84+
list.add(num);
85+
list.add(sym);
86+
}
87+
}
88+
89+
Node num = new Node(Float.valueOf(number), null, true, -1);
90+
list.add(num);
91+
return list;
92+
}
93+
94+
95+
private void calculate(Stack<String> symbolStack, Stack<Float> numberStack, boolean isRe) {
96+
if (symbolStack.isEmpty()) {
97+
return;
98+
}
99+
100+
101+
String symbole = symbolStack.pop();
102+
103+
float right;
104+
float left;
105+
106+
if(isRe){
107+
left = numberStack.pop();
108+
right = numberStack.pop();
109+
}else {
110+
right = numberStack.pop();
111+
left = numberStack.pop();
112+
}
113+
114+
115+
116+
float r = calculate(symbole, left, right);
117+
118+
numberStack.push(r);
119+
}
120+
121+
122+
private float calculate(String symbol, float left, float right) {
123+
if ("+".equals(symbol)) {
124+
return left + right;
125+
}
126+
127+
if ("-".equals(symbol)) {
128+
return left - right;
129+
}
130+
131+
if ("*".equals(symbol)) {
132+
return left * right;
133+
}
134+
135+
if ("/".equals(symbol)) {
136+
return left / right;
137+
}
138+
139+
return 0;
140+
}
141+
142+
143+
private class Node {
144+
float number;
145+
String symbol;
146+
boolean isNumber;
147+
int calLevel;//加减1,乘除2
148+
149+
public Node(float number, String symbol, boolean isNumber, int calLevel) {
150+
this.number = number;
151+
this.symbol = symbol;
152+
this.isNumber = isNumber;
153+
this.calLevel = calLevel;
154+
}
155+
156+
private boolean isLevel2() {
157+
return calLevel == 2;
158+
}
159+
}
160+
161+
}

group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/datastructure/Iterator.java renamed to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/Iterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.lzb.datastructure;
1+
package me.lzb.basic;
22

33
/**
44
* Created by LZB on 2017/3/11.

group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/algorithm/LRUPageFrame.java renamed to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/LRUPageFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.lzb.algorithm;
1+
package me.lzb.basic;
22

33
/**
44
* 用双向链表实现LRU算法

0 commit comments

Comments
 (0)