数据结构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 stack1 = new Stack<>();// 存数值
		Stack 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 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;
	} 
  

 

你可能感兴趣的:(数据结构06-完整逆波兰表达式实现计算器)