forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 9
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 onlyliuxin#79 from Viscaria233/master
jvm第4次作业
- Loading branch information
Showing
66 changed files
with
1,854 additions
and
193 deletions.
There are no files selected for viewing
140 changes: 0 additions & 140 deletions
140
group01/895457260/code/src/main/java/algorithm/InfixExpr.java
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
group01/895457260/code/src/main/java/algorithm/expression/InfixExpr.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,61 @@ | ||
package algorithm.expression; | ||
|
||
import datastructure.basic.Stack; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class InfixExpr { | ||
|
||
private Stack numbers = new Stack(); | ||
private Stack operators = new Stack(); | ||
|
||
private String expr; | ||
|
||
public InfixExpr(String expr) { | ||
this.expr = expr.replaceAll("\\s", ""); | ||
} | ||
|
||
public float evaluate() { | ||
numbers.clear(); | ||
operators.clear(); | ||
operators.push(Token.SCOPE); | ||
|
||
List<Token> tokens = TokenParser.parse(expr); | ||
tokens.add(Token.SCOPE); | ||
|
||
for (int i = 0; i < tokens.size() && !operators.isEmpty(); ++i) { | ||
Token token = tokens.get(i); | ||
if (token.isNumber()) { | ||
putNumber(token); | ||
} else { | ||
putOperator(token); | ||
} | ||
} | ||
return numbers.isEmpty() ? 0 : ((Token) numbers.peek()).getFloatValue(); | ||
} | ||
|
||
private void putNumber(Token num) { | ||
numbers.push(num); | ||
} | ||
|
||
private void putOperator(Token op) { | ||
int compare = Token.compare(op, (Token) operators.peek()); | ||
switch (compare) { | ||
case 1: | ||
operators.push(op); | ||
break; | ||
case 0: | ||
operators.pop(); | ||
break; | ||
case -1: | ||
Token num1 = (Token) numbers.pop(); | ||
Token num2 = (Token) numbers.pop(); | ||
Token operator = (Token) operators.pop(); | ||
Token result = Token.calculate(num2, operator, num1); | ||
numbers.push(result); | ||
putOperator(op); | ||
break; | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
group01/895457260/code/src/main/java/algorithm/expression/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 algorithm.expression; | ||
|
||
import datastructure.basic.Stack; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class InfixToPostfix { | ||
public static List<Token> convert(String expr) { | ||
List<Token> infix = TokenParser.parse(expr); | ||
List<Token> postfix = new ArrayList<>(); | ||
Stack op = new Stack(); | ||
|
||
for (Token token : infix) { | ||
if (token.isNumber()) { | ||
postfix.add(token); | ||
} else if (token.equals(Token.R_BRACKET)) { | ||
while (!op.peek().equals(Token.L_BRACKET)) { | ||
postfix.add((Token) op.pop()); | ||
} | ||
op.pop(); | ||
} else { | ||
while (!op.isEmpty() && Token.compare(token, (Token) op.peek()) < 0) { | ||
postfix.add((Token) op.pop()); | ||
} | ||
op.push(token); | ||
} | ||
} | ||
while (!op.isEmpty()) { | ||
postfix.add((Token) op.pop()); | ||
} | ||
return postfix; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
group01/895457260/code/src/main/java/algorithm/expression/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,46 @@ | ||
package algorithm.expression; | ||
|
||
import datastructure.basic.Stack; | ||
|
||
import java.util.EmptyStackException; | ||
import java.util.List; | ||
|
||
public class PostfixExpr { | ||
|
||
private String expr; | ||
private String splitRegex; | ||
private Stack stack = new Stack(); | ||
|
||
public PostfixExpr(String expr) { | ||
this.expr = expr; | ||
this.splitRegex = " "; | ||
} | ||
|
||
public PostfixExpr(String expr, String splitRegex) { | ||
this.expr = expr; | ||
this.splitRegex = splitRegex; | ||
} | ||
|
||
public float evaluate() { | ||
stack.clear(); | ||
List<Token> tokens = TokenParser.parse(expr, splitRegex); | ||
|
||
try { | ||
for (Token token : tokens) { | ||
if (token.isNumber()) { | ||
stack.push(token); | ||
} else { | ||
Token num1 = (Token) stack.pop(); | ||
Token num2 = (Token) stack.pop(); | ||
stack.push(Token.calculate(num2, token, num1)); | ||
} | ||
} | ||
} catch (EmptyStackException e) { | ||
throw new RuntimeException("Wrong expression: " + expr); | ||
} | ||
if (stack.size() != 1) { | ||
throw new RuntimeException("Wrong expression: " + expr); | ||
} | ||
return ((Token) stack.pop()).getFloatValue(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
group01/895457260/code/src/main/java/algorithm/expression/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,47 @@ | ||
package algorithm.expression; | ||
|
||
import datastructure.basic.Stack; | ||
|
||
import java.util.EmptyStackException; | ||
import java.util.List; | ||
|
||
public class PrefixExpr { | ||
private String expr; | ||
private String splitRegex; | ||
private Stack stack = new Stack(); | ||
|
||
public PrefixExpr(String expr) { | ||
this.expr = expr; | ||
this.splitRegex = " "; | ||
} | ||
|
||
public PrefixExpr(String expr, String splitRegex) { | ||
this.expr = expr; | ||
this.splitRegex = splitRegex; | ||
} | ||
|
||
public float evaluate() { | ||
stack.clear(); | ||
List<Token> tokens = TokenParser.parse(expr, splitRegex); | ||
|
||
try { | ||
for (int i = tokens.size() - 1; i >= 0; --i) { | ||
Token token = tokens.get(i); | ||
if (token.isNumber()) { | ||
stack.push(token); | ||
} else { | ||
Token num1 = (Token) stack.pop(); | ||
Token num2 = (Token) stack.pop(); | ||
Token result = Token.calculate(num1, token, num2); | ||
stack.push(result); | ||
} | ||
} | ||
} catch (EmptyStackException e) { | ||
throw new RuntimeException("Wrong expression: " + expr); | ||
} | ||
if (stack.size() != 1) { | ||
throw new RuntimeException("Wrong expression: " + expr); | ||
} | ||
return ((Token) stack.pop()).getFloatValue(); | ||
} | ||
} |
Oops, something went wrong.