zoukankan      html  css  js  c++  java
  • 算法训练 表达式计算

    问题描述
      输入一个只包含加减乖除和括号的合法表达式,求表达式的值。其中除表示整除。
    输入格式
      输入一行,包含一个表达式。
    输出格式
      输出这个表达式的值。
    样例输入
    1-2+3*(4-5)
    样例输出
    -4
    数据规模和约定
      表达式长度不超过100,表达式运算合法且运算过程都在int内进行。
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.Stack;  
    
        public class Main{   
            public static void main(String[] args){  
                Scanner input = new Scanner(System.in);
                String a = input.next();
                Stack<Integer> shu = new Stack<Integer>();
                Stack<Character> fu = new Stack<Character>();
                int index = 0;
                for(int i=0;i<a.length();i++){
                    char temp = a.charAt(i);
                    if(temp>='9'||temp>='0'){
                        index++;
                    }else{
                        if(index!=0){
                            shu.push(Integer.parseInt(a.substring(i-index, i)));
                            index = 0;
                        }
                        if(fu.isEmpty()==true){
                            fu.push(temp);
                        }else if(temp=='+'||temp=='-'){
                            while(fu.size()!=0&&fu.peek()!='('){
                                int b1 = shu.pop();
                                int a1 = shu.pop();
                                shu.push(jisuan(a1,b1,fu.peek()));
                                fu.pop();
                            }
                                fu.push(temp);
                                
                        }else if(temp=='*'||temp=='/'){
                            if(fu.peek()!='('){
                                while(fu.peek()=='*'||fu.peek()=='/'){
                                    int b1 = shu.pop();
                                    int a1 = shu.pop();
                                    shu.push(jisuan(a1,b1,fu.peek()));
                                    fu.pop();
                                    fu.push(temp);
                                }
                                fu.push(temp);
                            }
                            else{
                                fu.push(temp);
                            }
                        }
                        else if(temp=='('){
                            fu.push(temp);
                        }else if(temp==')'){
                            while(fu.peek()!='('){
                                if(fu.peek()=='*'||fu.peek()=='/'){
                                    int b1 = shu.pop();
                                    int a1 = shu.pop();
                                    shu.push(jisuan(a1,b1,fu.peek()));
                                    fu.pop();
                                    continue;
                                }
                                if(fu.peek()=='-'||fu.peek()=='+'){
                                    int b1 = shu.pop();
                                    int a1 = shu.pop();
                                    shu.push(jisuan(a1,b1,fu.peek()));
                                    fu.pop();
                                }
                            }
                            fu.pop();
                        }
                    } 
                
                }
                while(fu.size()!=0){
                    int b1 = shu.pop();
                    int a1 = shu.pop();
                    shu.push(jisuan(a1,b1,fu.pop()));
                }
                System.out.println(shu.pop());
            }
            public static int jisuan(int a,int b,char c){
                int result=0;
                if(c=='-'){
                    result = a-b;
                }
                if(c=='+'){
                    result = a+b;
                }
                if(c=='*'){
                    result = a*b;
                }
                if(c=='/'){
                    result = a/b;
                }
                return result;
            }
     }  
  • 相关阅读:
    tomcat7配置
    C# FTP常规方法
    C++ 用libcurl库进行http通讯网络编程
    webkit webApp 开发技术要点总结[转]
    websocket
    Linux下使用logrotate实现日志切换
    Linux日志文件utmp、wtmp、lastlog、messages
    妙用git rebase --onto指令
    iptables命令详解和举例
    linux下IPTABLES配置详解
  • 原文地址:https://www.cnblogs.com/lolybj/p/6512536.html
Copyright © 2011-2022 走看看