-
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 #65 from sulei0205/master
Master
- Loading branch information
Showing
23 changed files
with
901 additions
and
8 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
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
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
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
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
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
73 changes: 73 additions & 0 deletions
73
group04/1020483199/HomeworkSuppned/src/com/coding/basic/stack/myself/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,73 @@ | ||
package com.coding.basic.stack.myself; | ||
|
||
import java.util.List; | ||
import java.util.Stack; | ||
|
||
import com.coding.basic.stack.myself.Token; | ||
|
||
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){ | ||
//当前的token代表的是操作符时 | ||
if (token.isOperator()){ | ||
//当前操作符栈不为空并且当前操作符并不比栈顶优先级更高 | ||
while (!opStack.isEmpty() | ||
&& !token.hasHigherPriority(opStack.peek())){ | ||
Token preOperate = opStack.pop(); | ||
float f2 = numStack.pop(); | ||
float f1 = numStack.pop(); | ||
float result = calculate(preOperate.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(); | ||
|
||
} | ||
|
||
public 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
group04/1020483199/HomeworkSuppned/src/com/coding/basic/stack/myself/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,52 @@ | ||
package com.coding.basic.stack.myself; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
|
||
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); | ||
} | ||
|
||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
group04/1020483199/HomeworkSuppned/src/com/coding/basic/stack/myself/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,79 @@ | ||
package com.coding.basic.stack.myself; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Stack; | ||
|
||
import com.coding.basic.stack.myself.Token; | ||
|
||
public class InfixToPostfix { | ||
|
||
public static List<Token> convert(String expr) { | ||
/*List<Token> infixPost = new TokenParser().parse(expr); | ||
List<Token> postFixTokens = new ArrayList<>(); | ||
Stack<Token> opStack = new Stack<Token>(); | ||
//2-3*4+5 | ||
for (Token token:infixPost){ | ||
if(token.isOperator()){ | ||
while(!opStack.isEmpty() | ||
&& !token.hasHigherPriority(opStack.peek())){ | ||
System.out.println("满足条件的token"+token); | ||
postFixTokens.add(opStack.pop());// | ||
} | ||
opStack.push(token);//- | ||
} | ||
if(token.isNumber()){ | ||
postFixTokens.add(token);//2 3 | ||
} | ||
} | ||
while (!opStack.isEmpty()){ | ||
postFixTokens.add(opStack.pop()); | ||
} | ||
return postFixTokens;*/ | ||
|
||
List<Token> infixPost = new TokenParser().parse(expr); | ||
|
||
List<Token> postFixTokens = new ArrayList<Token>(); | ||
|
||
Stack<Token> openStack = new Stack<Token>(); | ||
|
||
for (Token token:infixPost){ | ||
|
||
if (token.isOperator()){ | ||
|
||
while (!openStack.isEmpty() | ||
&& !token.hasHigherPriority(openStack.peek())){ | ||
postFixTokens.add(openStack.pop()); | ||
} | ||
|
||
openStack.push(token); | ||
} | ||
|
||
if (token.isNumber()){ | ||
|
||
postFixTokens.add(token); | ||
} | ||
} | ||
|
||
while (!openStack.isEmpty()){ | ||
postFixTokens.add(openStack.pop()); | ||
} | ||
|
||
return postFixTokens; | ||
} | ||
|
||
|
||
|
||
} |
40 changes: 40 additions & 0 deletions
40
group04/1020483199/HomeworkSuppned/src/com/coding/basic/stack/myself/InfixToPostfixTest.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,40 @@ | ||
package com.coding.basic.stack.myself; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
|
||
|
||
public class InfixToPostfixTest { | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testConvert() { | ||
{ | ||
List<Token> tokens = InfixToPostfix.convert("2+3"); | ||
Assert.assertEquals("[2, 3, +]", tokens.toString()); | ||
} | ||
{ | ||
|
||
List<Token> tokens = InfixToPostfix.convert("2+3*4"); | ||
Assert.assertEquals("[2, 3, 4, *, +]", tokens.toString()); | ||
} | ||
{ | ||
|
||
List<Token> tokens = InfixToPostfix.convert("2-3*4+5"); | ||
Assert.assertEquals("[2, 3, 4, *, -, 5, +]", tokens.toString()); | ||
} | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
group04/1020483199/HomeworkSuppned/src/com/coding/basic/stack/myself/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,49 @@ | ||
package com.coding.basic.stack.myself; | ||
|
||
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<Float>(); | ||
for (Token token:tokens){ | ||
if (token.isNumber()){ | ||
numStack.push(new Float(token.getIntValue())); | ||
}else{ | ||
float f2 = numStack.pop(); | ||
float f1 = numStack.pop(); | ||
float result = calculate(token.toString(), f1, f2); | ||
numStack.push(result); | ||
} | ||
|
||
|
||
} | ||
return numStack.pop().floatValue(); | ||
} | ||
|
||
public 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"); | ||
} | ||
|
||
} |
Oops, something went wrong.