JAVA实现字符表达式的计算的实现(一)--逆波兰式的实现

写了2天,终于写出了个结果。

逆波兰式:可自行移步度娘http://baike.baidu.com/link?url=RWADssWBHn8SmI6A_DEuFXhwMLfCKtwG-RdP1PTIW1k5kFX0ZD4fI1_GRfXGMwy6FJwYPphMEDYyj2mCJzKaBa

程序如下:下面的程序中,有封装好的实现字符串转换成逆波兰式的方法。

package biaodashi;

public class nibolanshi {

	int stack2length = 0;

	// *****字符串与字符串数组的转换模块******
	public char[] stringTransferChar(String input) {

		char[] inputchar;
		inputchar = input.toCharArray();
		return inputchar;
	}

	public String[] getPoland(String input) {
		Stack stack1 = new Stack();
		Stack stack2 = new Stack();
		char[] inputchararray = this.stringTransferChar(input);

		stack1.push("#");
		Boolean pre = false;
		for (char chs : inputchararray) {
			// ***********************************************************HERE!!!**********
			if (chs >= '0' && chs <= '9') {
				if (pre) {
					int temp = Integer.valueOf(stack2.pop());
					stack2.push(String.valueOf(temp * 10 + Integer.parseInt(chs + "")));
					stack2length++;
				} else {
					stack2.push(String.valueOf(chs));
					stack2length++;
					pre = true;
				}
			}

			switch (chs) {
			case '(':
				stack1.push(String.valueOf(chs));
				pre = false;
				break;
			case ')':
				while (!(stack1.peek().equals("("))) {
					stack2.push(stack1.pop());
					stack2length++;
				}
				stack1.pop();
				pre = false;
				break;
			case '+':
			case '-':
				while (stack1.peek() != "#") {
					if (stack1.peek().equals("(")) {
						break;
					} else {
						stack2.push(stack1.pop());
						stack2length++;
					}
				}
				stack1.push(String.valueOf(chs));
				pre = false;
				break;
			case '*':
			case '/':
				while (!(stack1.peek().equals("-")) && !(stack1.peek().equals("+")) && !(stack1.peek().equals("#"))) {
					if (stack1.peek().equals("(")) {
						break;
					} else {
						stack2.push(stack1.pop());
						stack2length++;
					}
				}
				stack1.push(String.valueOf(chs));
				pre = false;
				break;

			}

		}
		while (!stack1.isEmpty() && stack1.top.data != "#") {
			stack2.push(stack1.pop());
			stack2length++;
		}
		String[] chararrfromstack2 = new String[stack2length-1];
		int i = 0;
		while (!stack2.isEmpty()) {
			chararrfromstack2[i] = stack2.pop();
			i++;
		}

		return chararrfromstack2;
	}
	
	public String getResUsePoland(String[] getcha){
		
		Stack stack = new Stack();
		int temx = 0;
		int temd = 0;
		int res = 0;
		String[] datas = new String[getcha.length];
		int k = getcha.length;
		System.out.println("*****"+k+"****");
		for(int i = 0;i<getcha.length;i++){
			datas[k-1] = getcha[i];
			k--;
		}
		System.out.println(datas[0]);
		for(int i = 0;i<datas.length;i++){
			if (this.isNum(datas[i])) {
				stack.push(datas[i]);
				
			}else {
				temx = Integer.valueOf(stack.pop());
				temd = Integer.valueOf(stack.pop());
				switch (datas[i]) {
				case "+":
					res = temx + temd;
					break;
				case "-":
					res = temd - temx;
					break;
				case "*":
					res = temd*temx;
					break;
				case "/":
					res = temd/temx;
					break;

				default:
					break;
				}
				stack.push(String.valueOf(res));
			}
		}
		return stack.pop();
	}
	
	public boolean isNum(String str){  
		  for (int i = str.length();--i>=0;){    
		   if (!Character.isDigit(str.charAt(i))){  
		    return false;  
		   }  
		  }  
		  return true;  
		}  

	public static void main(String[] args) {
		nibolanshi test = new nibolanshi();
		String input = "10-(7-3)*2";

		String[] getcha = test.getPoland(input);
		for (String ch : getcha) {
			System.out.print(ch);
		}
		System.out.println();
		String s = test.getResUsePoland(getcha);
		System.out.println("RES:"+s);
	}
}

class Stack {

	Node top = null;

	public Boolean isEmpty() {
		return top == null;
	}
	
	public int getLength(){
		
		
		return 0;
	}

	public void push(String data) {
		Node node = new Node(data);
		node.next = top;
		top = node;
	}

	public String peek() {
		if (top == null) {
			return null;
		}
		return top.data;
	}

	public String pop() {
		if (top == null) {
			return null;
		}
		Node tmp = top;
		top = top.next;
		return tmp.data;
	}

}

class Node {
	Node next;
	String data;

	public Node(String data) {
		this.data = data;
	}
}

从这两天写这个程序上得到的小总结:

1.字符串的比较有时候用!=不好使,可以用equal方法来解决问题。

2.各种基本数据类型之间的转换要熟悉了解,不然会让你很苦恼。 

3.模块化编程。

在此感谢:http://javapub.iteye.com/blog/666544

                  http://www.cnblogs.com/stay-foolish/archive/2012/04/25/2470590.html     这哥们思路解释的真不错。

                  http://blog.csdn.net/hudie1234567/article/details/5892175       借鉴了这哥们的某些细节,但这哥们的这篇博客里面的程序(正好是我借鉴的地方,即一位数到多位数的处理)貌似有问题。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值