62,614
社区成员
发帖
与我相关
我的任务
分享
Exception in thread "main" java.util.NoSuchElementException
at java.util.LinkedList.remove(LinkedList.java:788)
at java.util.LinkedList.removeFirst(LinkedList.java:134)
at java.util.LinkedList.pop(LinkedList.java:601)
at Temp.OPText.main(OPText.java:64)
//主类
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class OPText {
public static void main(String[] args) {
String expression = "2+2+(8-2)/3";// 要计算的表达式
List list = new LinkedList();
// 正则式
Pattern entryOfExpression = Pattern.compile("[0-9]+(\\.[0-9]+)? |\\( |\\) |\\+ |- |\\* |/");
Deque stack = new LinkedList();// 栈
Matcher m = entryOfExpression.matcher(expression);
while (m.find()) {
// 提取语素
String nodeString = expression.substring(m.start(), m.end());
if (nodeString.matches("[0-9].*")) {
list.add(Double.valueOf(nodeString));// 如果是数字直接送入列表
} else {
OPNode opn = new OPNode(nodeString);// 如果是运算符
int peekLevel = (stack.peek() == null) ? 0 : ((OPNode)stack.peek()).level;
if (opn.level >= peekLevel) {
stack.push(opn);// 新的运算符比旧的优先级别高则入栈
} else {
if (opn.level == -1) {
OPNode temp = (OPNode) stack.pop();
while (temp.level != -3) {// 如果为"("则一直出栈一直到")"
list.add(temp);
System.out.println(nodeString);
temp = (OPNode)stack.pop();
}
} else if (opn.level == -3) {
stack.push(opn);
} else {// 如果新运算符比栈顶运算符底则一直出栈
OPNode temp = (OPNode) stack.pop();
while (temp.level > opn.level) {
list.add(temp);
if (stack.isEmpty()) {
break;
}
temp = (OPNode)stack.pop();
}
stack.push(opn);
}
}
}
}
OPNode temp = null;
while (!stack.isEmpty()) {
temp = (OPNode)stack.pop();
list.add(temp);
}// 后续表达式计算
stack.clear();
for (Object o : list) {
if (o instanceof Double) {
stack.push(o);// 为数字入栈
} else {
double op2 = ((Double)stack.pop()).doubleValue();
double op1 = ((Double)stack.pop()).doubleValue();
switch (((OPNode) o).op) {
case '+':
stack.push(op1 + op2);
break;
case '-':
stack.push(op1 - op2);
break;
case '*':
stack.push(op1 * op2);
break;
case '/':
stack.push(op1 / op2);
break;
}
}
}
System.out.println("结果为:" + stack.pop());
}
}
public class OPNode {
char op;// 运算符号
int level;// 优先级
//设置优先级
public OPNode(String op) {
this.op = op.charAt(0);
if (op.equals("+") || op.equals("-")) {
this.level = 1;
}else if (op.equals("*") || op.equals("/")) {
this.level = 2;
}else if (op.equals("(")) {
this.level = -3;
}else {
this.level = -1;
}
}
}
import javax.script.*;
public class ScriptTest
{
public static void main(final String[] args)
{
ScriptEngineManager manager = new ScriptEngineManager();
final ScriptEngine engine = manager.getEngineByName("js");
if (engine == null)
{
System.err.println("No engine for JavaScript" );
System.exit(1);
}
try{
String s="2+2+(8-2)/3";//此行可变为从文件读入的表达式串。
System.out.print(s+"=");
Object result=engine.eval(s);
System.out.println((int)(double)result);
}
catch (Exception e){}
}
}
import javax.script.*;
public class ScriptTest {
public static void main(final String[] args) {
ScriptEngineManager manager = new ScriptEngineManager();
final ScriptEngine engine = manager.getEngineByName("js");
if (engine == null) {
System.err.println("No engine for JavaScript");
System.exit(1);
}
try {
// 此行可变为从文件读入的表达式串。
String s = "2+2+(8-2)/4";
System.out.print(s + "=");
Object result = engine.eval(s);
// 类型转换要修正下。上面那样写一直报错
String str = result+"";
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}