[Java] String Calculator With Parentheses

import java.util.*;
import java.lang.String.*;

public class Calculator{
    private Stack<Double> numbers = new Stack<Double>();    //Stack numbers store the numbers to be calculated.
    private Stack<String> operators = new Stack<String>();  //Stack operators store the operators to be used.
    String number = ""; //Use the number string to temporarily store the number.

    //Compare the priority between two operators,return true if and only if the former is more weighted.(The same priority will return false.)
    public boolean prior(String former, String latter){
        if((former.equals("*")||former.equals("/")) && (latter.equals("+")||latter.equals("-")||latter.equals("(")))
            return true;
        else if((former.equals("+") || former.equals("-")) && latter.equals("("))
            return true;
        else
            return false;
    }
    //Calculate the two numbers.
    public double figureout(double former, String operator, double latter){
        if(operator.equals("+"))
            return former+latter;
        else if(operator.equals("-"))
            return former-latter;
        else if(operator.equals("*"))
            return former*latter;
        else if(operator.equals("/"))
            return former/latter;
        else return 0;
    }

    //identify the char.
    public int classifyOperator(String operator){
        if(operator.equals("0")||operator.equals("1")||operator.equals("2")||
           operator.equals("3")||operator.equals("4")||operator.equals("5")||
           operator.equals("6")||operator.equals("7")||operator.equals("8")||
           operator.equals("9")||operator.equals("."))
            return 0;
        if(operator.equals("+")||operator.equals("-")||operator.equals("*")||
           operator.equals("/"))
            return 1;
        if(operator.equals("("))
            return 2;
        if(operator.equals(")"))
            return 3;
        else
            return 4;
    }

    //Calculate the String.
    public double cal(String sentense){
        sentense = "("+sentense+")";    //Add parentheses ahead.
        for(int i = 0;i<sentense.length();i++){
            if(classifyOperator(""+sentense.charAt(i)) == 0){   //If  we meet a number character we just add it behind the number string.
                number = number+sentense.charAt(i);
            }
            else{
                if(!number.equals(""))
                    numbers.push(Double.parseDouble(number));   //Whenever it comes to character which isn't a number, parse the whole number string and push it to the stack.
                number = "";    //Reset the string to null every time we push the number to the stack.
                System.out.println("Now the numbers stack is"+numbers);
                if(classifyOperator(""+sentense.charAt(i)) == 2)    //Push the "(" to the stack directly.
                    operators.push(""+sentense.charAt(i));
                if(classifyOperator(""+sentense.charAt(i)) == 3){   //When it comes to ")".
                    while(!operators.peek().equals("(")){   //Do the math with all the operators stored in the operators stack until we meet "("
                        double latter = numbers.pop();
                        double former = numbers.pop();
                        numbers.push(figureout(former,operators.pop(),latter));
                    }
                    operators.pop();    //Pop the "(" left.
            }
                if(classifyOperator(""+sentense.charAt(i)) == 1){   //When it comes to +-*/

                    if(prior((""+sentense.charAt(i)),operators.peek()))     //If the present char has priority over the one at the top of the stack, just push it in.
                        operators.push(""+sentense.charAt(i));
                    else{                                                   //else, we repeatedly do the math until we find some operator in the operators stack that is less weighted.
                        double latter = numbers.pop();
                        double former = numbers.pop();
                        do{
                            numbers.push(figureout(former,operators.pop(),latter));
                        }while(!(prior((""+sentense.charAt(i)),operators.peek())));
                        operators.push(""+sentense.charAt(i));              //Push the current char into the stack.
                    }
                }
            }
            System.out.println("The number String is "+number);
            System.out.println("The numbers stack is "+numbers);
            System.out.println("The operators stack is"+operators);
            System.out.println("The charAt  is "+sentense.charAt(i));
            System.out.println("-----------");
        }
        while(!operators.empty()){                                      //In the end there may be some numbers left to be calculate.
            double latter = numbers.pop();
            double former = numbers.pop();
            numbers.push(figureout(former,operators.pop(),latter));
        }
        return numbers.pop();
    }


    public static void main(String[] args){
        String sentense = "((2+3)*7+2)*2+(5-7)*2";
        Calculator calculate = new Calculator();
        System.out.println(calculate.cal(sentense));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值