Skip to content

Commit

Permalink
Merge pull request #8 from DonaldY/master
Browse files Browse the repository at this point in the history
11周作业
  • Loading branch information
sdnb authored May 18, 2017
2 parents 6bb6bc2 + 7a7d3f1 commit de8c82d
Show file tree
Hide file tree
Showing 2,666 changed files with 130,979 additions and 12,210 deletions.
25 changes: 24 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#################
## Eclipse
#################
Expand Down Expand Up @@ -259,6 +258,24 @@ tmp
RemoteSystemsTempFiles
#.gitignore


build/
.idea/
.gradle/
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

#ide config
.metadata
.recommenders
.idea/
*.iml
Expand All @@ -269,3 +286,9 @@ target
*.DS_Store
liuxin/.DS_Store
liuxin/src/.DS_Store






110 changes: 110 additions & 0 deletions group01/1298552064/src/week05/basic/StackUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package week05.basic;

import java.util.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<Integer> s) {
Stack<Integer> reverseStack = new Stack<>();
Stack<Integer> sequenceStack = new Stack<>();
while(!s.isEmpty()){
reverseStack.push(s.pop());
}

while(!reverseStack.isEmpty()){
sequenceStack.push(reverseStack.pop());
}

while(!sequenceStack.isEmpty()){
s.push(sequenceStack.pop());
}
}

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

while(!tmpStack.isEmpty()){
s.push(tmpStack.pop());
}
}

/**
* 从栈顶取得len个元素, 原来的栈中元素保持不变
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
* @param len
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Object[] getTop(Stack s,int len) {
if(len < 0 ||len > s.size()){
throw new IllegalArgumentException("len is invalid argument.");
}

Object[] objects = new Object[len];
Stack tmpStack = new Stack<>();
int index = 0;
while(!s.isEmpty() && index < len){
Object o = s.pop();
tmpStack.push(o);
objects[index++] = o;
}

while(!tmpStack.isEmpty()){
s.push(tmpStack.pop());
}
return objects;
}
/**
* 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz
* 使用堆栈检查字符串s中的括号是不是成对出现的。
* 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true
* 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的,该方法返回false;
*
* @param s
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static boolean isValidPairs(String s){
String symStr = "";
Stack symStack = new Stack<>();
for(int i = 0; i < s.length(); i++){
if(!Character.isLetter(s.charAt(i))){
symStack.push(s.charAt(i));
symStr += s.charAt(i);
}
}

for(int i=0; i< symStr.length();i++){
char s1 = symStr.charAt(i);
char s2 = (char) symStack.pop();
if(s1 == '(' && s2 != ')'){
return false;
}
if(s1 == '[' && s2 != ']'){
return false;
}
if(s1 == '{' && s2 != '}'){
return false;
}
}

return true;
}
}
68 changes: 68 additions & 0 deletions group01/1298552064/src/week05/test/StackUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package week05.test;

import java.util.Stack;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import week05.basic.StackUtil;

public class StackUtilTest {
@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}


@Test
public void testReverse() {
Stack<Integer> s = new Stack();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString());
StackUtil.reverse(s);
Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString());
}

@Test
public void testRemove() {
Stack<Integer> s = new Stack();
s.push(1);
s.push(2);
s.push(3);
StackUtil.remove(s, 2);
Assert.assertEquals("[1, 3]", s.toString());
}

@Test
public void testGetTop() {
Stack<Integer> s = new Stack();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
{
Object[] values = StackUtil.getTop(s, 3);
Assert.assertEquals(5, values[0]);
Assert.assertEquals(4, values[1]);
Assert.assertEquals(3, values[2]);
System.out.println(s.toString());
}
}

@Test
public void testIsValidPairs() {
Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])"));
Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})"));
}

}
102 changes: 102 additions & 0 deletions group01/1298552064/src/week06/basic/InfixExpr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package week06.basic;

import java.util.List;
import java.util.Stack;

public class InfixExpr {
private String expr = null;

public InfixExpr(String expr) {
this.expr = expr;
}

/**
* 计算
* @param operator 运算符
* @param i1 操作数1
* @param i2 操作数2
* @return
*/
private int cal(char operator, int i1, int i2){
int result = 0;
switch(operator){
case '+' : result = i1 + i2 ; break;
case '-' : result = i1 - i2 ; break;
case '*' : result = i1 * i2 ; break;
case '/' :
if(i1 == 0){
throw new ArithmeticException("除数不能为0");
}
result = i1 / i2 ; break;
}
return result;
}

public float evaluate() {
TokenParser tokenParser = new TokenParser();
List<Token> tokens = tokenParser.parse(expr);
Stack<Token> operatorStack = new Stack<>();
Stack<Integer> operandStack = new Stack<>();
for(Token token : tokens){
if(token.isNumber()){
operandStack.push(token.getIntValue());
continue;
}

if(token.isOperator()){
if(operatorStack.isEmpty()){
operatorStack.push(token);
continue;
}

Token pre = operatorStack.peek();
boolean hasHigherPriority = token.hasHigherPriority(pre);
if(hasHigherPriority){
operatorStack.push(token);
}else{
int n2 = operandStack.pop();
int n1 = operandStack.pop();
String operator = operatorStack.pop().value;
operatorStack.push(token);
operandStack.push(cal(operator.charAt(0), n1, n2));
}
}
}

while(!operatorStack.isEmpty()){
if(operatorStack.size() == 1){
int n2 = operandStack.pop();
int n1 = operandStack.pop();
String operator = operatorStack.pop().value;
operandStack.push(cal(operator.charAt(0), n1, n2));
break;
}

Token cur = operatorStack.pop();
Token pre = operatorStack.pop();
if(cur.hasHigherPriority(pre)){
int n2 = operandStack.pop();
int n1 = operandStack.pop();
operandStack.push(cal(cur.value.charAt(0), n1, n2));

operatorStack.push(pre);
}else{
int n3 = operandStack.pop();
int n2 = operandStack.pop();
int n1 = operandStack.pop();
operandStack.push(cal(pre.value.charAt(0), n1, n2));
operandStack.push(n3);

operatorStack.push(cur);
}

}
return (float)operandStack.pop();
}

public static void main(String[] args) {
InfixExpr expr = new InfixExpr("10-2*3+50");
System.out.println(expr.evaluate());
}

}
53 changes: 53 additions & 0 deletions group01/1298552064/src/week06/basic/Token.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package week06.basic;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Token {
static final int OPERATOR = 1;
static final int NUMBER = 2;
String value;
int type;
public static final List<String> OPERATORS = Arrays.asList("+","-","*","/");
private static final Map<String, Integer> priorities = new HashMap<>();
static{
priorities.put("+", 1);
priorities.put("-", 1);
priorities.put("*", 2);
priorities.put("/", 2);
}


public Token(int type, String value){
this.type = type;
this.value = value;
}

public boolean isOperator(){
return type == OPERATOR;
}

public boolean isNumber(){
return type == NUMBER;
}

public int getIntValue(){
return Integer.parseInt(value);
}

@Override
public String toString(){
return value;
}

public boolean hasHigherPriority(Token t){
if(!this.isOperator() && !t.isOperator()){
throw new RuntimeException("numbers can't compare priority");
}
return priorities.get(this.value) - priorities.get(t.value) > 0;

}

}
Loading

0 comments on commit de8c82d

Please sign in to comment.