Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions group24/121111914/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="E:/javaImprove/git/group24/121111914/lib/dom4j-1.6.1.jar" sourcepath="E:/javaImprove/git/group24/121111914/lib/dom4j-1.6.1-sources.jar"/>
<classpathentry kind="lib" path="lib/commons-io-2.5.jar"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.5.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions group24/121111914/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>JavaImp2017</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
package com.github.ipk2015.coding2017.basic;

import java.util.EmptyStackException;

public class Stack {
private ArrayList elementData = new ArrayList();

public void push(Object o){
elementData.add(o);
}

public Object pop(){
if(isEmpty()){
throw new EmptyStackException();
}
Object data=elementData.remove(size()-1);
return data;
}

public Object peek(){
if(isEmpty()){
throw new EmptyStackException();
}
return elementData.get(size()-1);
}
public boolean isEmpty(){
return size()==0;
}
public int size(){
return elementData.size();
}
}
package com.github.ipk2015.coding2017.basic.stack;

import java.util.EmptyStackException;

import com.github.ipk2015.coding2017.basic.ArrayList;

public class Stack {
private ArrayList elementData = new ArrayList();

public void push(Object o){
elementData.add(o);
}

public Object pop(){
if(isEmpty()){
throw new EmptyStackException();
}
Object data=elementData.remove(size()-1);
return data;
}

public Object peek(){
if(isEmpty()){
throw new EmptyStackException();
}
return elementData.get(size()-1);
}
public boolean isEmpty(){
return size()==0;
}
public int size(){
return elementData.size();
}
public String toString(){
StringBuffer buffer=new StringBuffer();
int size=elementData.size();
for(int i=0;i<size;i++){
buffer.append(elementData.get(i)+",");
}
return buffer.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.github.ipk2015.coding2017.basic.stack;


public class StackUtil {


/**
* 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
*/
public static void reverse(Stack s) {
Stack tempStack=new Stack();
int size=s.size();
if(size<2){
return;
}
for(int i=0;i<size;i++){
tempStack.push(s.pop());
}
Stack tempStack1=new Stack();
for(int i=0;i<size;i++){
tempStack1.push(tempStack.pop());
}
for(int i=0;i<size;i++){
s.push(tempStack1.pop());
}
}

/**
* 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
*
* @param o
*/
public static void remove(Stack s,Object o) {
Stack tempStack=new Stack();
Object tempElement;
while(!s.isEmpty()){
tempElement=s.pop();
if(tempElement.equals(o)){
break;
}
tempStack.push(tempElement);
}
while(!tempStack.isEmpty()){
s.push(tempStack.pop());
}

}

/**
* 从栈顶取得len个元素, 原来的栈中元素保持不变
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
* @param len
* @return
*/
public static Object[] getTop(Stack s,int len) {
if(len>s.size()){
throw new RuntimeException("len超出范围");
}
Stack tempStack=new Stack();
for(int i=0;i<len;i++){
tempStack.push(s.pop());
}
Object[] array=new Object[len];
Object tempObject;
for(int i=0;i<len;i++){
tempObject=tempStack.pop();
array[i]=tempObject;
s.push(tempObject);
}
return array;
}
/**
* 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz
* 使用堆栈检查字符串s中的括号是不是成对出现的。
* 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true
* 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false;
* @param s
* @return
*/
public static boolean isValidPairs(String s){
char[] charArray = s.toCharArray();
Stack stack=new Stack();
int flag;
for(int i=0;i<charArray.length;i++){
flag=switchChar(charArray[i]);
if(flag>0){
stack.push(flag);
}else if(flag<0){
if(stack.size()==0){
return false;
}
Integer peek = (Integer)stack.peek();
if(peek+flag==0){
stack.pop();
}else{
return false;
}
}
}
if(stack.size()>0){
return false;
}
return true;
}
private static int switchChar(char c){
int result=0;
switch(c){
case '(':
result=1;
break;
case ')':
result=-1;
break;
case '[':
result=2;
break;
case ']':
result=-2;
break;
case '{':
result=3;
break;
case '}':
result=-3;
break;
default:
break;
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.junit.Before;
import org.junit.Test;

import com.github.ipk2015.coding2017.basic.Stack;
import com.github.ipk2015.coding2017.basic.stack.Stack;

public class StackTest {
Stack stack;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.github.ipk2015.coding2017.basic.test;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import com.github.ipk2015.coding2017.basic.stack.Stack;
import com.github.ipk2015.coding2017.basic.stack.StackUtil;

public class StackUtilTest {
Stack stack=null;
@Before
public void setUp() throws Exception {
stack=new Stack();
}

@Test
public void testReverse() {
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
StackUtil.reverse(stack);
assertEquals("4,3,2,1,", stack.toString());
}

@Test
public void testRemove() {
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
StackUtil.remove(stack, 2);
assertEquals("1,3,4,", stack.toString());
StackUtil.remove(stack, 5);
assertEquals("1,3,4,", stack.toString());
}

@Test
public void testGetTop() {
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
Object[] array=StackUtil.getTop(stack, 3);
assertEquals("1,2,3,4,", stack.toString());
StringBuffer buffer=new StringBuffer();
for(int i=0;i<array.length;i++){
buffer.append(array[i]+",");
}
assertEquals("2,3,4,", buffer.toString());
}

@Test
public void testIsValidPairs() {
String s="([a{b}c])";
boolean result=StackUtil.isValidPairs(s);
assertEquals(true, result);
s="[a]{[(b)]}{(c)}";
result=StackUtil.isValidPairs(s);
assertEquals(true, result);
s="([a{b]c})";
result=StackUtil.isValidPairs(s);
assertEquals(false, result);
s="{[{a}]b]";
result=StackUtil.isValidPairs(s);
assertEquals(false, result);
s="{(a)}b(][)";
result=StackUtil.isValidPairs(s);
assertEquals(false, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.ipk2015.coding2017.minijvm.clz;



public class AccessFlag {
public static int ACC_PUBLIC = 0x0001;
public static int ACC_FINAL = 0x0002;
public static int ACC_SUPER = 0x0020;
public static int ACC_INTEERFACE = 0x0200;
public static int ACC_ABSTRACT = 0x0400;
public static int ACC_SYNTHETIC = 0x1000;
public static int ACC_ANNOTATION = 0x2000;
public static int ACC_ENUM = 0x4000;

private int flagValue;

public AccessFlag(int value) {
this.flagValue = value;
}

public int getFlagValue() {
return flagValue;
}

public void setFlagValue(int flag) {
this.flagValue = flag;
}

public boolean isPublicClass(){
return (this.flagValue & ACC_PUBLIC) != 0;
}

public boolean isFinalClass(){
return (this.flagValue & ACC_FINAL) != 0;
}

}
Loading