表达式字符串转化为后缀表达式格式

Java数据结构和算法中文第二版.pdf 代码
StackX.java
package com.ch4.infix;

public class StackX {

	private int maxSize ;
	private char[] stackArray ;
	private int top ;
	
	public StackX(int size){
		
		maxSize = size ;
		stackArray = new char[size] ;
		top = -1 ;
	}
	
	public void push(char elem){
		
		stackArray[++top] = elem ;
	}
	
	public char pop(){
		
		return stackArray[top--] ;
	}
	
	public char peek(){
		
		return stackArray[top] ;
	}
	
	public boolean isEmpty(){
		
		return (top == -1) ;
	}
	
	public char peekIndex(int index){
		
		return stackArray[index] ;
	}
	
	public int size(){
		
		return top +1 ;
	}
	public void displayStack(String info){
		
		System.out.print(info) ;
		System.out.print("Stack (bottom -->top): ") ;
		for (int j = 0; j < size(); j++){
			System.out.print(peekIndex(j)) ;
			System.out.print(' ');
		}
		System.out.println() ;
	}
}

Infix.java
package com.ch4.infix;


public class Infix {

	private StackX theStack;
	private String input;
	private String output = "";

	public Infix() {

	}

	public Infix(String in) {
		input = in;
		int stackSize = input.length();
		theStack = new StackX(stackSize);
	}

	public String doTrans() {

		for (int i = 0; i < input.length(); i++) {

			char ch = input.charAt(i);
			if (ch == ' '){
				continue ;
			}
			theStack.displayStack("For " + ch + " ");

			switch (ch) {
			case '+':
			case '-':
				gotOper(ch, 1);
				break;
			case '*':
			case '/':
				gotOper(ch, 2);
				break;
			case '(':
				theStack.push(ch);
				break;
			case ')':
				gotParent(ch);
				break;
			default:
				output += ch;
				break;
			}

		}
		while (!theStack.isEmpty()) {
			theStack.displayStack("While ");
			output += theStack.pop();
		}
		theStack.displayStack("End  ");
		return output;
	}

	public void gotOper(char opThis, int prec1) {

		while (!theStack.isEmpty()) {

			char opTop = theStack.pop();
			if (opTop == '(') {
				theStack.push(opTop);
				break;
			} else {
				int prec2 = 2;
				if (opTop == '+' || opTop == '-') {
					prec2 = 1;
				}

				if (prec2 < prec1) {
					theStack.push(opTop);
					break;
				} else {
					output += opTop;
				}
			} // end else
		} // end while

		theStack.push(opThis);
	}

	public void gotParent(char ch) {

		while (!theStack.isEmpty()) {

			char chx = theStack.pop();
			if (chx == '(') {
				break;
			} else {
				output += chx;
			}
		} // end while
	}
}

InfixApp.java

package com.ch4.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.ch4.infix.Infix;

public class InfixApp {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {

		String input , output ;
		
		while(true ){
			
			System.out.print("Enter infix: ") ;
			System.out.flush() ;
			input = getString() ;
			
			if (input.equals("")){
				break ;
			}
			Infix theTrans = new Infix(input) ;
			
			output = theTrans.doTrans() ;
			System.out.println("src String : " + input) ;
			System.out.println("Postfix is :" + output) ; ;
		}
	}
	
	public static String getString() throws IOException{
		
		InputStreamReader isr = new InputStreamReader(System.in) ;
		BufferedReader br = new BufferedReader(isr) ;
		String s = br.readLine() ;
		return s ;
		
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值