{
//接受一个表达式参数的构造函数
public ExpressionAnalyze(String expr) throws Exception{
this.expr = expr;
numOfParameter = 0;
try{
clearBlank();
seekOutElem();
compute();
}
catch (Exception e){
throw new Exception(e.toString());
}
}
//接受一个表达式参数、一个传值参数的构造函数
public ExpressionAnalyze(String expr,double value1) throws Exception{
this.expr = expr;
numOfParameter = 1;
this.value1 = value1;
try{
clearBlank();
seekOutElem();
compute();
}
catch (Exception e){
throw new Exception(e.toString());
}
}
//接受一个表达式参数、两个传值参数的构造函数
public ExpressionAnalyze(String expr,double value1,double value2)
throws Exception
{
this.expr = expr;
numOfParameter = 2;
this.value1 = value1;
this.value2 = value2;
try{
clearBlank();
seekOutElem();
compute();
}
catch (Exception e){
throw new Exception(e.toString());
}
}
//接受一个表达式参数、三个传值参数的构造函数
public ExpressionAnalyze(String expr,double value1,double value2,double value3)
throws Exception
{
this.expr = expr;
numOfParameter = 3;
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
try{
clearBlank();
seekOutElem();
compute();
}
catch (Exception e){
throw new Exception(e.toString());
}
}
//清除expr中的空格
private void clearBlank() throws Exception{
//清除expr末尾的空格
expr = expr.trim();
char [] exprArray = new char[expr.length()];
//将expr中的元素复制到数组exprArray中
for(int i = expr.length()-1;i >= 0;i--)
exprArray[i] = expr.charAt(i);
//逐个将空格清除
for(int i = expr.length()-1;i >= 0;i--){
int j;
if(exprArray[i] ==' '){
j = i;
while(j < exprArray.length - 1){
exprArray[j] = exprArray[j + 1];
j++;
}
exprArray[exprArray.length - 1] = ' ';
}
}
//将数组形式转换成StringBuffer形式
StringBuffer exprStrBuf = new StringBuffer("");
for(int i = 0;i < exprArray.length;i++){
exprStrBuf.insert(i,exprArray[i]);
}
//将StringBuffer形式转换成String形式
expr = exprStrBuf.toString().trim();
if(expr.length() == 0)
throw new Exception("\nthe length of the expression is 0");
}
//将字符串中的变量、常量、运算符挑出
private void seekOutElem() throws Exception{
int minLocation;
int startPoint = 0;
boolean isKeyword;
do{
minLocation = expr.length();
isKeyword = false;
for(int i = 0;i < keywords.length;i++) {
int kwdLocation = expr.substring(startPoint).indexOf(keywords[i]);
if(kwdLocation == -1)
kwdLocation = expr.length();
else
kwdLocation += startPoint;
//如果是运算符
if(kwdLocation == startPoint &&
(keywords[i].equals("(") ||
keywords[i].length() == 1 ||
expr.charAt(startPoint + keywords[i].length()) == '('
)
)
{
//如果链表为空
if(elemList == null){
elemList = new ElemList (keywords[i]);
current = elemList;
current.isOperator = true;
}
//如果链表不空
else{
current.next = new ElemList (keywords[i]);
current = current.next;
current.isOperator = true;
}
isKeyword = true;
break;
}
if(minLocation > kwdLocation)
minLocation = kwdLocation;
}
//如果不是运算符
if(!isKeyword){
//如果链表为空
if(elemList == null){
elemList = new ElemList (expr.substring(startPoint,minLocation));
current = elemList;
current.isOperator = false;
}
//如果链表不空
else{
current.next = new ElemList (expr.substring(startPoint,minLocation));
current = current.next;
current.isOperator = false;
}
}
startPoint += current.data.length();
}while(startPoint < expr.length());
//公式末尾添加"#"
current.next = new ElemList ("#");
current = current.next;
current.isOperator = true;
}
//计算最终的结果
private void compute() throws Exception{
//处理公式开头的负号
if(elemList.data.equals("-")){
ElemList temp = new ElemList ("0");
temp.next = elemList;
elemList = temp;
}
for(current = elemList;current.next != null;current = current.next){
//处理负号
if(current.data.equals("(") && current.next.data.equals("-")){
ElemList temp = new ElemList ("0");
temp.next = current.next;
current.next = temp;
}
//处理常量自然对数的底 e
else if(current.data.equals("E"))
current.data = (new Double(Math.E)).toString();
//处理常量圆周率
else if(current.data.equals("PI"))
current.data = (new Double(Math.PI)).toString();
}
//找出公式中的变量,并对其恰当的赋值
boolean findFirst = false , findSecond = false,findThird = false;
switch(numOfParameter){
case 0 :
for(current = elemList;current != null;current = current.next)
if(!current.isOperator){
//强制类型转换,若转换不成功则说明是变量
try{
Double.parseDouble(current.data);
}
catch (NumberFormatException e){
throw new Exception ("\nthe variable '" + current.data
+ "' needs a value");
}
}
break;
case 1 :
for(current = elemList;current != null;current = current.next)
if(!current.isOperator){
//强制类型转换,若转换不成功则说明是变量
try{
Double.parseDouble(current.data);
}
catch (NumberFormatException e){
findFirst = true;
firstStr = new String(current.data);
firstDou = value1;
break;
}
}
if(!findFirst)
throw new Exception ("\nthere is no variable,the value '" + value1
+"' is not needed");
break;
case 2 :
for(current = elemList;current != null;current = current.next)
if(!current.isOperator){
//强制类型转换,若转换不成功则说明是变量
try{
Double.parseDouble(current.data);
}
catch (NumberFormatException e){
findFirst = true;
firstStr = new String(current.data);
firstDou = value1;
break;
}
}
if(!findFirst)
throw new Exception ("\nthere is no variable,the value '" + value1
+"' is not needed");
for(;current != null;current = current.next)
if(!current.isOperator && !current.data.equals(firstStr)){
//强制类型转换,若转换不成功则说明是变量
try{
Double.parseDouble(current.data);
}
catch (NumberFormatException e){
findSecond = true;
secondStr = new String(current.data);
secondDou = value2;
break;
}
}
if(!findSecond)
throw new Exception ("\nthere is not so much variables,the value '"
+ value2 + "' is not needed");
break;
case 3 :
for(current = elemList;current != null;current = current.next)
if(!current.isOperator){
//强制类型转换,若转换不成功则说明是变量
try{
Double.parseDouble(current.data);
}
catch (NumberFormatException e){
findFirst = true;
firstStr = new String(current.data);
firstDou = value1;
break;
}
}
if(!findFirst)
throw new Exception ("\nthere is no variable,the value '" + value1
+"' is not needed");
for(;current != null;current = current.next)
if(!current.isOperator && !current.data.equals(firstStr)){
//强制类型转换,若转换不成功则说明是变量
try{
Double.parseDouble(current.data);
}
catch (NumberFormatException e){
findSecond = true;
secondStr = new String(current.data);
secondDou = value2;
break;
}
}
if(!findSecond)
throw new Exception ("\nthere is not so much variables,the value '"
+ value2 + "' is not needed");
for(;current != null;current = current.next)
if(!current.isOperator && !current.data.equals(firstStr)
&& !current.data.equals(secondStr)){
//强制类型转换,若转换不成功则说明是变量
try{
Double.parseDouble(current.data);
}
catch (NumberFormatException e){
findThird = true;
thirdStr = new String(current.data);
thirdDou = value3;
break;
}
}
if(!findThird)
throw new Exception ("\nthere is not so much variables,the value '"
+ value3 + "' is not needed");
}
//以下是表达式求值的算符优先算法。公式以"#"做结束符。
//设optr和opnd分别为运算符栈和操作数栈
Stack optr = new Stack(); //存储操作符的栈
Stack opnd = new Stack(); //存储操作数的栈
optr.push("#");
current = elemList;
while(!(current.data.equals("#") && optr.top().equals("#"))){
//如果不是运算符,则直接入操作数栈
if(!current.isOperator){
opnd.push(current.data);
current = current.next;
}
else{
int indexOfFirst = 0,indexOfLast = 0;
boolean flagOfFirst = false,flagOfLast = false;
for(int i = 0;i < keywords.length;i++){
if(optr.top().equals(keywords[i])){
indexOfLast = i;
flagOfLast = true;
}
if(current.data.equals(keywords[i])){
indexOfFirst = i;
flagOfFirst = true;
}
if(flagOfLast && flagOfFirst)
break;
}
if(!flagOfLast)
throw new Exception ("\nthe operator '" + optr.top()
+ "' is not supported");
if(!flagOfFirst)
throw new Exception ("\nthe operator '" + current.data
+ "' is not supported");
flagOfLast = false;
flagOfFirst = false;
switch(PRI[indexOfLast][indexOfFirst]){
case '<' ://栈顶元素优先级低
optr.push(current.data);
current = current.next;
break;
case '=' ://脱括弧、计算单目运算并接受下一个字符串
optr.pop();
current = current.next;
boolean isOptr = false;
for(int i = 0;i < keywords.length;i++)
if(optr.top().equals(keywords[i])){
if(keywords[i].length() > 1)
isOptr = true;
break;
}
if(!isOptr)
break;
isOptr = false;
//强制类型转换,若转换不成功则说明是变量
try {
firstDouTemp = Double.parseDouble(opnd.top());
}
catch (NumberFormatException e){
if(opnd.top().equals(firstStr))
firstDouTemp = firstDou;
else if(opnd.top().equals(secondStr))
firstDouTemp = secondDou;
else if(opnd.top().equals(thirdStr))
firstDouTemp = thirdDou;
else
throw new Exception ("\nthe value of '" + opnd.top()
+ "' is not found");
}
opnd.pop();
if(optr.top().equals("abs"))
opnd.push(new Double(Math.abs(firstDouTemp)).toString());
else if(optr.top().equals("acos")){
if(Math.abs(firstDouTemp) > 1)
throw new Exception("\nthe absolute value of the argument '"
+ firstDouTemp + "' that 'acos' takes is greater than 1");
opnd.push(new Double(Math.acos(firstDouTemp)).toString());
}
else if(optr.top().equals("asin")){
if(Math.abs(firstDouTemp) > 1)
throw new Exception("\nthe absolute value of the argument '"
+ firstDouTemp + "' that 'asin' takes is greater than 1");
opnd.push(new Double(Math.asin(firstDouTemp)).toString());
}
else if(optr.top().equals("atan"))
opnd.push(new Double(Math.atan(firstDouTemp)).toString());
else if(optr.top().equals("cbrt"))
opnd.push(new Double(Math.cbrt(firstDouTemp)).toString());
else if(optr.top().equals("cos"))
opnd.push(new Double(Math.cos(firstDouTemp)).toString());
else if(optr.top().equals("cosh"))
opnd.push(new Double(Math.cosh(firstDouTemp)).toString());
else if(optr.top().equals("ceil"))
opnd.push(new Double(Math.ceil(firstDouTemp)).toString());
else if(optr.top().equals("exp"))
opnd.push(new Double(Math.exp(firstDouTemp)).toString());
else if(optr.top().equals("expm1"))
opnd.push(new Double(Math.expm1(firstDouTemp)).toString());
else if(optr.top().equals("floor"))
opnd.push(new Double(Math.floor(firstDouTemp)).toString());
else if(optr.top().equals("log")){
if(firstDouTemp < 0)
throw new Exception("\nthe argument '" + firstDouTemp
+ "' that 'log' takes is less than zero");
opnd.push(new Double(Math.log(firstDouTemp)).toString());
}
else if(optr.top().equals("log10")){
if(firstDouTemp < 0)
throw new Exception("\nthe argument '" + firstDouTemp
+ "' that 'log10' takes is less than zero");
opnd.push(new Double(Math.log10(firstDouTemp)).toString());
}
else if(optr.top().equals("log1p")){
if(firstDouTemp < -1)
throw new Exception("\nthe argument '" + firstDouTemp
+ "' that 'log1p' takes is less than -1");
opnd.push(new Double(Math.log1p(firstDouTemp)).toString());
}
else if(optr.top().equals("rint"))
opnd.push(new Double(Math.rint(firstDouTemp)).toString());
else if(optr.top().equals("round"))
opnd.push(new Double(Math.round(firstDouTemp)).toString());
else if(optr.top().equals("signum"))
opnd.push(new Double(Math.signum(firstDouTemp)).toString());
else if(optr.top().equals("sin"))
opnd.push(new Double(Math.sin(firstDouTemp)).toString());
else if(optr.top().equals("sinh"))
opnd.push(new Double(Math.sinh(firstDouTemp)).toString());
else if(optr.top().equals("sqrt")){
if(firstDouTemp < 0)
throw new Exception("\nthe argument '" + firstDouTemp
+ "' that 'sqrt' takes is less than zero");
opnd.push(new Double(Math.sqrt(firstDouTemp)).toString());
}
else if(optr.top().equals("tan"))
opnd.push(new Double(Math.tan(firstDouTemp)).toString());
else if(optr.top().equals("tanh"))
opnd.push(new Double(Math.tanh(firstDouTemp)).toString());
else if(optr.top().equals("toDegrees"))
opnd.push(new Double(Math.toDegrees(firstDouTemp)).toString());
else if(optr.top().equals("toRadians"))
opnd.push(new Double(Math.toRadians(firstDouTemp)).toString());
else
throw new Exception ("\nthe operator '" + optr.top()
+ "' is not supported");
optr.pop();
break;
case '@' ://表达式输入有误
throw new Exception ("\nthe operators '" + keywords[indexOfLast]
+ "' and '" + keywords[indexOfFirst]
+ "' are not matched");
case '>' ://站定元素优先级高、计算双目运算
//强制类型转换,若转换不成功则说明是变量
try {
secondDouTemp = Double.parseDouble(opnd.top());
}
catch (NumberFormatException e){
if(opnd.top().equals(firstStr))
secondDouTemp = firstDou;
else if(opnd.top().equals(secondStr))
secondDouTemp = secondDou;
else if(opnd.top().equals(thirdStr))
firstDouTemp = thirdDou;
else
throw new Exception ("\nthe value of '" + opnd.top()
+ "' is not found");
}
opnd.pop();
//强制类型转换,若转换不成功则说明是变量
try {
firstDouTemp = Double.parseDouble(opnd.top());
}
catch (NumberFormatException e){
if(opnd.top().equals(firstStr))
firstDouTemp = firstDou;
else if(opnd.top().equals(secondStr))
firstDouTemp = secondDou;
else if(opnd.top().equals(thirdStr))
firstDouTemp = thirdDou;
else
throw new Exception ("\nthe value of '" + opnd.top()
+ "' is not found");
}
opnd.pop();
if(optr.top().equals("+")){
opnd.push(new Double(firstDouTemp + secondDouTemp).toString());
}
else if(optr.top().equals("-")){
opnd.push(new Double(firstDouTemp - secondDouTemp).toString());
}
else if(optr.top().equals("*")){
opnd.push(new Double(firstDouTemp * secondDouTemp).toString());
}
else if(optr.top().equals("/")){
if(secondDouTemp == 0)
throw new Exception ("\nthe second argument that '/' takes is 0");
opnd.push(new Double(firstDouTemp / secondDouTemp).toString());
}
else if(optr.top().equals("%")){
if(secondDouTemp == 0)
throw new Exception ("\nthe second argument that '%' takes is 0");
opnd.push(new Double(firstDouTemp % secondDouTemp).toString());
}
else
throw new Exception ("\nthe operator '" + optr.top()
+ "' is not supported");
optr.pop();
break;
}
}
}
result = Double.parseDouble(opnd.pop());
}
public Double getResult(){
return result;
}
private String firstStr;//存储公式中第一个变量的表达式
private String secondStr;//存储公式中第二个变量的表达式
private String thirdStr;//存储公式中第三个变量的表达式
private double firstDou;//存储公式中第一个变量的值
private double secondDou;//存储公式中第二个变量的值
private double thirdDou;//存储公式中第三个变量的值
private double value1;//从构造函数接收过来的第一个变量得值
private double value2;//从构造函数接收过来的第二个变量得值
private double value3;//从构造函数接收过来的第三个变量得值
private double firstDouTemp;//存储每次计算的第一个临时变量
private double secondDouTemp;//存储每次计算的第二个变量
private ElemList elemList = null;//链表头
private ElemList current = null;//链表当前指针
private int numOfParameter; //公式中变量的个数
private String expr;//存储公式
private double result;//最终的计算结果
//运算符
private static final String [] keywords =
{
"+","-","*","/","(",")","%","abs","acos","asin","atan","cbrt","ceil",
"cos","cosh","exp","expm1","floor","log","log10","log1p","rint","round",
"signum","sin","sinh","sqrt","tan","tanh","toDegrees","toRadians","#"
};
//运算符优先级
private static final char [] [] PRI =
{ // + - * / ( ) % abs acos asin atan cbrt ceil cos cosh exp expm1 floor log log10 log1p rint round signum sin sigh sprt tan tanh toDegrees toRadians #
/**//* + */{'>','>','<','<','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<', '<', '<','<', '<', '<', '<', '<', '<','<', '<', '<','<', '<', '<', '>'},
/**//* - */{'>','>','<','<','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<', '<', '<','<', '<', '<', '<', '<', '<','<', '<', '<','<', '<', '<', '>'},
/**//* * */{'>','>','>','>','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<', '<', '<','<', '<', '<', '<', '<', '<','<', '<', '<','<', '<', '<', '>'},
/**//* / */{'>','>','>','>','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<', '<', '<','<', '<', '<', '<', '<', '<','<', '<', '<','<', '<', '<', '>'},
/**//* ( */{'<','<','<','<','<','=','<','<', '<', '<','<', '<', '<', '<','<', '<','<', '<', '<','<', '<', '<', '<', '<', '<','<', '<', '<','<', '<', '<', '@'},
/**//* ) */{'>','>','>','>','@','>','>','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '>'},
/**//* % */{'>','>','>','>','<','>','<','<', '<', '<','<', '<', '<', '<','<', '<','<', '<', '<','<', '<', '<', '<', '<', '<','<', '<', '<','<', '<', '<', '>'},
/**//* abs */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* acos */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* asin */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* cbrt */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* ceil */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* acos */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* cos */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* cos */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* exp */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* expm1*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* floor*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* log */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* log10*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* log1p*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* rint*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* round*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//*signum*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* sin */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* sinh */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* sprt*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* tan */{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* tanh*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//*toDegrees*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//*toRadians*/{'@','@','@','@','<','@','@','@', '@', '@','@', '@', '@', '@','@', '@','@', '@', '@','@', '@', '@', '@', '@', '@','@', '@', '@','@', '@', '@', '@'},
/**//* # */{'<','<','<','<','<','@','<','<', '<', '<','<', '<', '<', '<','<', '<','<', '<', '<','<', '<', '<', '<', '<', '<','<', '<', '<','<', '<', '<', '='}
};
public static void main(String [] args){
try{
System.out.println(new ExpressionAnalyze
("-cos(num1 * num2 -num3/num1)+(-E)",12,10,6)
.getResult());
}
catch(Exception e){
System.out.println(e.toString());
}
}
}
//将公式中关键字和其他量分开存放的链式结构
class ElemList {
ElemList (String value) {
data = value;
}
ElemList next;
String data;
boolean isOperator;
}
//Stack类中用到的链式结构
class ListElement {
ListElement (String value){
data = value;
}
ListElement next;
String data;
}
//--栈类
class Stack{
//返回栈顶元素的data域
public String top(){
if(top != null)
return top.data;
else
return null;
}
//将新元素压入栈
public void push(String value){
if(top == null)
top = new ListElement(value);
else{
ListElement temp = new ListElement(value);
temp.next = top;
top = temp;
}
}
//弹出栈顶元素并返回其data域
public String pop(){
String result = top();
if(top != null)
top = top.next;
return result;
}
//判断栈是否为空
public boolean empty(){
return top == null;
}
private ListElement top = null;
}
http://blog.csdn.net/ddpie/archive/2005/03/21/326025.aspx
posted on 2007-12-18 13:33
北国狼人的BloG 阅读(300)
评论(0) 编辑 收藏