📄 comexpression.java
字号:
/*
* ComExpression.java
*
* Created on 2001年11月17日, 上午11:08
*/
/*
*本类主要实现象 2+3*(3-1)/(10+2)这样的表达式
*/
package com.gs.util;
import java.util.*;
import java.lang.*;
public class ComExpression {
private static final String ADD = "+";
private static final String MINUSE = "-";
private static final String RIDE = "*";
private static final String DIVIDE = "/";
private static final String POWER = "^";
private static final String LEFT_BRACKET = "(";
private static final String RIGHT_BRACKET = ")";
public java.util.Stack operand = new Stack();
public java.util.Stack operator = new Stack();
public static String expression = null;
public double result;
public String resultStr = null;
public static boolean isError = false;
public static String errorStr = null;
public ComExpression(String exp)
{
exp = exp.toLowerCase().trim();//删除空格和将大写字符转换为小写字符
this.isError = this.isLegality(exp);//判断表达式是否合法
if(isError)
{
return;
}
exp = this.expression;
this.errorStr = null;
this.isError = false;
////////////////////////////////////////计算////////////////////////////////////////////
String lopn = "0";//操作数
String lopn1 = null;//操作数
String lopn2 = null;//操作数
String lopr = null;//操作符
String top = null;
int sepera = 0;
int numCount = 0;
operator.push("#");
boolean isOper = false;
while(exp.length() > 0)
{
for(int i = 0;i < exp.length();i++)
{
String temp = exp.substring(i,i+1);
///////////////////////////////////////////////////////////////////////////
if((temp.hashCode() >= 48&&temp.hashCode() <= 57)||temp.hashCode()==46)///是否是数字或小数点
{
numCount++;//计算该操作数的长度
}
else//是操作符
{
if(numCount > 0)
{
lopn = exp.substring(i - numCount,i);//取出操作数
operand.push(lopn);//将该操作数入栈
}
top = this.getTop(operator);
if(top.equals("(")&&temp.equals(")"))
{
operator.pop();
}
else
{
switch(this.precede(top,temp).charAt(0))//比较操作符栈中的第一个操作符与输入的操作的优先级
{
case '<':
operator.push(temp);//操作符入栈
break;
case '='://括号或"#"时才会出现这种情况
// operator.pop();
top = this.getTop(operator);
if(!top.equals("#"))
{
//////////////////////////////////
String exp_temp = null;
while(!top.equals("#"))
{
if(top.equals("("))
{
break;
}
lopr = (String)operator.pop();
top = this.getTop(operator);
///////////////////????//////////////////////
if(lopr.equals("c")||lopr.equals("s")||lopr.equals("t")||lopr.equals("e")||lopr.equals("l"))//单目运算
{
lopn1 = (String)operand.pop();
lopn = this.colcu(lopr,lopn1,"0.0");
if(isError)
{
// return;
}
}
else
{
lopn2 = (String)operand.pop();
lopn1 = (String)operand.pop();
lopn = this.colcu(lopr,lopn1,lopn2);
if(isError)
{
// return;
}
}
operand.push(lopn);
}
break;
}
///////////////////////////////////////
else
{
operator.pop();
this.resultStr = (String)operand.pop();
return;
}
case '>':
////////////////////////////////////////////////////////////////
if(temp.equals(")"))
{
top = this.getTop(operator);
String exp_temp = null;
while(!top.equals("("))
{
lopr = (String)operator.pop();
top = this.getTop(operator);
///////////////////????//////////////////////
if(lopr.equals("c")||lopr.equals("s")||lopr.equals("t")||lopr.equals("e")||lopr.equals("l"))//单目运算
{
lopn1 = (String)operand.pop();
lopn = this.colcu(lopr,lopn1,"0.0");
if(lopn==null)
{
isError = true;
return;
}
}
else
{
lopn2 = (String)operand.pop();
lopn1 = (String)operand.pop();
lopn = this.colcu(lopr,lopn1,lopn2);
if(lopn==null)
{
isError = true;
return;
}
}
operand.push(lopn);
}
operator.pop();//去掉"("
}
else
{
////////////////////////////////////////////////////////////////////////
lopr = (String)operator.pop();
/////////////////////////////////////////////
if(lopr.equals("c")||lopr.equals("s")||lopr.equals("t")||lopr.equals("e")||lopr.equals("l"))//单目运算
{
lopn1 = (String)operand.pop();
lopn = this.colcu(lopr,lopn1,"0.0");
if(lopn==null)
{
isError = true;
return;
}
operand.push(lopn);
top = this.getTop(operator);
while(top.equals("c")||top.equals("s")||top.equals("t")||top.equals("e")||top.equals("l"))
{
lopr = (String)operator.pop();
top = this.getTop(operator);
lopn1 = (String)operand.pop();
lopn = this.colcu(lopr,lopn1,"0.0");
if(lopn==null)
{
isError = true;
return;
}
operand.push(lopn);
}
////////////////////////////
top = this.getTop(operator);
while(this.precede(top,temp).equals(">"))
{
lopr = (String)operator.pop();
if((top.equals("c")||top.equals("s")||top.equals("t")||top.equals("e")||top.equals("l")))
{
lopn1 = (String)operand.pop();
lopn = this.colcu(lopr,lopn1,"0.0");
if(lopn==null)
{
isError = true;
return;
}
}
else
{
lopn2 = (String)operand.pop();
lopn1 = (String)operand.pop();
lopn = this.colcu(lopr,lopn1,lopn2);
if(lopn==null)
{
isError = true;
return;
}
}
top = this.getTop(operator);
operand.push(lopn);
}
////////////////////////////
}
else
{
lopn2 = (String)operand.pop();
lopn1 = (String)operand.pop();
lopn = this.colcu(lopr,lopn1,lopn2);
if(lopn==null)
{
isError = true;
return;
}
operand.push(lopn);
/////////////////////////////////////////////////////////
top = this.getTop(operator);
while(top.equals("c")||top.equals("s")||top.equals("t")||top.equals("e")||top.equals("l"))
{
lopr = (String)operator.pop();
top = this.getTop(operator);
lopn1 = (String)operand.pop();
lopn = this.colcu(lopr,lopn1,"0.0");
if(lopn==null)
{
isError = true;
return;
}
operand.push(lopn);
}
////////////////////////////
top = this.getTop(operator);
while(this.precede(top,temp).equals(">"))
{
lopr = (String)operator.pop();
if((top.equals("c")||top.equals("s")||top.equals("t")||top.equals("e")||top.equals("l")))
{
lopn1 = (String)operand.pop();
lopn = this.colcu(lopr,lopn1,"0.0");
if(lopn==null)
{
isError = true;
return;
}
}
else
{
lopn2 = (String)operand.pop();
lopn1 = (String)operand.pop();
lopn = this.colcu(lopr,lopn1,lopn2);
if(lopn==null)
{
isError = true;
return;
}
}
top = this.getTop(operator);
operand.push(lopn);
}
////////////////////////////
/////////////aaaaaaa///////////////
}
////////////////////////////////
if(temp.equals("#"))//表达式的字符已经全部入栈
{
top = this.getTop(operator);
if(top.equals("#"))
{
this.resultStr = (String)operand.pop();
return;
}
///////////////使用递归计算栈中剩余的数据/////////////
while(!top.equals("#"))
{
lopr = (String)operator.pop();
top = this.getTop(operator);
///////////////////????////////////////////////
/* if(lopr.equals("c")||lopr.equals("s")||lopr.equals("t")||lopr.equals("e")||lopr.equals("l"))//单目运算
{
lopn1 = (String)operand.pop();
lopn1 = this.colcu(lopr,lopn1,"0.0");
exp = lopn1;
}
else
{*/
lopn2 = (String)operand.pop();
lopn1 = (String)operand.pop();
if(lopn1.substring(0,1).equals("-"))
{
lopn1 = "(0"+lopn1+")";//如果是负数则前面加0如:(-2)-->(0 - 2)
}
if(lopn2.substring(0,1).equals("-"))
{
lopn2 = "(0"+lopn2+")";//如果是负数则前面加0如:(-2)-->(0 - 2)
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -