Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.johnChnia.coding2017.basic.stack.expr;


import com.johnChnia.coding2017.basic.List;

public class InfixExpr {
String expr = null;

public InfixExpr(String expr) {
this.expr = expr;
}

public float evaluate() {
List<Token> tokens = InfixToPostfix.convert(this.expr);
return PostfixExpr.evaluate(tokens);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.johnChnia.coding2017.basic.stack.expr;

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);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.johnChnia.coding2017.basic.stack.expr;


import com.johnChnia.coding2017.basic.ArrayList;
import com.johnChnia.coding2017.basic.List;
import com.johnChnia.coding2017.basic.stack.Stack;


/***
* Rule:
*/
public class InfixToPostfix {

public static List<Token> convert(String expr) {
TokenParser tokenParser = new TokenParser();
List<Token> tokens = tokenParser.parse(expr);
List<Token> list = new ArrayList<>();
Stack<Token> stack = new Stack<>();
for (int i = 0; i < tokens.size(); i++) {
Token token = tokens.get(i);
if (token.isNumber()) {
list.add(token);
} else if (token.isOperator()) {
while (!stack.empty() && !token.hasHigherPriority(stack.peek())) {
list.add(stack.pop());
}
stack.push(token);

}
}
while (!stack.empty()) {
list.add(stack.pop());
}
return list;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.johnChnia.coding2017.basic.stack.expr;

import com.johnChnia.coding2017.basic.stack.Stack;

/**
* Created by john on 2017/4/20.
*/
public class Operator {

public void handlerToken(String fix, Stack<Float> stack, Token token) {
if (token.isNumber()) {
stack.push(Float.parseFloat(token.toString()));
} else if (token.isOperator()) {
float p = stack.pop();
float q = stack.pop();
stack.push(perform(fix, token.toString(), p, q));
}
}


private float perform(String fix, String operator, float p, float q) {
float result = 0.0f;
if (operator.equals("+")) {
result = p + q;
} else if (operator.equals("-")) {
if (fix.equals("postfix")) {
result = q - p;
} else if (fix.equals("prefix")) {
result = p - q;
}
} else if (operator.equals("*")) {
result = p * q;
} else if (operator.equals("/")) {
if (fix.equals("postfix")) {
result = q / p;
} else if (fix.equals("prefix")) {
result = p / q;
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.johnChnia.coding2017.basic.stack.expr;

import com.johnChnia.coding2017.basic.List;
import com.johnChnia.coding2017.basic.stack.Stack;

public class PostfixExpr {
private String expr = null;
static Operator operator = new Operator();


public PostfixExpr(String expr) {
this.expr = expr;
}

public float evaluate() {
TokenParser tokenParser = new TokenParser();
List<Token> tokens = tokenParser.parse(this.expr);
Stack<Float> stack = new Stack<>();
for (int i = 0; i < tokens.size(); i++) {
operator.handlerToken("postfix", stack, tokens.get(i));

}

return stack.pop();
}

public static float evaluate(List<Token> tokens) {
Stack<Float> stack = new Stack<>();
for (int i = 0; i < tokens.size(); i++) {
operator.handlerToken("postfix", stack, tokens.get(i));
}
return stack.pop();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.johnChnia.coding2017.basic.stack.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);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.johnChnia.coding2017.basic.stack.expr;

import com.johnChnia.coding2017.basic.List;
import com.johnChnia.coding2017.basic.stack.Stack;

public class PrefixExpr {
String expr = null;

public PrefixExpr(String expr) {
this.expr = expr;
}

public float evaluate() {
TokenParser tokenParser = new TokenParser();
List<Token> tokens = tokenParser.parse(this.expr);
Operator operator = new Operator();
Stack<Float> stack = new Stack<>();
for (int i = tokens.size() - 1; i >= 0; i--) {
operator.handlerToken("prefix", stack, tokens.get(i));
}
return stack.pop();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.johnChnia.coding2017.basic.stack.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);
}


}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.johnChnia.coding2017.basic.stack.expr;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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;
}



}
Loading