forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #10 from zavier/master
sync
- Loading branch information
Showing
1,413 changed files
with
67,271 additions
and
6,665 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package week07.expr; | ||
|
||
import java.util.List; | ||
import java.util.Stack; | ||
|
||
public class InfixExpr { | ||
String expr = null; | ||
|
||
public InfixExpr(String expr) { | ||
this.expr = expr; | ||
} | ||
|
||
public float evaluate() { | ||
TokenParser parser = new TokenParser(); | ||
List<Token> tokens = parser.parse(expr); | ||
|
||
Stack<Token> opStack = new Stack<>(); | ||
Stack<Float> numStack = new Stack<>(); | ||
|
||
for (Token token : tokens) { | ||
if (token.isOperator()) { | ||
if (opStack.isEmpty()) { | ||
opStack.push(token); | ||
} else { | ||
while (!opStack.isEmpty() && !token.hasHigherPriority(opStack.peek())) { | ||
Token prevOperator = opStack.pop(); | ||
Float f2 = numStack.pop(); | ||
Float f1 = numStack.pop(); | ||
Float result = calculate(prevOperator.toString(), f1, f2); | ||
numStack.push(result); | ||
} | ||
opStack.push(token); | ||
} | ||
} | ||
if (token.isNumber()) { | ||
|
||
numStack.push(new Float(token.getIntValue())); | ||
} | ||
} | ||
|
||
while (!opStack.isEmpty()) { | ||
Token token = opStack.pop(); | ||
Float f2 = numStack.pop(); | ||
Float f1 = numStack.pop(); | ||
numStack.push(calculate(token.toString(), f1, f2)); | ||
} | ||
return numStack.pop().floatValue(); | ||
} | ||
|
||
private Float calculate(String op, Float f1, Float f2) { | ||
if (op.equals("+")) { | ||
return f1 + f2; | ||
} | ||
if (op.equals("-")) { | ||
return f1 - f2; | ||
} | ||
if (op.equals("*")) { | ||
return f1 * f2; | ||
} | ||
if (op.equals("/")) { | ||
return f1 / f2; | ||
} | ||
throw new RuntimeException(op + " is not supported"); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
group01/1814014897/zhouhui/src/week07/expr/InfixToPostfix.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,34 @@ | ||
package week07.expr; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Stack; | ||
|
||
public class InfixToPostfix { | ||
|
||
public static List<Token> convert(String expr) { | ||
List<Token> inFixTokens = new TokenParser().parse(expr); | ||
|
||
List<Token> postFixTokens = new ArrayList<>(); | ||
|
||
Stack<Token> opStack = new Stack<Token>(); | ||
|
||
for (Token token : inFixTokens) { | ||
if (token.isOperator()) { | ||
while (!opStack.isEmpty() && !token.hasHigherPriority(opStack.peek())) { | ||
postFixTokens.add(opStack.pop()); | ||
} | ||
opStack.push(token); | ||
} | ||
if (token.isNumber()) { | ||
postFixTokens.add(token); | ||
} | ||
} | ||
|
||
while (!opStack.isEmpty()) { | ||
postFixTokens.add(opStack.pop()); | ||
} | ||
return postFixTokens; | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
group01/1814014897/zhouhui/src/week07/expr/PostfixExpr.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,48 @@ | ||
package week07.expr; | ||
|
||
import java.util.List; | ||
import java.util.Stack; | ||
|
||
public class PostfixExpr { | ||
String expr = null; | ||
|
||
public PostfixExpr(String expr) { | ||
this.expr = expr; | ||
} | ||
|
||
public float evaluate() { | ||
TokenParser parser = new TokenParser(); | ||
List<Token> tokens = parser.parse(expr); | ||
|
||
Stack<Float> numStack = new Stack<>(); | ||
for (Token token : tokens) { | ||
if (token.isNumber()) { | ||
numStack.push(new Float(token.getIntValue())); | ||
} else if (token.isOperator()) { | ||
Float f2 = numStack.pop(); | ||
Float f1 = numStack.pop(); | ||
Float result = calculate(token.toString(), f1, f2); | ||
numStack.push(result); | ||
} | ||
} | ||
|
||
return numStack.pop().floatValue(); | ||
} | ||
|
||
private Float calculate(String op, Float f1, Float f2) { | ||
if (op.equals("+")) { | ||
return f1 + f2; | ||
} | ||
if (op.equals("-")) { | ||
return f1 - f2; | ||
} | ||
if (op.equals("*")) { | ||
return f1 * f2; | ||
} | ||
if (op.equals("/")) { | ||
return f1 / f2; | ||
} | ||
throw new RuntimeException(op + " is not supported"); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
group01/1814014897/zhouhui/src/week07/expr/PrefixExpr.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,52 @@ | ||
package week07.expr; | ||
|
||
import java.util.List; | ||
import java.util.Stack; | ||
|
||
public class PrefixExpr { | ||
String expr = null; | ||
|
||
public PrefixExpr(String expr) { | ||
this.expr = expr; | ||
} | ||
|
||
public float evaluate() { | ||
TokenParser parser = new TokenParser(); | ||
List<Token> tokens = parser.parse(expr); | ||
|
||
Stack<Token> exprStack = new Stack<>(); | ||
Stack<Float> numStack = new Stack<>(); | ||
for (Token token : tokens) { | ||
exprStack.push(token); | ||
} | ||
|
||
while (!exprStack.isEmpty()) { | ||
if (exprStack.peek().isNumber()) { | ||
numStack.push(new Float(exprStack.pop().getIntValue())); | ||
} else if (exprStack.peek().isOperator()) { | ||
Float f1 = numStack.pop(); | ||
Float f2 = numStack.pop(); | ||
Float result = calculate(exprStack.pop().toString(), f1, f2); | ||
numStack.push(result); | ||
} | ||
} | ||
return numStack.pop().floatValue(); | ||
} | ||
|
||
private Float calculate(String op, Float f1, Float f2) { | ||
if (op.equals("+")) { | ||
return f1 + f2; | ||
} | ||
if (op.equals("-")) { | ||
return f1 - f2; | ||
} | ||
if (op.equals("*")) { | ||
return f1 * f2; | ||
} | ||
if (op.equals("/")) { | ||
return f1 / f2; | ||
} | ||
throw new RuntimeException(op + "is not supported"); | ||
} | ||
|
||
} |
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,50 @@ | ||
package week07.expr; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Token { | ||
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); | ||
} | ||
static final int OPERATOR = 1; | ||
static final int NUMBER = 2; | ||
String value; | ||
int type; | ||
public Token(int type, String value){ | ||
this.type = type; | ||
this.value = value; | ||
} | ||
|
||
public boolean isNumber() { | ||
return type == NUMBER; | ||
} | ||
|
||
public boolean isOperator() { | ||
return type == OPERATOR; | ||
} | ||
|
||
public int getIntValue() { | ||
return Integer.valueOf(value).intValue(); | ||
} | ||
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; | ||
} | ||
|
||
|
||
|
||
} |
57 changes: 57 additions & 0 deletions
57
group01/1814014897/zhouhui/src/week07/expr/TokenParser.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,57 @@ | ||
package week07.expr; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class TokenParser { | ||
|
||
|
||
public List<Token> parse(String expr) { | ||
List<Token> tokens = new ArrayList<>(); | ||
|
||
int i = 0; | ||
|
||
while (i < expr.length()) { | ||
|
||
char c = expr.charAt(i); | ||
|
||
if (isOperator(c)) { | ||
|
||
Token t = new Token(Token.OPERATOR, String.valueOf(c)); | ||
tokens.add(t); | ||
i++; | ||
|
||
} else if (Character.isDigit(c)) { | ||
|
||
int nextOperatorIndex = indexOfNextOperator(i, expr); | ||
String value = expr.substring(i, nextOperatorIndex); | ||
Token t = new Token(Token.NUMBER, value); | ||
tokens.add(t); | ||
i = nextOperatorIndex; | ||
|
||
} else{ | ||
System.out.println("char :["+c+"] is not number or operator,ignore"); | ||
i++; | ||
} | ||
|
||
} | ||
return tokens; | ||
} | ||
|
||
private int indexOfNextOperator(int i, String expr) { | ||
|
||
while (Character.isDigit(expr.charAt(i))) { | ||
i++; | ||
if (i == expr.length()) { | ||
break; | ||
} | ||
} | ||
return i; | ||
|
||
} | ||
|
||
private boolean isOperator(char c) { | ||
String sc = String.valueOf(c); | ||
return Token.OPERATORS.contains(sc); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
group01/1814014897/zhouhui/src/week07/expr/test/AllTest.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,18 @@ | ||
package week07.expr.test; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Suite; | ||
import org.junit.runners.Suite.SuiteClasses; | ||
|
||
@RunWith(Suite.class) | ||
@SuiteClasses({ | ||
InfixExprTest.class, | ||
InfixToPostfixTest.class, | ||
PostfixExprTest.class, | ||
PrefixExprTest.class, | ||
TokenParserTest.class | ||
}) | ||
|
||
public class AllTest { | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
group01/1814014897/zhouhui/src/week07/expr/test/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,54 @@ | ||
package week07.expr.test; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import week07.expr.InfixExpr; | ||
|
||
|
||
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/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("10-2*3+50"); | ||
Assert.assertEquals(54, expr.evaluate(), 0.001f); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.