栈实现综合计算器
栈——使用栈完成计算一个表达式的结果
7 2  2-5+1-5+3 -4?
思路分析

栈的构建
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 
 | 
 class ArrayStack2{
 private int maxSize;
 private int[] stack;
 private int top = -1;
 
 
 public ArrayStack2 ( int maxSize ) {
 this.maxSize = maxSize;
 stack = new int[ this.maxSize ];
 }
 
 
 public boolean isFull(){
 return top == maxSize - 1;
 }
 
 public boolean isEmpty(){
 return top == -1;
 }
 
 
 public void push ( int value ) {
 
 if ( isFull ( ) ) {
 System.out.println ( "栈满" );
 return;
 }
 top++;
 stack[ top ] = value;
 }
 
 
 public int  pop ( ) {
 if ( isEmpty ( ) ) {
 
 throw new RuntimeException ( "栈空,没有数据" );
 }
 int value = stack[ top ];
 top--;
 return value;
 }
 
 
 public void list ( ) {
 if ( isEmpty ( ) ) {
 System.out.println ("栈空没有数据~" );
 return;
 }
 for ( int i = top ; i >= 0 ; i-- ) {
 System.out.printf ( "stack[%d]=%d\n",i,stack[i]);
 }
 }
 
 
 
 public int priority ( int oper) {
 if ( oper == '*' || oper == '/' ) {
 return 1;
 }
 else if ( oper == '+'|| oper == '-' ) {
 return 0;
 }else{
 return -1;
 }
 }
 
 
 public boolean isOper ( char oper ) {
 return oper == '+' || oper == '-' || oper == '*' || oper == '/';
 }
 
 
 
 
 
 
 
 
 public int cal ( int num1, int num2, int oper ) {
 int res=0;
 switch (oper){
 case '+':
 res = num1 + num2;
 break;
 case '-':
 res = num2 - num1;
 break;
 case '*':
 res = num2 * num1;
 break;
 case '/':
 res = num2 / num1;
 break;
 }
 return res;
 }
 
 
 public int peek(){
 return stack[ top ];
 }
 
 
 }
 
 | 
实例应用
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 
 | public class Calculator {public static void main ( String[] args ) {
 String expression = "7*2*2-5+1-5+3-4";
 
 ArrayStack2 numStack  = new ArrayStack2 ( 10 );
 ArrayStack2 operStack  = new ArrayStack2 ( 10 );
 
 int index = 0;
 int num1 = 0;
 int num2 = 0;
 int oper = 0;
 int res = 0;
 char ch = ' ';
 String keepnum="";
 
 while (true) {
 
 ch = expression.substring ( index, index + 1 ).charAt ( 0 );
 
 if ( operStack.isOper ( ch ) ) {
 
 if ( operStack.isEmpty ( ) ) {
 
 operStack.push ( ch );
 }
 else{
 
 
 
 
 
 if ( operStack.priority ( ch ) <= operStack.priority ( operStack.peek ( ) ) ) {
 num1 = numStack.pop ( );
 num2 = numStack.pop ( );
 oper=operStack.pop ( );
 res = numStack.cal ( num1, num2, oper );
 
 numStack.push ( res );
 
 operStack.push ( ch  );
 }
 
 else {
 operStack.push ( ch );
 }
 }
 
 }
 else {
 
 
 
 
 
 
 keepnum+=ch;
 
 
 if ( index==expression.length()-1){
 numStack.push ( Integer.parseInt ( keepnum ) );
 }else {
 
 
 if ( operStack.isOper ( expression.substring ( index + 1, index + 2 ).charAt ( 0 ) ) ) {
 numStack.push ( Integer.parseInt ( keepnum ) );
 
 keepnum = "";
 }
 }
 
 }
 
 index++;
 if ( index >= expression.length ( ) ) {
 break;
 }
 
 
 }
 
 
 while (!operStack.isEmpty ()) {
 num1 = numStack.pop ( );
 num2 = numStack.pop ( );
 oper=operStack.pop ( );
 res = numStack.cal ( num1, num2, oper );
 numStack.push ( res );
 }
 
 System.out.printf ("表达式 %s = %d",expression,numStack.pop ( ));
 
 
 
 }
 }
 
 |