📄 strexpression.java
字号:
TempStack.push(strTemp);
}//end if
}//end if ( strOperator.indexOf(strTemp)!=-1 ) //如果是运算符号
if ( isOp(strTemp) == false ) {//如果是运算数
TempStack.push(strTemp);//将运算数直接压栈
}
}//end for
// SourceStack.addAll((Collection)s.clone());
return ;
}
//第2层处理,消除第2级别的运算符号
private void delLevel2() {
//第2层处理,消除第2级别的运算符号
String strTemp;//用来保存从SourceStack中弹出来的字符串.
int intSize = SourceStack.size() ;//堆栈大小
//循环检测2级运算符,并消除之.
for( int i=0 ;i< intSize;i++) {
//注意,程序最多的循环次数是intSize,当表达式中出现*,/号时,循环次数会小
//于intSize次,所以必须加上下面这条语句.
if (SourceStack.isEmpty())
break;
strTemp = SourceStack.pop().toString();
if ( isOp(strTemp) ) {//如果是运算符号
int intLevel = this.getLevel(strTemp);//获取运算级别
if ( intLevel==2) {
Object objX = TempStack.pop();//取出运算数
Object objY = SourceStack.pop();//取出运算数
String strClass = getClass(strTemp);
try {
Class clsCalculate = Class.forName(strClass);
DOperator cal = (DOperator)clsCalculate.newInstance();
TempStack.push(cal.calculate(objX,objY));
}catch(Exception e) {
e.printStackTrace();
}//end try-catch
}//if (intLevel==2)
if ( intLevel==3) {//将3级别运算符号进行压栈处理
TempStack.push(strTemp);
}//end if
}//end if ( strOperator.indexOf(strTemp)!=-1 ) //如果是运算符号
if ( isOp(strTemp) == false ) {//如果是运算数
TempStack.push(strTemp);//将运算数直接压栈
}
}//end for
}//end level2
//第三层处理,消除第三级别的运算符号
private double delLevel3() {
//第三层处理,消除第三级别的运算符号
while(true) {
if ( TempStack.size() == 1 )//如果TempStack中只剩下一项,那么剩下的这项必定
return ( (Double)TempStack.pop() ).doubleValue();//是计算结果.
Object objX = TempStack.pop();//取出运算数
String strOp = TempStack.pop().toString();//获取操作符号
Object objY = TempStack.pop();//取出运算数
String strClass = getClass(strOp);
try {
Class clsCalculate = Class.forName(strClass);
DOperator cal = (DOperator)clsCalculate.newInstance();
TempStack.push(cal.calculate(objX,objY));
}catch(Exception e) {
e.printStackTrace();
}
}//end while
}
}//end StringToValue
//-------------------------------------------------------------------------
//}//end StringExpression.
//=========================================================================================
//标准运算符号的实现类:
// 命名约定:
// 单目运算符的实现类以 S 开头
// 双目运算符的实现类一 D 开头
//================================================================================
//第三级别实现类==================================================================
//加法运算实现类
class DAdd implements DOperator {
public Object calculate(Object op_num1,Object op_num2) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num1.toString());//取被加数
double y = Double.parseDouble(op_num2.toString());//取加数
double result = x + y;
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//end DAdd
//减法运算实现类
class DDec implements DOperator {
public Object calculate(Object op_num1,Object op_num2) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num1.toString());//取被减数数
double y = Double.parseDouble(op_num2.toString());//取减数
double result = x - y;
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//DDec
//================================================================================
//第2级别实现类
//================================================================================
//乘法运算实现类
class DMul implements DOperator {
public Object calculate(Object op_num1,Object op_num2) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num1.toString());//取被乘数
double y = Double.parseDouble(op_num2.toString());//取乘数
double result = x * y;
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//DMul
//除法运算实现类
class DDiv implements DOperator {
public Object calculate(Object op_num1,Object op_num2) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num1.toString());//取被除数
double y = Double.parseDouble(op_num2.toString());//取除数
double result = x / y;
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//DDiv
//求余运算实现类
class DMod implements DOperator {
public Object calculate(Object op_num1,Object op_num2) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num1.toString());//取被除数
double y = Double.parseDouble(op_num2.toString());//取除数
double result = x % y;
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//DDod
//求指数运算实现类
class DPow implements DOperator {
public Object calculate(Object op_num1,Object op_num2) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num1.toString());//取被除数
double y = Double.parseDouble(op_num2.toString());//取除数
double result = Math.pow(x,y);
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//DPow
//================================================================================
//第一级别实现类=================================================================
//求倒数
class SDao implements SOperator {
public Object calculate(Object op_num) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num.toString());//取被除数
double result =1/x;
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//DDiv
//开方
class SSqrt implements SOperator {
public Object calculate(Object op_num) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num.toString());//取被除数
double result =Math.sqrt(x);
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//DSqrt
//正玄
class SSin implements SOperator {
public Object calculate(Object op_num) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num.toString());//取被除数
double result =Math.sin(x);
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//DSin
//正玄
class SCos implements SOperator {
public Object calculate(Object op_num) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num.toString());//取被除数
double result =Math.cos(x);
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//DCos
//正切
class STan implements SOperator {
public Object calculate(Object op_num) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num.toString());//取被除数
double result =Math.tan(x);
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//STan
//exp
class SExp implements SOperator {
public Object calculate(Object op_num) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num.toString());//取被除数
double result =Math.exp(x);
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//SExp
//log
class SLog implements SOperator {
public Object calculate(Object op_num) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num.toString());//取被除数
double result =Math.log(x);
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//SLog
//N!
class SN implements SOperator {
public Object calculate(Object op_num) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num.toString());//取被除数
double result =n(new Double(x).intValue());
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
private double n(int x) {
if ( x==1 )
return 1;
else
return x*n(x-1);
}
}//SN
//N!
class SX3 implements SOperator {
public Object calculate(Object op_num) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num.toString());//取被除数
double result = Math.pow(x,3);
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//SX3
class SX2 implements SOperator {
public Object calculate(Object op_num) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num.toString());//取被除数
double result = Math.pow(x,2);
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//SX2
class SLn implements SOperator {
public Object calculate(Object op_num) throws DataInvalidException {
try {
double x = Double.parseDouble(op_num.toString());//取被除数
double result = Math.log(x)/Math.log(10);
return new Double(result);
} catch (Exception e) {
throw new DataInvalidException("您输入的字符串表达式,不符合格式要求,"+
"导致无法计算,请检测您的输入表达式");
}// end try-catch
}//end calculate
}//SLn
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -