处理字符串的四则运算(2013小米笔试题)

输入:

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();
            }
        }
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值