forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gongxun
committed
Apr 22, 2017
1 parent
ca83f60
commit cd642e8
Showing
15 changed files
with
359 additions
and
19 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package expr; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by gongxun on 2017/4/22. | ||
*/ | ||
public class InfixToPostfix { | ||
public static List<Token> convert(String expr) { | ||
|
||
return null; | ||
} | ||
} |
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,16 @@ | ||
package expr; | ||
|
||
/** | ||
* Created by gongxun on 2017/4/22. | ||
*/ | ||
public class PostfixExpr { | ||
String expr = null; | ||
|
||
public PostfixExpr(String expr) { | ||
this.expr = expr; | ||
} | ||
|
||
public float evaluate() { | ||
return 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,38 @@ | ||
package expr; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by gongxun on 2017/4/22. | ||
*/ | ||
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,16 @@ | ||
package expr; | ||
|
||
/** | ||
* Created by gongxun on 2017/4/22. | ||
*/ | ||
public class PrefixExpr { | ||
String expr = null; | ||
|
||
public PrefixExpr(String expr) { | ||
this.expr = expr; | ||
} | ||
|
||
public float evaluate() { | ||
return 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,45 @@ | ||
package expr; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by gongxun on 2017/4/22. | ||
*/ | ||
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,52 @@ | ||
package expr; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by gongxun on 2017/4/22. | ||
*/ | ||
public class Token { | ||
public static final List<String> OPERATORS = Arrays.asList("+", "-", "*", "/"); | ||
private static final Map<String,Integer> priorities = new HashMap<String,Integer>(); | ||
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; | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.