处理字符串的四则运算(2013小米笔试题)
来源:互联网 发布:淘宝网一次性丁腈手套 编辑:程序博客网 时间:2024/06/06 07:22
输入:
2+3*(4-5)
1+1
输出:
-1.0
2.0
类似于四则运算的实现(+ - * / ^)
数据结构:
Stack<String> opr 字符栈
Stack<Double> num 数字栈
HashMap<String,Integer> map 优先级
算法:
1.先用map给运算符定义优先级
[ +=1 , -=1 , *=2 , /=2 , ^=3 , (=10 , )=0 ]
2.从左到右依次录入字符ch
(1)当字符为数字时,push到num栈
(2)如果为 ")" ,pop opr栈中的一个符号和 num栈中的2个数,做运算,直至opr中top出 "("
(3)如果该符号优先级大于opr栈顶元素的优先级,直接push到opr
(4)如果小于等于opr栈顶元素优先级或者栈顶元素为"(",则做类似(2)的运算直到不满足(4)的条件
3.全部录完之后,从opr栈顶依次做运算,直到栈元素全部出栈,此时num栈顶元素为所求值
代码:
package test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Stack;public class demo4 { Stack<String> opr = new Stack<String>(); Stack<Double> num = new Stack<Double>(); HashMap<String, Integer> map = new HashMap<String, Integer>(); public demo4() { map.put("+", 1); map.put("-", 1); map.put("*", 2); map.put("/", 2); map.put("^", 3); map.put("(", 10); map.put(")", 0); } public static void main(String[] args) { demo4 temp = new demo4(); temp.calc(); } public double calcCompute(String op, double num1, double num2) {//四则计算 switch (op) { case "+": return num1 + num2; case "-": return num1 - num2; case "*": return num1 * num2; case "/": return num1 / num2; case "^": return Math.pow(num1, num2); } return 0.0; } public void calcHelper() {//取栈顶元素计算 String op = opr.pop(); double num1 = num.pop(); double num2 = num.pop(); num.push(calcCompute(op, num2, num1)); } public boolean checkNum(char ch) {//检查是否为数字 boolean bool = false; if (Character.isDigit(ch)) { bool = true; } return bool; } public void calc() { String str = new String(); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); try { str = in.readLine(); } catch (IOException e) { e.printStackTrace(); } while (str != null) { opr.clear(); num.clear(); int len = str.length(); opr.push("("); num.push(0.0); for (int i = 0; i < len; i++) { char ch = str.charAt(i); String c = ch + ""; double db = 0.0; if (checkNum(ch)) {//将连续的数字拼接一起再push while (checkNum(ch)) { db = db * 10 + Integer.valueOf(ch + ""); i++; if (i < len) { ch = str.charAt(i); continue; } else { break; } } num.push(db); i--; continue; } if (c.equals(")")) { while (!opr.peek().equals("(")) { calcHelper(); } opr.pop(); } else if (map.get(c) > map.get(opr.peek())) { opr.push(c); } else { while (!"(".equals(opr.peek()) && map.get(c) <= map.get(opr.peek())) { calcHelper(); } opr.push(c); } } while (opr.size() > 1) { calcHelper(); } System.out.println(num.pop()); str = null; try { str = in.readLine(); } catch (IOException e) { e.printStackTrace(); } } }}
- 处理字符串的四则运算(2013小米笔试题)
- 2013小米笔试题
- (小米笔试)打乱字符串还原
- 一道小米的笔试题
- 小米笔试:验证输入的表达式是不是正确的四则运算,不考虑负数
- 笔试题--字符串处理
- 小米笔试题-小米Git
- 整数数组的奇偶数分开(小米笔试题)
- 笔试题--字符串处理(1)
- 笔试题--字符串处理(2)
- 笔试题--字符串处理(3)
- 【小米笔试题】实现一个算法,确定一个字符串的所有字符是否全部不同
- 小米公司2013校园招聘笔试题(研发)
- 2013年小米校园招聘笔试题(三)
- 小米2013校园招聘笔试最后一题(朋友圈)
- 小米公司2013校园招聘笔试题(研发)+答案
- 小米公司2013校园招聘笔试题(研发)
- 小米公司2013校园招聘笔试题(研发)+答案
- clojure
- 排序算法分析
- 怎么设置电脑锁屏
- 算法收集系列
- 2013.10.18
- 处理字符串的四则运算(2013小米笔试题)
- Java线程初步1
- windows真机+Linux虚拟机 共享上网
- linux内核开发之并发控制(二)
- 菜鸟求职记6
- vc6.0 打开多个工程的方法
- 贪吃蛇
- Linux多线程开发(二):使用C++封装线程同步操作
- 结构之法 算法之道 的博客,基本上都是知名企业的面试笔试题目