package com.service;
	
	 
	import java.util.Stack;  
	/** 
	 *
	 **/  
	public class CalculatorImpl implements Calculator{  
		
//		public static void main(String[] args) {
//			String s ="6X5-5X15/4+3/3+4";
//			double result = new CalculatorImpl().toReversePolishNotation(s);
//			System.out.println(result);
//		}
		
		/*转换逆波兰式*/
		public double toReversePolishNotation(String s) throws NumberFormatException{
			
			Stack<Character> s1 = new Stack<Character>();//
			Stack<Double>    s2 = new Stack<Double>();
			int       lastIndex = -1;
			char           temp;
			String          num;
			
			for(int i=0; i<s.length(); i++){
				System.out.println("第"+i+"趟");
				temp = s.charAt(i);
				//判断是否为操作数,如果是,就截取操作数
				if(Character.isDigit(temp) || temp == '.' || temp == '-'){
					//对出现的前面的负号进行处理
					if( temp == '-'){
						lastIndex = readDouble(s, i+1);
						num = s.substring(i, lastIndex);
						System.out.println("截取字符串" + num);
						s2.push(Double.parseDouble(num));
						System.out.println("s2 入栈" + s2.peek());
						System.out.println("s1 入栈  --> +" );
						s1.push('+');
					}else{
						lastIndex = readDouble(s, i);
						num = s.substring(i, lastIndex);
						System.out.println("截取字符串" + num);
						s2.push(Double.parseDouble(num));
						System.out.println("s2 入栈" + s2.peek());
					}
					i = lastIndex -1;
					System.out.println("当前i" +i);
				}else{
					//乘除时,直接做运算
					if(temp == 'X' || temp== '/' ){
						System.out.println("s2出栈:-->" +s2.peek());
						double num1 = s2.pop();
						lastIndex   = readDouble(s, i+1);
						double num2 = Double.parseDouble(s.substring(i+1, lastIndex));
						System.out.println("操作数:" +num2);
						
						i = lastIndex -1;
						System.out.println("当前i"+i);
						double result = calculate(num1, temp, num2);
						System.out.println("num1" +temp+" num2 =" + result);
						System.out.println("result 入栈 s2" + result);
						s2.push(result);
					//当为加减时,让他直接入栈,等待操作
					}else if(temp == '+' || temp == '-'){
						System.out.println("s1 入栈" + temp);
						s1.push(temp);
					}
				}
				
			}
			//对剩下的加减运算,得出最终结果
			while(!s1.isEmpty()&&!s2.isEmpty() && s2.size()>=2){
				
				System.out.println("s2 出栈:" +s2.peek());
				double num2 = s2.pop();
				System.out.println("s2 出栈:" +s2.peek());
				double num1 = s2.pop();
				System.out.println("s1 出栈:" +s1.peek());
				double result = calculate(num1 , s1.pop(), num2);
				System.out.println("计算结果为: " +result);
				s2.push(result);
			}
			
			return s2.pop();
		}
		
//		/*读取科学计算器操作符*/
//		public int readTecOP(String s, int index){
//			char c;
//			for(int i = 0; i<s.length(); i++){
//				
//			}
//			
//		}
		/*读取操作数*/
		public int readDouble(String s, int index){
			
			char c;
			for(int i = index;i<s.length(); i++){
				c = s.charAt(i);
				if(c == 'X'|| c == '/' || c == '+' || c == '-' ){
					System.out.println("return i ="+i);
					return i;
				}else if(i == s.length()-1){
					System.out.println(s.length()+"last");
					return s.length();
				}
			}
			return -1;
		}
		
		/*取栈元素操作计算*/
		public double calculate(double pre, char op, double after){
			
			double result = 0;
			switch(op){
			case '+':
				result = pre+after;
				System.out.println(pre+"+"+after);
				break;
			case '-':
				if(after<0){
					result = after - pre;
				}else{
					result = pre-after;
				}
				System.out.println(pre+"-"+after);
				break;
			case 'X':
				result = pre*after;
				System.out.println(pre+"X"+after);
				break;
			case '/':
				result = pre/after;
				System.out.println(pre+"/"+after);
				break;
			default:
				break;
			}
			return result;
		}
		
	}  
	
 
	
	
最近下载更多
芋妮奶茶 8小时前
暂无贡献等级
1053001914  LV1 2023年12月27日
mutouhh  LV5 2023年10月9日
ja12121  LV2 2023年6月17日
hhr1101  LV2 2022年12月12日
msq1126  LV1 2022年11月18日
Demo1111  LV30 2021年12月12日
Augusthao  LV1 2021年12月5日
123456dfy  LV2 2020年12月31日
VICTORYll  LV15 2020年12月10日
最近浏览更多
芋妮奶茶 8小时前
暂无贡献等级
1053001914  LV1 2023年12月27日
一起加油  LV4 2023年12月27日
clumsy191 2023年12月17日
暂无贡献等级
gk_xmj  LV1 2023年12月6日
1112WHQ  LV7 2023年11月3日
EFWAGGFAWGR 2023年10月19日
暂无贡献等级
四十四十  LV3 2023年10月18日
mutouhh  LV5 2023年10月9日
lmml2307831267  LV2 2023年6月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友