Skip to content

Commit

Permalink
week6 中序表达式求值 完成
Browse files Browse the repository at this point in the history
  • Loading branch information
vegetableDogBai committed Apr 14, 2017
1 parent 50b74b1 commit 5044710
Showing 1 changed file with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public float evaluate() {
//TODO analyze
char op = (char) opStack.pop();
if (level.get(chars[pos]) <= level.get(op)) {
int b = (int) numStack.pop();
int a = (int) numStack.pop();
int c = this.caculate(a, b, op);

int c = this.caculate(op);
numStack.push(c);
opStack.push(chars[pos]);
pos++;
Expand All @@ -62,12 +61,10 @@ public float evaluate() {

while(!opStack.isEmpty()){
char op = (char) opStack.pop();
int b = (int) numStack.pop();
int a = (int) numStack.pop();
int c = this.caculate(a, b, op);
numStack.push(c);

}
int c = this.caculate(op);
numStack.push(c);
}

return (int)numStack.pop();
}
Expand Down Expand Up @@ -96,7 +93,9 @@ private int getNumber() {
return num;
}

private int caculate(int a, int b, char c) {
private int caculate(char c) {
int b = (int) numStack.pop();
int a = (int) numStack.pop();
switch (c) {
case '+':
return a + b;
Expand All @@ -106,9 +105,6 @@ private int caculate(int a, int b, char c) {
return a * b;
case '/':
return a / b;



}
return -1;
}
Expand Down

0 comments on commit 5044710

Please sign in to comment.