用栈来实现计算器功能运算

使用步骤

  • 1.将一个字符串算式先转换成后缀表达式
  • 2.把后缀表达式转换成结果
    结果
    相关的代码如下

import java.util.Scanner;
import java.util.Stack;

public class Task3_22 {

    /*
     * 中缀转后缀
     */
    public static void Suffix(String str){
        Stack<Character> s = new Stack<>();
        int position = 0;
        /*
        *我的自定义个ArrayList相关代码已在数据结构的MyArrayList中
        */
        MyArrayList<String> my = new MyArrayList<>();
        for(int i=0; i<str.length(); i++){
            char c = str.charAt(i);
            switch(c){
                case '+':
                case '-':
                    my.add(str.substring(position, i));
                    position = i+1;
                    while(!s.empty() && s.peek() != '('){
                        my.add(s.pop()+"");
                    }
                    s.push(c);
                    break;
                case '*':
                case '/':
                    my.add(str.substring(position, i));
                    position = i+1;
                    while(!s.empty() && s.peek() != '+' && s.peek() != '-' && s.peek() != '('){
                        my.add(s.pop()+"");
                    }
                    s.push(c);
                    break;
                case '(':
                    my.add(str.substring(position, i));
                    position = i+1;
                    s.push(c);
                    break;
                case ')':
                    my.add(str.substring(position, i));
                    position = i+1;
                    while(!s.empty() && s.peek() != '('){
                        my.add(s.pop()+"");
                    }
                    s.pop();
                    break;
                case '^':
                    my.add(str.substring(position, i));
                    position = i+1;
                    while(!s.empty() && s.peek() == '^' || s.peek() == '('){
                        my.add(s.pop()+"");
                    }
                    s.push(c);
                    break;
                case '=':
                    my.add(str.substring(position, i));
                    while(!s.empty()){
                        my.add(s.pop()+"");
                    }
                    break;
            }
        }
        for(int i=0; i<my.size(); i++){
            System.out.print(my.get(i));
        }
        System.out.println();
        evalPostfix(my);
    }

    /*
     * 后缀转结果
     */
    public static void evalPostfix(MyArrayList<String> my){
        Stack<Double> s = new Stack<Double>();
        Double a, b, result = 0.0;
        boolean isNumber;
        for(int i=0; i<my.size(); i++){
            try{
                isNumber = true;
                result = Double.parseDouble(my.get(i));
            }catch(Exception e){
                isNumber = false;
            }
            if(isNumber)
                s.push(result);
            else{
                switch(my.get(i)){
                    case "+":
                        a = s.pop();    b = s.pop();
                        s.push(b+a);    break;
                    case "-":
                        a = s.pop();    b = s.pop();
                        s.push(b-a);    break;
                    case "*":
                        a = s.pop();    b = s.pop();
                        s.push(b*a);    break;
                    case "/":
                        a = s.pop();    b = s.pop();
                        s.push(b/a);    break;
                    case "^":
                        a = s.pop();    b = s.pop();
                        s.push(Math.exp(a*Math.log(b)));
                        break;
                }
            }
        }
        System.out.println(s.peek());
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        Suffix(str);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值