1700474216
1700474217
HashMap<String,Integer>var=getValue(expStr);
1700474218
1700474219
Calculator cal=new Calculator(expStr);
1700474220
1700474221
System.out.println(“运算结果为:”+expStr+”=”+cal.run(var));
1700474222
1700474223
}
1700474224
1700474225
//获得表达式
1700474226
1700474227
public static String getExpStr()throws IOException{
1700474228
1700474229
System.out.print(“请输入表达式:”);
1700474230
1700474231
return(new BufferedReader(new InputStreamReader(System.in))).readLine();
1700474232
1700474233
}
1700474234
1700474235
//获得值映射
1700474236
1700474237
public static HashMap<String,Integer>getValue(String exprStr)throws
1700474238
1700474239
IOException{
1700474240
1700474241
HashMap<String,Integer>map=new HashMap<String,Integer>();
1700474242
1700474243
//解析有几个参数要传递
1700474244
1700474245
for(char ch:exprStr.toCharArray()){
1700474246
1700474247
if(ch!=’+’ && ch!=’-‘){
1700474248
1700474249
//解决重复参数的问题
1700474250
1700474251
if(!map.containsKey(String.valueOf(ch))){
1700474252
1700474253
String in=(new BufferedReader(new InputStreamReader
1700474254
1700474255
(System.in))).readLine();
1700474256
1700474257
map.put(String.valueOf(ch),Integer.valueOf(in));
1700474258
1700474259
}
1700474260
1700474261
}
1700474262
1700474263
}
1700474264
1700474265
return map;
[
上一页 ]
[ :1.700474216e+09 ]
[
下一页 ]