package com.java.ly2011.July;
/**
* 栈及 进制转换 、括号匹配、中序表达式变后序表达式
* @author Acer
*
*/
public class SomeStackRelated {
public static void main(String[] args) {
/*
//进制转换问题
//System.out.println(String.valueOf( (char)('A'+(11-10)) ));
ReInteger10toN//
System.out.println(ReInteger10toN(16, 65312));
Integer10toN//
Integer10toN(16, 65312);
System.out.println();
Integer10toN 2//
Integer10toN(16, 65312, new Stack());
System.out.println();
*/
/*
//括号匹配问题
System.out.println(BracketMath("()()()"));
System.out.println(BracketMath("())()"));
System.out.println(BracketMath("((()))"));
System.out.println(BracketMath("(()())"));
System.out.println(BracketMath("(()()"));
*/
//System.out.println(operatorPriority('*',')'));
//中序表达式变后序
System.out.println(formChange("(a+b)*(c+d)"));
}
private static String transfer(int num){
if(num<10)
return String.valueOf(num);
else
return String.valueOf( (char)('A'+(num-10)) );
}
/**
* 10进制到其他进制的转换
* @param N 为其他进制
* @param input
* @return
*/
public static String ReInteger10toN(int N ,int input){
if(input<=0)
return "";
else{
int a = input/N;
int b = input%N;
return ReInteger10toN(N, a) + transfer(b);
}
}
/**
* 10进制到其他进制的转换 手工使用栈
* @param N
* @param input
*/
public static void Integer10toN(int N ,int input){
Stack stack = new Stack(3);
int curent = input;
int a;
int b;
while(curent!=0){
a = curent/N;
b = curent%N;
stack.push(b);
curent = a;
}
while(!stack.isEmpty()){
System.out.print(transfer( (Integer)stack.pop() ) );
}
}
/**
* @param N
* @param input
* @param stack
*/
public static void Integer10toN(int N ,int input,Stack stack){
if(input<=0){
while(!stack.isEmpty()){
System.out.print(transfer( (Integer)stack.pop() ) );
}
}
else{
int a = input/N;
int b = input%N;
stack.push(b);
Integer10toN(N, a, stack);
}
}
/**
* 括号匹配 问题 每个右括号都有可以匹配的左括号,不能有多余的左括号或者右括号
* ()()() (()()) ((())) ()(()) (())() 这样都是匹配的
* (()())) ()()()( ())() 这样或者多个左括号,或者某个右括号没有可以匹配的左括号都是不匹配的
* @param brackInput
* @return
*/
public static boolean BracketMath(String brackInput){
boolean result = true;//初始默认为匹配
Stack stack = new Stack();
for(int i = 0 ;i < brackInput.length();i++){
if(brackInput.charAt(i)=='(')
stack.push(brackInput.charAt(i));
else if(brackInput.charAt(i)==')'){
if(stack.isEmpty()){
result = false;
break;
}
else{
stack.pop();
}
}
}
if(!stack.isEmpty())//如果栈不为空,则有剩余的左括号,也不算完全匹配
result = false;
return result;
}
/**
* 从中序式转变为后续式 (a+b)*(c+d) ---> ab+cd+*
*/
public static String formChange(String input){
StringBuffer result = new StringBuffer();
Stack stack = new Stack();
for(int i = 0 ; i<input.length();i++){
char loop = input.charAt(i);
if( (loop<=57&&loop>='0')|| (loop<='z'&&loop>='a') || (loop<='Z'&&loop>='A') ){
result.append(loop);
//System.out.print(loop+" ");
}
else{
if(loop=='('){
stack.push(loop);
}
else{
Object object = stack.getTopElement();
if(object==null){
stack.push(loop);
}
else if(loop==')'&&(Character)stack.getTopElement()=='('){
stack.pop();
}
else{
char stackTop = (Character)stack.getTopElement();
boolean flag = true;
int pri = operatorPriority(stackTop, loop);
while(pri==1){
result.append(stack.pop());
//System.out.print(stack.pop()+" ");
stackTop = (Character)stack.getTopElement();
if(loop==')'&stackTop=='('){
stack.pop();
flag = false;
break;
}
pri = operatorPriority(stackTop, loop);
}
if(flag)
stack.push(loop);
}
}
}
}
while(!stack.isEmpty()){
result.append(stack.pop());
}
return result.toString();
}
/**
* a高于b 返回1
* a小于等于 返回0
* @param a
* @param b
* @return
*/
private static int operatorPriority(char a , char b){
int result = 0;
if(a=='*'||a=='/'){
if(b!='*'&&b!='/'){
result=1;
}
else {
result=0;
}
}
if(a=='+'||a=='-'){
if(b=='('||b==')'){
result=1;
}
else {
result=0;
}
}
if(a=='('||a==')'){
result=0;
}
return result;
}
}
class Stack{
private Object[] elements ; //采用顺序表实现
private int top=0;//栈顶元素 从下到上指向第一个空闲的元素
public Stack(){
elements = new Object[10];
}
public Stack(int n){
elements = new Object[n];
}
public Object[] getElements() {
return elements;
}
public boolean isEmpty(){
if(top==0) return true; //栈顶指针如果为0就说明第一个空闲的元素是0号,即栈为空
else return false;
}
public boolean isFull(){
if(top==elements.length)
return true;
else
return false;
}
public int getElementNum(){
return top;
}
public Object getTopElement(){
if(top==0){
System.out.println("the stack is empty");
return null;
}else{
return elements[top-1];
}
}
public void push(Object e){
if(top==elements.length){
System.out.println("the stack is full, add new space");
Object[] oldElements =elements;
elements = new Object[elements.length+5];
System.arraycopy(oldElements, 0, elements, 0, oldElements.length);
}
elements[top]=e;
top++;
}
public Object pop(){
if(top==0){
System.out.println("the stack is empty");
return null;
}else{
top--;
try{
return elements[top];//将返回结果压入到调用栈中 ,并不是立即返回,而是继续向下执行finally语句块中的内容
}
finally{
elements[top]=null;
}
}
}
}