Skip to content

Commit

Permalink
Merge pull request #65 from sulei0205/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
844028312 authored May 9, 2017
2 parents 75fe20b + ff76087 commit 2ecdae1
Show file tree
Hide file tree
Showing 23 changed files with 901 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static List<Integer> execute(int n, int m){
List<Integer> result = new ArrayList<Integer>();
int i = 0;

while (!queue.isEmpty()) {
while (!queue.isEmpty() && queue.size() > 1) {

int x = queue.deQueue();

Expand All @@ -32,7 +32,7 @@ public static List<Integer> execute(int n, int m){
}
}


System.out.println(result);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.junit.Before;
import org.junit.Test;

import com.coding.basic.stack.myselfQueue.QueueWithTwoStacks;



public class JosephusTest {
Expand All @@ -23,5 +25,19 @@ public void testExecute() {
Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString());

}

@Test
public void testTwoStackQueue(){
QueueWithTwoStacks<Integer> q = new QueueWithTwoStacks<Integer>();
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
q.enQueue(4);

System.out.println(q.deQueue());
System.out.println(q.deQueue());
System.out.println(q.deQueue());
System.out.println(q.deQueue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public float evaluate() {
TokenParser parser = new TokenParser();
List<Token> tokens = parser.parse(this.expr);


//存放操作符栈
Stack<Token> opStack = new Stack<>();
//操作数据值栈
Stack<Float> numStack = new Stack<>();

for(Token token : tokens){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public float evaluate() {

Stack<Float> numStack = new Stack<>();
for(Token token : tokens){
System.out.println(token+"----");
if(token.isNumber()){
numStack.push(new Float(token.getIntValue()));
} else{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void tearDown() throws Exception {
@Test
public void testEvaluate() {
{
PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *");
PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + * ");
Assert.assertEquals(288, expr.evaluate(),0.0f);
}
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ public List<Token> parse(String expr) {
List<Token> tokens = new ArrayList<>();

int i = 0;

while (i < expr.length()) {

char c = expr.charAt(i);

if (isOperator(c)) {

Token t = new Token(Token.OPERATOR, String.valueOf(c));
tokens.add(t);
i++;

} else if (Character.isDigit(c)) {

int nextOperatorIndex = indexOfNextOperator(i, expr);
String value = expr.substring(i, nextOperatorIndex);
Token t = new Token(Token.NUMBER, value);
Expand Down
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");
}


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

}

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



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

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

}
Loading

0 comments on commit 2ecdae1

Please sign in to comment.