用栈实现计算器01

先将字符串保存在容易遍历的list中,可以处理多位数字

package a;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class TransDemo {
    public static void main(String[] args) {
        String suff = "1+((2+3)*4)-5";
        char c = suff.charAt(1);
        List<String> li = toList(suff);
        System.out.println("li = " + li);
    }

    private static List<String> toList(String suff) {
        ArrayList<String> res = new ArrayList<>();//创建res
        int i =0;//用于遍历
        String whole;//用来保存多位数字
        char c ;//用来保存每一个变量
        while (i < suff.length()) {
            if ((c = suff.charAt(i)) < 48 || (c = suff.charAt(i)) > 57) {//如果不是数
                i++;
                res.add(c + "");
            }else {//就是数
                whole ="";
                do{
                    whole+=c;
                    i++;
                }while (i<suff.length()&&((c = suff.charAt(i)) >= 48 && (c = suff.charAt(i)) <= 57));
                res.add(whole);
            }
        }
        return res;

    }
}

发布了66 篇原创文章 · 获赞 0 · 访问量 765

猜你喜欢

转载自blog.csdn.net/Be_With_I/article/details/104219849
今日推荐