逆波兰计算器完整版
栈——通过中缀转后缀在进行逆波兰计算式构建计算器
只能在运算式中出现数字()+ - * / .
代码
1 2 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| public class PolandNotation { public static void main ( String[] args ) {
String expression = "1+((22.222+3)*4.05)-5"; List<String> list = toInfixList ( expression ); System.out.println ("中缀表达式List="+ list ); List<String> suffixlist = turnSuffixExpression ( list ); System.out.println ( "后缀表达式List="+ suffixlist ); float res=calculate ( suffixlist ); System.out.printf ( "%s=%f" ,expression, res );
}
public static List<String> toInfixList ( String s ) { List<String> ls=new ArrayList<String> ( ) ; int i = 0; String str; char c; do { if ( (c=s.charAt ( i ))<48 ||(c=s.charAt ( i ))>57&&(c=s.charAt ( i ))!='.' ){ ls.add ( "" + c ); i++; }else{ str = ""; while ((i<s.length ()&&((c=s.charAt ( i ))>=48&&(c=s.charAt ( i ))<=57||(c=s.charAt ( i ))=='.')) ){ str += c; i++; } ls.add ( str );
}
} while (i < s.length ( ));
return ls; }
public static List<String> turnSuffixExpression ( List<String> ls ) { Stack<String> s1 = new Stack<String> ( ); List<String> s2 = new ArrayList<String> ( );
for ( String i:ls ){ if ( i.matches ( "\\d+" )|| i.matches ( "\\d+.\\d+" )) { s2.add ( i ); } else if ( i.equals ( "(")) { s1.push ( i ); } else if (i.equals ( ")" )){ while (!s1.peek ( ).equals ( "(" )) { s2.add ( s1.pop ( ) ); } s1.pop (); }else { while (s1.size ()!=0&& Operation.getValue ( s1.peek () )>=Operation.getValue ( i)){ s2.add ( s1.pop ( ) ); } s1.push ( i );
} } while (s1.size ( ) != 0) { s2.add ( s1.pop() ); }
return s2; }
public static List<String> getListStrings ( String suffixEpression ) { String[] spilt=suffixEpression.split ( " " ); List<String> list = new ArrayList<String> ( ); for ( String element : spilt ) { list.add( element );
} return list; }
public static float calculate ( List<String> ls) { Stack<String> stack = new Stack<> ( ); for ( String s : ls ) { if ( s.matches ("\\d+" )||s.matches ( "\\d+.\\d+" ) ){ stack.push ( s ); }else { float num2=Double.valueOf ( stack.pop ( )).floatValue (); float num1=Double.valueOf ( stack.pop ( )).floatValue (); double res=0; if ( s.equals ( "+" ) ) { res = num1 + num2; } else if ( s.equals ( "-" ) ) { res = num1 - num2; } else if ( s.equals ( "*" ) ) { res = num1 * num2; } else if ( s.equals ( "/" ) ) { res = num1 / num2; }else { throw new RuntimeException ( "运算符完毕" ); } stack.push ( ""+res ); } } return Double.valueOf ( stack.pop ( ) ).floatValue (); }
}
class Operation{ private static int ADD=1; private static int SUB=1; private static int MUL=2; private static int DIV=2;
public static int getValue ( String operation ) { int result = 0; switch (operation) { case "+": result = ADD ; break; case "-": result = SUB ; break; case "*": result = MUL ; break; case "/": result = DIV ; break; default: System.out.println ("运算符有误" ); } return result; } }
|
结果