字符串表达式求值(虽然代码是错误的,但是还是贴出了)

 

代码是错误的,NND!!,如果是高手,就改下吧。

 

 

用栈和后缀表达式来求解

 

栈 

 

package endual.iteye.com.main;

public class StackX {

	private char[] arrays = null ;
	private int    maxSize ;
	private int    top ;
	
	
	public StackX(int maxSize) {
		this.arrays = new char[maxSize] ;
		maxSize = maxSize;
		this.top = 0 ;
	}
	
	//默认是在正常的范围内
	public void push (char newChar) {
		
		this.arrays[top] = newChar ;
		this.top++ ;
		
	}
	
	//上面那个给弄出来
	public char pop () {
		
		char topChar = 'x' ;
		topChar = this.arrays[top-1] ;
		top-- ;
		return topChar  ;
	}
	
	//获取栈的个数
	public int getLength() {
		
		return this.top ;
	}
	
	
	//判断栈是否为空
	public boolean isFull() {
		
		if (this.getLength() == 0) {
			return false ;
		}
		
		return true ;
	}
	
	
}

 

 运行类:

 

    package endual.iteye.com.main;


public class MainTest {

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		test();
	}

	private static void test() {
		
		String _s = "3-(5+4)" ;
		char[] arrs = _s.toCharArray() ;
		
	
		int maxSize = arrs.length ;
	
		int nLabel = 0 ;
		for (int i=0; i<maxSize; i++) {
			
			if (isLabel(arrs[i])) {
				nLabel++ ;
			}
			
		}
		
		StackX stack = new StackX(nLabel) ;
		String _ans = "" ;
		for (int i=0; i<maxSize; i++) {
			
			char newChar = arrs[i] ; //读取字符
			//判断该字符不是运算符
			if (isLabel(newChar)) {
				//判断栈是否为空
				if (!stack.isFull() || isLabel(newChar) || isLeft(newChar)) { //如果站为空了
					stack.push(newChar) ;
				    
				} else { //如果站不为空,那么就跳出来一个一个字符
					
					if (stack.isFull() ) { //如果栈有东西
						
						if (isLeft(newChar)) { // 如果是左括号,加入到栈去
							stack.push(newChar) ;
							continue ; // 终止,下一轮
						}
						
						//判断该跳出来的字符是与当前的字符进行比较
						 if (isRight(newChar)) { //那么要弹出了
							 char topChar = stack.pop() ;
							 while (topChar == '(') {
								 _ans = _ans + topChar ;
								 topChar = stack.pop() ;
							 }
							 
							 continue ;
						}
						 
						 char topChar = stack.pop() ;
						//那么剩余的就是减号或者加号了
						 _ans = _ans + topChar ;
						 stack.push(newChar) ;
					}
					else { //如果站为空
						
					}
				}
			}
			else { //如果不是运算符号
				
				_ans = _ans + newChar ;
			}
		} // for 
		
		while (stack.isFull()) {
			
			char topChar = stack.pop() ;
			_ans = _ans + topChar ;
			
		}
		
		System.out.println(_ans);
		
	}
	
	public static boolean isHighClass(char newChar) {
		
		if ('/' == newChar) {
			return true ;
		}
		if ('*' == newChar) {
			return true ;
		}
		
		return false ;
	}
	
	public static boolean isLeft(char newChar) {
		
		if ('(' == newChar) {
			return true ;
		}
		
		return false ;
		
	}
	
   public static boolean isRight(char newChar) {
		
		if (')' == newChar) {
			return true ;
		}
		
		return false ;
		
	}
	
	
	//判断是否是运算符号
	public static boolean isLabel(char newChar) {
		
		
		if ('-' == newChar) {
			return true ;
		}
		if ('+' == newChar) {
			return true ;
		}
		if ('/' == newChar) {
			return true ;
		}
		if ('*' == newChar) {
			return true ;
		}
		if ('(' == newChar) {
			return true ;
		}
		if (')' == newChar) {
			return true ;
		}
		
		
		return false ; //不是运算符
		
		
		
	}
	

}
 

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值