处理字符串的四则运算(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();            }        }    }}



原创粉丝点击