⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listexpression.java

📁 Calculator:可以计算各种基础表达式的计算器,比如3x+5x^2(x=3)
💻 JAVA
字号:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Calculator;

/**
 *
 * @author asus
 */
/*
 ******************PRIVATE MOTHOD*********************
	Vector list2Exp(StringTokenizer token) -> recursion mothod use exp2Preorder
											  to convert the string expression

	Vector exp2Preorder(Vector vc)		   -> preOrder the mininum part
 *****************************************************
 */
import java.util.Vector;
import java.util.StringTokenizer;

public class ListExpression {
	private StringTokenizer token;
	//new a token, then we can depart the operation and varible
	public ListExpression(String list){
		token = new StringTokenizer("("+list+")", " ()+-*/" , true);
	}
	@SuppressWarnings("unchecked")
	public Vector list2Preorder(){
		return this.list2Exp(token);
	}
	/*token the String into the Vector, use recurrence to put all the
	 * small-part-vector into a big vector and change them into preorder*/
	@SuppressWarnings("unchecked")
	private Vector list2Exp(StringTokenizer token){
		String temp ="";
		Vector vc = new Vector();
		while ( token.hasMoreTokens() ){
			temp = token.nextToken();
			if (temp.equals(" "))  continue;
			if ((temp.equals("(")))	vc.add(exp2Preorder(list2Exp(token)));
			else if(temp.equals(")")) return vc;
			else vc.add(temp);
		}
		return vc;

	}
	//function to change inOrder Vector to preOrder Vector
    @SuppressWarnings("unchecked")
	private Vector exp2Preorder(Vector vc)
	{
		int sizeOfList = vc.size();
		int curOp = 1;
		int insertIndex = 0;
		 for ( int i = 0;i < sizeOfList ;i++ ){
	        if (Operation.isBiOperator(vc.get(i))){
	        	if (Operation.priority(vc.get(i))==1){
	        		vc.add(0,vc.get(i));vc.remove(i+1);
	        		curOp = 1;
	        	}
	        	else if (Operation.priority(vc.get(i))==2 && curOp==1){
	        		vc.add(i-1,vc.get(i));vc.remove(i+1);
	        		insertIndex = i-1;
	        		curOp = 2;
	        	}
	        	else if (Operation.priority(vc.get(i))==2 && curOp==2){
	        		vc.add(insertIndex,vc.get(i));vc.remove(i+1);
	        	}
	        }

		 }
		return vc;

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -