-
Notifications
You must be signed in to change notification settings - Fork 641
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 #61 from jy97799/master
4.23作业
- Loading branch information
Showing
51 changed files
with
2,683 additions
and
0 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,79 @@ | ||
package task7.expr; | ||
|
||
import org.junit.Assert; | ||
import task5.stack.Stack; | ||
|
||
import java.util.Arrays; | ||
|
||
public class InfixExpr { | ||
|
||
private String expr; | ||
|
||
private Stack<Float> numberStack = new Stack<>(); | ||
|
||
private Stack<Float> resultStack = new Stack<>(); | ||
|
||
public InfixExpr(String expr) { | ||
this.expr = expr; | ||
} | ||
|
||
public float evaluate() { | ||
|
||
Assert.assertNotNull(expr); | ||
|
||
String[] operators = expr.split("[\\d]+"); | ||
|
||
int length = operators.length; | ||
|
||
Arrays.stream(expr.split("[+\\-*/]+")) | ||
.map(Float::parseFloat) | ||
.forEach(numberStack::push); | ||
|
||
numberStack = reverse(numberStack); | ||
|
||
resultStack.push(numberStack.pop()); | ||
|
||
for (int i = 1; i < length; i++) { | ||
String currentOperator = operators[i]; | ||
// 先做乘除,结果放resultStack里面 | ||
switch (currentOperator) { | ||
case "*": | ||
resultStack.push(resultStack.pop() * numberStack.pop()); | ||
break; | ||
case "/": | ||
resultStack.push(resultStack.pop() / numberStack.pop()); | ||
break; | ||
case "+": | ||
resultStack.push(numberStack.pop()); | ||
break; | ||
case "-": | ||
resultStack.push(numberStack.pop()); | ||
break; | ||
} | ||
} | ||
|
||
resultStack = reverse(resultStack); | ||
|
||
// 做加减 | ||
for (int i = 1; i < length; i++) { | ||
String currentOperator = operators[i]; | ||
if ("+".equals(currentOperator)) { | ||
Float num1 = resultStack.pop(); | ||
Float num2 = resultStack.pop(); | ||
resultStack.push(num1 + num2); | ||
} else if ("-".equals(currentOperator)) { | ||
Float num1 = resultStack.pop(); | ||
Float num2 = resultStack.pop(); | ||
resultStack.push(num1 - num2); | ||
} | ||
} | ||
return resultStack.peek(); | ||
} | ||
|
||
private Stack<Float> reverse(Stack<Float> stackToReverse) { | ||
Stack<Float> temp = new Stack<>(); | ||
while (!stackToReverse.isEmpty()) | ||
temp.push(stackToReverse.pop()); | ||
return temp; | ||
} | ||
} |
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,56 @@ | ||
package task7.expr; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
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); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void testExprSplit() { | ||
String expr = "3*20+12*5-40/2"; | ||
Arrays.stream(expr.split("[+\\-*/]+")).forEach(System.out::println); | ||
Arrays.stream(expr.split("[\\d]+")).forEach(System.out::println); | ||
} | ||
|
||
} |
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,45 @@ | ||
package task7.expr; | ||
|
||
import task5.stack.Stack; | ||
|
||
import java.util.List; | ||
|
||
public class PostfixExpr { | ||
private String expr = null; | ||
|
||
private Stack<Float> numberStack = new Stack<>(); | ||
|
||
public PostfixExpr(String expr) { | ||
this.expr = expr; | ||
} | ||
|
||
public float evaluate() { | ||
List<Token> parse = new TokenParser().parse(expr); | ||
for (int i = 0; i < parse.size(); i++) { | ||
Token token = parse.get(i); | ||
// 后缀表达式:从左向右遍历 遇到操作数入栈,遇到操作符弹出操作数栈的两个数计算再入栈 | ||
if (token.isNumber()) | ||
numberStack.push((float) token.getIntValue()); | ||
else | ||
numberStack.push(cal(token.toString(), numberStack.pop(), numberStack.pop())); | ||
} | ||
/*while (!operatorStack.isEmpty()) { | ||
numberStack.push(cal(operatorStack.pop(), numberStack.pop(), numberStack.pop())); | ||
}*/ | ||
return numberStack.peek(); | ||
} | ||
|
||
private static float cal(String operator, float var1, float var2) { | ||
switch (operator) { | ||
case "+": | ||
return var2 + var1; | ||
case "-": | ||
return var2 - var1; | ||
case "*": | ||
return var2 * var1; | ||
case "/": | ||
return var2 / var1; | ||
} | ||
return -1; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
group15/1507_977996067/src/task7/expr/PostfixExprTest.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,37 @@ | ||
package task7.expr; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class PostfixExprTest { | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testEvaluate() { | ||
{ | ||
PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *"); | ||
Assert.assertEquals(288, expr.evaluate(), 0.0f); | ||
} | ||
{ | ||
//9+(3-1)*3+10/2 | ||
PostfixExpr expr = new PostfixExpr("9 3 1-3*+ 10 2/+"); | ||
Assert.assertEquals(20, expr.evaluate(), 0.0f); | ||
} | ||
|
||
{ | ||
//10-2*3+50 | ||
PostfixExpr expr = new PostfixExpr("10 2 3 * - 50 +"); | ||
Assert.assertEquals(54, expr.evaluate(), 0.0f); | ||
} | ||
} | ||
|
||
} |
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 task7.expr; | ||
|
||
import task5.stack.Stack; | ||
|
||
import java.util.List; | ||
|
||
public class PrefixExpr { | ||
|
||
private String expr; | ||
|
||
private Stack<Float> numberStack = new Stack<>(); | ||
|
||
public PrefixExpr(String expr) { | ||
this.expr = expr; | ||
|
||
} | ||
|
||
public float evaluate() { | ||
List<Token> parse = new TokenParser().parse(expr); | ||
for (int i = parse.size() - 1; i >= 0; i--) { | ||
Token token = parse.get(i); | ||
// 前缀表达式:从右向左遍历 遇到操作数入栈,遇到操作符弹出操作数栈的两个数计算再入栈 | ||
if (token.isNumber()) | ||
numberStack.push((float) token.getIntValue()); | ||
else | ||
numberStack.push(cal(token.toString(), numberStack.pop(), numberStack.pop())); | ||
} | ||
/*while (!operatorStack.isEmpty()) { | ||
numberStack.push(cal(operatorStack.pop(), numberStack.pop(), numberStack.pop())); | ||
}*/ | ||
return numberStack.peek(); | ||
} | ||
|
||
private static float cal(String operator, float var1, float var2) { | ||
switch (operator) { | ||
case "+": | ||
return var1 + var2; | ||
case "-": | ||
return var1 - var2; | ||
case "*": | ||
return var1 * var2; | ||
case "/": | ||
return var1 / var2; | ||
} | ||
return -1; | ||
} | ||
} |
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,44 @@ | ||
package task7.expr; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class PrefixExprTest { | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testEvaluate() { | ||
{ | ||
// 2*3+4*5 | ||
PrefixExpr expr = new PrefixExpr("+ * 2 3* 4 5"); | ||
Assert.assertEquals(26, expr.evaluate(), 0.001f); | ||
} | ||
{ | ||
// 4*2 + 6+9*2/3 -8 | ||
PrefixExpr expr = new PrefixExpr("- + + 6 / * 2 9 3 * 4 2 8"); | ||
Assert.assertEquals(12, expr.evaluate(), 0.001f); | ||
} | ||
{ | ||
//(3+4)*5-6 | ||
PrefixExpr expr = new PrefixExpr("- * + 3 4 5 6"); | ||
Assert.assertEquals(29, expr.evaluate(), 0.001f); | ||
} | ||
{ | ||
//1+((2+3)*4)-5 | ||
PrefixExpr expr = new PrefixExpr("- + 1 * + 2 3 4 5"); | ||
Assert.assertEquals(16, expr.evaluate(), 0.001f); | ||
} | ||
|
||
|
||
} | ||
|
||
} |
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,53 @@ | ||
package task7.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); | ||
} | ||
|
||
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; | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.