栈的应用

栈的应用

后缀表达式

计算后缀表达式的基本思路是:当遇到一个数时就把它推入栈中;在遇到一个运算符时该运算符就作用于

从该栈弹出的两个数上,将所得结果推入栈中。例如,

常规中缀记法的“3 - 4 + 5”在逆波兰记法中写作“3 4 - 5 +”:先3减去4,再加上5

 

java 实现


package com.lemon.stack;

public class PostfixExpressionTest {

	public static void main(String[] args) {
		//String[] strs={"6","5","2","3"," +","8","*"," +","3"," +","*"};
		//String[] strs={"5","1","2","+","4","*","+","3","-"};
		//String[] strs={"1","2","3","*","+"};
		String[] strs={"1","10","-"};
		MyStack stack=new MyStack();
		for(int i=0;i<strs.length;i++){
			if(strs[i].matches("[0-9]+")){
				stack.push(strs[i]);
			}else if(stack.size()>=2){
				Object firstNum=  stack.pop();
				Object secendNum= stack.pop();
				Integer newNum=calculate(Integer.valueOf(secendNum.toString()),Integer.valueOf(firstNum.toString()),strs[i]);
				stack.push(newNum);
			}else{
				continue;
			}
		}
		while(!stack.isEmpty()){
			System.out.println(stack.pop());
		}


	}

	/**
	 * calculate addition, subtraction,multiplication,division of num1 and num2
	 * @param num1
	 * @param num2
	 * @param operator
	 * @return
	 */
	static Integer calculate(Integer num1,Integer num2,String operator){
		Integer result=new Integer(0);
		switch (operator.trim().charAt(0)) {
		case '+':
			result=num1+num2;
			break;
		case '-':
			result=num1-num2;
			break;
		case '*':
			result=num1*num2;
			break;
		case '/':
			result=num1/num2;
			break;
		default:
			break;
		}
		return result;
	}
}

栈的应用

一个典型应用就是十进制转换二进制,转换的基本思路是:采用"除2取余,逆序排列"法。具体做法是:用2整除十进制整数,可以得到一个商和余数;再用2去除商,又会得到一个商和余数,如此进行,直到商为0时为止,然后把先得到的余数作为二进制数的低位有效位,后得到的余数作为二进制数的高位有效位,依次排列起来。

栈的特点就是后进先出,结合十进制转换二进制的原理,我们可以很轻易的写出如下代码来完成转换工作:


package com.lemon.stack;

import java.security.InvalidParameterException;

public class BinarySystemConvert {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MyStack<Integer> stack=getBinarySystemNumber(56);
		while(!stack.isEmpty()){
			System.out.println(stack.pop());
		}
	}
	
	
	private static MyStack<Integer> getBinarySystemNumber(Integer num){
		MyStack<Integer> result=null;
		if(num==null){
			throw new InvalidParameterException();
		}
		result=new MyStack<Integer>();
		while(true){
			int quotient=num/2;
			int remainder=num%2;
			if(quotient==0){
				result.push(new Integer(1));
				break;
			}else{				
				num=quotient;
				result.push(new Integer(remainder));
			}
		}
		return result;
	}

}

运行结果如下:

栈的应用_第1张图片


计算两个非常大的数相加

例如求 522124198907131233890713与 522124199303241225930324之和。整型变量不能存放这么大的值,更不用说存放它们之和了。解决办法之一是:将这样的数看成一串数字,并将这些数字分别放进两个栈中,然后从栈中弹出元素,进行加法操作。伪代码如下:
Stack numStack1;
Stack numStack2;
Stack resultStack;
int carry=0;
while(至少一个栈不为空){
       从每一个非空的栈中弹出元素,并将二者相加,相加后的结果存为tempNum;
       if(tempNum<10){
          将个位数存放到resultStack中;
       }else{
}
       if(carry!=0){
          将进位存放到resultStack中;
       }
}
现在结果栈中的元素即为最终结果。


package com.lemon.stack;


import java.util.Stack;


public class BigNumber {


static Stack<Integer> num1Stack=new Stack<Integer>();
static Stack<Integer> num2Stack=new Stack<Integer>();
public static void main(String[] args) {
int[] num1={5,2,2,1,2,4,1,9,8,9,0,7,1,3,1,2,3,3,8,9,0,7,1,3};
int[] num2={5,2,2,1,2,4,1,9,9,3,0,3,2,4,1,2,2,5,9,3,0,3,2,4};
// int[] num1={5,9,2};
// int[] num2={3,7,8,4};
for(int i=0;i<num1.length;i++){
num1Stack.push(num1[i]);
System.out.print(num1Stack.peek());
}
System.out.println();
for(int i=0;i<num2.length;i++){
num2Stack.push(num2[i]);
System.out.print(num2Stack.peek());
}
System.out.println();
addingLargeNumbers();

}

static void addingLargeNumbers(){
Stack<Integer> resultStack=new Stack<Integer>();
int carry=0;
while(!num1Stack.isEmpty() || !num2Stack.isEmpty()){
int num=0;
int num1=0;
int num2=0;
if(!num1Stack.isEmpty()){
num1=num1Stack.pop();
}
if(!num2Stack.isEmpty()){
num2=num2Stack.pop();
}

num=num1+num2+carry;
if(num>=10){
//if(!num1Stack.isEmpty())num1Stack.pop();
//if(!num2Stack.isEmpty())num2Stack.pop();
resultStack.push(num-10);
carry=1;
}else{
resultStack.push(num);
carry=0;
}
/*if(carry!=0){
resultStack.push(carry);
}*/
}
while(!resultStack.isEmpty()){
System.out.print(resultStack.pop());
}
}


}

你可能感兴趣的:(计算,栈,应用,后缀表达式)