数据结构06-完整逆波兰表达式实现计算器

目录

一、切分字符串

二、获得后缀表达式

三、计算逆波兰表达式


一、切分字符串

	public String[] splite(String str) {
		String[] arr = new String[str.length()];
		int index = 0;
		int index1 = 0;
		String str1 = "+-*/()";
		for (int i = 0; i < str.length(); i++) {
			String str2 = str.charAt(i) + "";
			if (str1.contains(str2)) {
				arr[index1] = str.substring(index, i);
				index1++;
				arr[index1] = str2;
				index1++;
				index = i + 1;
			}
			if (i == str.length() - 1) {
				arr[index1] = str.substring(index, i + 1);
			}
		}
		return arr;
	}

二、获得后缀表达式

	public Object[] get(String[] strs) {
		Stack<String> stack1 = new Stack<>();// 存数值
		Stack<String> stack2 = new Stack<>();// 存符号
		for (int i = 0; i < strs.length; i++) {
			String str4 = strs[i];
			if (i % 2 == 0) {// 数值
				if (strs[i] != null && !strs[i].equals("")) {
					stack1.push(strs[i]);
				}
			} else {// 符号
				if (strs[i] != null && !strs[i].equals("")) {
					// 如果stack2为空或者入栈的为(时,直接入栈
					if (stack2.isEmpty() || strs[i].equals("(")) {
						stack2.push(strs[i]);
					} else {
						// 否则定义一个死循环,直到满足条件退出
						while (true) {
							// 如果入栈的为)时,将stack2的符号出栈,并压入stack1中,直到遇到(并消除
							if (strs[i].equals(")")) {
								while (true) {
									if (stack2.peek().equals("(")) {
										stack2.pop();
										break;
									}
									stack1.push(stack2.pop());
								}
								break;
							}
							// 其他符号(+-*/)
							// 如果优先级比栈顶运算符优先级高("("优先级最低),也将符号压入s1中(栈顶元素)
							// 否则,将s1栈顶的运算符弹出并压入s2中,直到优先级比栈顶运算符优先级高
							else {
								String str2 = "+-";
								String str3 = "*/";
								// 如果入栈符号为"+-",将stack2的符号出栈,并压入stack1中,直到遇到(或stack2为空
								// 将符号入栈
								if (str2.contains(strs[i])) {
									while (true) {
										if (stack2.isEmpty()) {
											stack2.push(strs[i]);
											break;
										} else if (stack2.peek().equals("(")) {
											stack2.push(strs[i]);
											break;
										}
										stack1.push(stack2.pop());
									}
								}
								// 如果入栈符号为"*/",如果stack2栈顶元素为"*/",则将stack2的符号出栈,
								// 并压入stack1中
								else {
									while (true) {
										if (stack2.isEmpty()) {
											stack2.push(strs[i]);
											break;
										} else if (str3.contains(stack2.peek())) {
											stack1.push(stack2.pop());
										} else {
											stack2.push(strs[i]);
											break;
										}
									}
								}
								break;
							}
						}
					}

				}
			}
		}
		while (true) {
			if (stack2.isEmpty()) {
				break;
			}
			stack1.push(stack2.pop());
		}
		Object[] sObjects = stack1.toArray();
		return sObjects;
	}

三、计算逆波兰表达式

	public Object getResult(Object[] objects){
		Stack<Object> stack1 = new Stack<>();
		String str1 = "+-*/";
		for(Object object:objects){
			if(!str1.contains((String)object)){
				stack1.push(object);
			}else {
				if("+".equals(object)){
					double n1 = new Double((String)stack1.pop());
					double n2 = new Double((String)stack1.pop());
					double num = n1+n2;
					String str2 = num+""; 
					stack1.push(str2);
				}
				if("-".equals(object)){
					double n1 = new Double((String)stack1.pop());
					double n2 = new Double((String)stack1.pop());
					double num = n1-n2;
					String str2 = num+""; 
					stack1.push(str2);
				}
				if("*".equals(object)){
					double n1 = new Double((String)stack1.pop());
					double n2 = new Double((String)stack1.pop());
					double num = n1*n2;
					String str2 = num+""; 
					stack1.push(str2);
				}
				if("/".equals(object)){
					double n1 = new Double((String)stack1.pop());
					double n2 = new Double((String)stack1.pop());
					double num = n1/n2;
					String str2 = num+""; 
					stack1.push(str2);
				}
				
			}
		}
		Object object = stack1.peek();
		return object;
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值