zoukankan      html  css  js  c++  java
  • 算术表达式栈与递归

     
    import java.util.Stack;
    public class Main {
    	public  Double Evaluate(String str) {
    		Stack<String> sk1 = new Stack<String>();
    		Stack<Double> sk2 = new Stack<Double>();
    		
    		for(int i = 0;i< str.length();i++) {
    			
    			String temp = str.charAt(i)+"";
    			
    			if(temp.equals("*")) sk1.push(temp);
    			//不要加东西进去 不然会计算后数据出错
    			else if(temp.equals("("))               ;
    			else if(temp.equals("-")) sk1.push(temp);
    			else if(temp.equals("+")) sk1.push(temp);
    			else if(temp.equals("/")) sk1.push(temp);
    			else if(temp.equals(")")) {
    				String x = sk1.pop();
    				double y = sk2.pop();
    				if(x.equals("+")) sk2.push(y + sk2.pop());
    				else if(x.equals("-"))	sk2.push(y - sk2.pop());
    				else if(x.equals("*"))	sk2.push(y * sk2.pop());
    				else if(x.equals("/"))	sk2.push(sk2.pop() / y);
    			}
    			else 
    			{
    				sk2.push(Double.parseDouble(temp));
    			}
    		}
    		return sk2.peek();
    	}
     
    	public static void main(String[] args) {
    		Main m = new Main();
    		String str ="(((1+2)*6)+4)";
    		System.out.println(m.Evaluate(str));
    	}
    }
    

    另外一种是采用递归的方式

    package leetcode;
     
    import java.util.Scanner;
     
    public class Main {
    	private static int i = 0;
    //递归的方式:如(2+2)*(1+2)+9/2 分解为(2+2)这个表达式 (1+2)这个表达式 + 9/2这个表达式
    	//然后每个表达式进行递归取值 然后把所有的值加起来 
    	private static int expression_value(String str) {
    		//result 的作用是将表达式的第一个因子求出来
    		//因为i++ 随着递归不断增加 所以判断后面是什么符号 所以一开始在运算式后面再家一个空格的作用就在这里
    		int result = term_value(str);
    		while (true) {
    			//后面如果是加号的话进行加减 不考虑乘除
    			char temp = str.charAt(i) ;
    			if (temp == '+' || temp == '-') {
    				i++;
    			//通过term_value 这个方法求解第二个因子
    				int value = term_value(str);
    				if (temp == '+')
    					result += value;
    				else
    					result -= value;
    			}
    			else
    				break;
    		}
    		return result;
    	}
    //求解因子的方法
    	private static int term_value(String str) {
    //调用了一个factor 这个方法 所有加减都要优先考虑乘除 更要先考虑括号
    		int result = factor_value(str);
    		while (true) {
    			char temp = str.charAt(i);
    			if (temp == '*' || temp == '/' ) {
    				i++;
    				//继续调用factor方法去后面的值是否右有括号 除掉括号进行乘除
    				int value = factor_value(str);
    				if (temp =='*')
    					result *= value;
    				else
    					result /= value;
    			} else
    				break;
    		}
    		return result;
    	}
     
    	private static int factor_value(String str) {
    		//进行括号内的运算
    		int result = 0;
    		char temp = str.charAt(i);
    		if (temp == '(') {
    			i++;
    			result = expression_value(str);
    			i++;
    		}
    		else {
    			while ( (temp - '0') >= 0 && (temp - '0') <= 9) {
    				result = 10 * result + temp - '0';
    				i++;
    				temp = str.charAt(i);
    			}
    		}
    		return result;
    	}
    //总结三个方法
    	//expression_value 方法是进行加减
    	//trem_value方法是进行乘除
    	//factor_value 方法是进行去括号 括号内又有表达式 表达式又有加减乘除 括号 所有继续调用
    	public static void main(String[] args) {
    		Scanner sc = new Scanner (System.in);
    		String s = sc.next()+" ";
    		System.out.println(expression_value(s));
    	}
    	}
     
    
  • 相关阅读:
    jquery.validate.unobtrusive的使用
    企业库判断数据库连接类型
    win10下安装LoadRunner12汉化包
    win10安装LoadRunner时,安装.net framwork组件报0x800F081F错误 解决办法
    wrk性能测试(详解)
    wrk压测报错 LF character expected at 1:111
    Linux scp命令详解(服务器之间复制文件或目录)
    Linux cat命令详解(连接文件并打印到标准输出设备上)
    性能测试流程
    ‘mysql’不是内部或外部命令,也不是可运行的程序--解决方法
  • 原文地址:https://www.cnblogs.com/cznczai/p/11149954.html
Copyright © 2011-2022 走看看