📄 comexpression.java
字号:
{
index = exp.indexOf("tan");
if(index >= 0)
{
exp = exp.substring(0,index + 1) + exp.substring(index + 3);//将tan转化为:t
}
else
{
index = exp.indexOf("exp");
if(index >= 0)
{
exp = exp.substring(0,index + 1) + exp.substring(index + 3);//将exp转化为:e
}
else
{
index = exp.indexOf("ln");
if(index >= 0)
{
exp = exp.substring(0,index + 1) + exp.substring(index + 2);//将ln转化为:l
}
else
{
break;
}
}
}
}
}
}
//////////////////////////////
expression = exp + "#";
///////////////////////////////
int countBracket = 0;
for(int i = 0;i < exp.length();i++)
{
String temp = exp.substring(i,i+1);
///////////////检查第一个和最后一个字母的合法性////////////////
if(i == 0 || i == exp.length() - 1)
{
for(int j = 0; j < 6;j++)
{
if(op_temp[j].equals(temp))
{
isLeg = true;
errorStr = "表达式的第一个或最后一个字符输入错误";
return isLeg;
}
}
for(int j = 8;j < 13;j++)
{
if(op_temp[j].equals(temp)&&(i==exp.length() - 1))
{
isLeg = true;
errorStr = "表达式的最后一个字符输入错误";
return isLeg;
}
}
}
//////////////////////检查是否出现两个运算符相邻///////////////////////////////////
String temp_per = null;
boolean isPr0 = false;
boolean isPr1 = false;
if(i >= 1)
{
temp_per = exp.substring(i - 1,i);
if(temp.equals(")")&&temp_per.equals("("))
{
isLeg = true;
errorStr = "出现( )错误";
return isLeg;
}
for(int j = 0;j < 6;j++)
{
if(temp.equals(op_temp[j]))
{
isPr0 = true;
}
if(temp_per.equals(op_temp[j]))
{
isPr1 = true;
}
if(isPr0&&isPr1)
{
isLeg = true;
errorStr = "两个运算符相邻:["+temp+"]和["+temp_per+"]";
return isLeg;
}
}
}
/////////////////////////////检查公式中是否含有字母////////////////////////////
int hashInt = temp.hashCode();
if(hashInt < 48||hashInt > 57)
{
isLeg = true;
for(int j = 0;j < 13;j++)
{
if(temp.equals(op_temp[j]))
{
isLeg = false;
break;
}
}
if(isLeg)
{
errorStr = "公式中包含有字母["+temp+"]";
return isLeg;
}
}
//////////////////////////////////判断 cos,sin,tan,exp,ln 前后的合法性/////////////////////////////////////////
if(temp.equals("c")||temp.equals("s")||temp.equals("t")||temp.equals("e")||temp.equals("l"))
{
if(i >= 1)
{
temp_per = exp.substring(i - 1,i);
isLeg = true;
for(int j = 0;j < 4;j++)
{
if(temp_per.equals(op_temp[j]))
{
isLeg = false;
break;
}
}
for(int j = 8;j < 13;j++)
{
if(temp_per.equals(op_temp[j]))
{
isLeg = false;
break;
}
}
if(temp_per.equals("(")||temp_per.equals("^"))
{
isLeg = false;
}
if(isLeg)
{
errorStr = "表达式左括号前输入了错误字符["+temp_per+"(]";
return isLeg;
}
}
if(i < exp.length() - 1)
{
temp_per = exp.substring(i+1,i+2);
if(temp_per.equals(op_temp[7]))
{
isLeg = true;
errorStr = "表达式左括号后输入了错误字符[("+temp_per+"]";
return isLeg;
}
for(int j = 0;j < 6;j++)
{
if(temp_per.equals(op_temp[j]))
{
isLeg = true;
errorStr = "表达式左括号后输入了错误字符[("+temp_per+"]";
}
}
if(temp_per.equals("-"))
{
isLeg = false;
errorStr = null;
exp = exp.substring(0,i+1)+"0"+exp.substring(i+1);
expression = exp+"#";
}
if(isLeg)
{
return isLeg;
}
}
}
//////////////////////////////////判断"("括号前后的合法性///////////////////////////////////////////////////
if(temp.equals("("))
{
countBracket++;
if(i >= 1)
{
temp_per = exp.substring(i - 1,i);
isLeg = true;
for(int j = 0;j < 4;j++)
{
if(temp_per.equals(op_temp[j]))
{
isLeg = false;
break;
}
}
for(int j = 8;j < 13;j++)
{
if(temp_per.equals(op_temp[j]))
{
isLeg = false;
break;
}
}
if(temp_per.equals("(")||temp_per.equals("^"))
{
isLeg = false;
}
if(isLeg)
{
errorStr = "表达式左括号前输入了错误字符["+temp_per+"(]";
return isLeg;
}
}
if(i < exp.length() - 1)
{
temp_per = exp.substring(i+1,i+2);
if(temp_per.equals(op_temp[7]))
{
isLeg = true;
errorStr = "表达式左括号后输入了错误字符[("+temp_per+"]";
return isLeg;
}
for(int j = 0;j < 6;j++)
{
if(temp_per.equals(op_temp[j]))
{
isLeg = true;
errorStr = "表达式左括号后输入了错误字符[("+temp_per+"]";
}
}
if(temp_per.equals("-"))
{
isLeg = false;
errorStr = null;
exp = exp.substring(0,i+1)+"0"+exp.substring(i+1);
expression = exp+"#";
}
if(isLeg)
{
return isLeg;
}
}
}
//////////////////////////////////判断")"括号前后的合法性/////////////////////////////////////////
if(temp.equals(")"))
{
countBracket = countBracket - 1;
if(i >= 1)
{
temp_per = exp.substring(i-1,i);
if(temp_per.equals("("))
{
isLeg = true;
errorStr = "表达式中的右括号前输入错误字符:["+temp_per+")]";
return isLeg;
}
for(int j = 0;j < 6;j++)
{
if(temp_per.equals(op_temp[j]))
{
isLeg = true;
errorStr = "表达式中的右括号前输入错误字符:["+temp_per+")]";
return isLeg;
}
}
}
if(i < exp.length() - 1)
{
temp_per = exp.substring(i+1,i+2);
isLeg = true;
for(int j = 0;j < 4;j++)
{
if(temp_per.equals(op_temp[j]))
{
isLeg = false;
break;
}
}
if(temp_per.equals(")")||temp_per.equals("^"))
{
isLeg = false;
}
if(isLeg)
{
errorStr = "表达式中右括号的后面输入错误字符:[)"+temp_per+"]";
return isLeg;
}
}
}
}
///////////////////////////检测括号是否匹配///////////////////////////////////
if(countBracket != 0)
{
isLeg = true;
errorStr = "括号不匹配";
if(countBracket > 0)
{
errorStr = "左括号多了"+countBracket+"个";
}
if(countBracket < 0)
{
errorStr = "右括号多了"+Math.abs(countBracket)+"个";
}
return isLeg;
}
return isLeg;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////double转换为String 精确到0.00////////////////////////////////////////
public static String floadToint(double input)
{
int dInt = 0;
int dDec = 0;
String doubleStr = null;
double temp = 0.00;
if(input < 0)
{
temp = 0-(Math.abs(input)+0.005);
}
else
{
temp = input + 0.005;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -