当前位置 : 主页 > 编程语言 > java >

尝试着做一个比网上靠谱的真正能用的计算器

来源:互联网 收集:自由互联 发布时间:2021-06-28
带了io优化,可以直接跳过 import java.io.*;import java.util.*; public class Main{ static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new Buf
带了io优化,可以直接跳过
import java.io.*;
import java.util.*;
 
public class Main{
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
         
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
         
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
         
        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
    static class stack {
        char[] c = new char[105];
        int top = 0;
        char top() {
            return c[top];
        }
        char pop() {
            return c[top--];
        }
        void push(char x) {
            c[++top] = x;
        }
    }
    static int getRank(char c) {
        switch(c){
            case'+':return 1;
            case'-':return 1;
            case'*':return 2;
            case'/':return 2;
            case'^':return 3;
            default:return 0;
        }
    }
    static boolean isOp(char c) {
        if(
        c == '+'||
        c == '-'||
        c == '*'||
        c == '/'||
        c == '^') {
            return true;
        }else {
            return false;
        }
    }
    public static boolean isNum(String s)
    {
          
        try{
            Integer.parseInt(s);
            return true;
        }catch(NumberFormatException e){
            return false;
        }
    }
    public static boolean isOperator(Object o)
    {
        Character c = (Character)o;
        String s = "+-*/^";
        if(s.indexOf(c) != -1)
            return true;
        return false;
    }
    public static boolean isOperator(String s)
    {
        String operator = "+-*/^";
        if(operator.indexOf(s)!=-1)
            return true;
        return false;
    }
    public static void main(String args[]) {
        InputStream inputStream = System.in;
        InputReader in = new InputReader(System.in);
        int T = in.nextInt();
        while(T-->0) {
            String s = in.next();
            Stack
 
   exp = new Stack<>();
            stack op = new stack();
            op.push('n');
            Stack
  
    num = new Stack<>(); for(int i = 0; i < s.length();) { if (s.charAt(i)=='(') { op.push(s.charAt(i++)); } else if (s.charAt(i)==')') { while (op.top()!='(') { exp.push(String.valueOf(op.top())); //exp+=' '; op.pop(); } op.pop(); i++; } else if (isOp(s.charAt(i))) { while (getRank(op.top())>=getRank(s.charAt(i))){ exp.push(String.valueOf(op.top())); //exp+=' '; op.pop(); } op.push(s.charAt(i++)); } else { String n = ""; while (s.charAt(i)>='0' && s.charAt(i)<='9'&& i
   
    =s.length()) { break; } } exp.push(n); //exp+=' '; } } while (op.top()!='n'){ exp.push(String.valueOf(op.top())); //exp+=' '; op.pop(); } //System.out.println(exp); int num1,num2; int temp = 0; for(String ne : exp) { if(isNum(ne)) { num.push(Integer.parseInt(ne)); }else if(isOperator(ne)) { num1 = num.pop(); num2 = num.pop(); switch(ne) { case "+": temp = num2+num1;break; case "-": temp = num2-num1;break; case "*": temp = num2*num1;break; case "/": temp = num2/num1;break; case "^": temp = (int) Math.pow(num2, num1);break; } num.push(temp); } } System.out.println(num.pop()); } } }
   
  
 
网友评论