String str = "1+2-3*4/(1-5)";
怎么样通过这个字符串得到该公式算出来的值?
[b]问题补充:[/b]
请问,我定义
Stack stack = new Stack()
这个的时候,怎么会出错了?
[b]问题补充:[/b]
List list
我这样定义的时候,出现这样的错误 expented
我用了jre1.5了,也import 了
[b]问题补充:[/b]
public class test1 {
public static void main(String[] args) {
test1 test11 = new test1();
List list;
}
}
这样出现了not a statement的错误,我已经在juild配置了jdk1.5了,也import了。不知道为什么还有这样的错误?
[b]问题补充:[/b]
我用的是JBuilder X版本,JDK配置的是jdk1.5.0_12
就是不知道为什么出错,真郁闷~
[b]问题补充:[/b]
还是不行啊。都不知道什么原因。。
Eclipse没用过,一直都用jbuild。。
不过还是谢谢RednaxelaFX兄台了。呵呵~~
字符串运算公式求值问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
8条回答 默认 最新
- rednaxelafx 2008-12-15 12:40关注
你编译一下这个文件看看:
Test.java:
[code="java"]import java.util.*;public class Test {
public static void main(String[] args) {
List list;
}
}[/code]
如果环境正确,那这段代码不应该出错。如果编译这段代码没出错而编译你自己写的代码出错了,那对比一下看看问题到底是什么。或者把你出问题的代码整个贴出来,不然无从判断什么地方有问题。稍微再改改……难怪优先级相等的时候不对劲,原来我少写了个等号。
[code="java"]import java.util.Scanner;
import java.util.Stack;public class SimpleCalculator {
/**
* Evaluate an arithmetic expression, and return the result as a double.
* @param input the expression to evaluate.
* @return the evaluated result.
*/
public double evaluate(String input) {
initialize();this.scanner = new Scanner(input); this.scanner.useDelimiter("\\s+|(?=[.0-9])(?<![.0-9])|(?![.0-9])(?<=[.0-9])|(?![.0-9])(?<![.0-9])"); Token currentToken = nextToken(); Token t = null; while (null != currentToken) { switch (currentToken.getKind()) { case NUMBER: // Simply push number tokens onto the evaluation stack. this.eval.push(currentToken.getValue()); break; case LPAREN: // Simply push left parenthesis tokens onto the operator stack. this.ops.push(currentToken); break; case RPAREN: // Until a left parenthesis pops off the operator stack, keep // poping operators and execute them. // If the stack becomes empty without a matching left parenthesis, // the expression must have syntax errors. for (t = this.ops.pop(); TokenKind.LPAREN != t.getKind(); t = this.ops.pop()) { if (ops.empty()) throw new Error("Syntax Error: unmatched parenthesis"); doOperation(t); } break; default: // For binary arithmetic operators, keep poping operators whose binding power // is less or equal to the current token's and execute them; after that push // the current token onto the operator stack. if (!ops.empty()) { for (t = this.ops.pop(); currentToken.getKind().getBindingPower() <= t.getKind().getBindingPower(); t = this.ops.pop()) { doOperation(t); if (this.ops.empty()) { t = null; break; } } } if (null != t) ops.push(t); ops.push(currentToken); break; } // reinitialize currentToken = nextToken(); } // execute remaining operators on stack while (!ops.empty()) { t = this.ops.pop(); doOperation(t); } // the result is on the top of evaluation stack, // pop it off and return the result. return this.eval.pop(); } /* * Initialize the evaluation and operator stacks. */ private void initialize() { if (null == this.eval) this.eval = new Stack<Double>(); if (null == this.ops) this.ops = new Stack<Token>(); this.eval.clear(); this.ops.clear(); } /* * Return the next token from the input expression. * The token returned will be associated with its numeric value, * if and only if the token is a number. */ private Token nextToken() { Token t = null; if (this.scanner.hasNextDouble()) { t = new Token(TokenKind.NUMBER, this.scanner.nextDouble()); } else if (this.scanner.hasNext()) { String s = this.scanner.next("[-+*/()]"); if ("+".equals(s)) { t = new Token(TokenKind.ADD); } else if ("-".equals(s)) { t = new Token(TokenKind.SUBTRACT); } else if ("*".equals(s)) { t = new Token(TokenKind.MULTIPLY); } else if ("/".equals(s)) { t = new Token(TokenKind.DIVIDE); } else if ("(".equals(s)) { t = new Token(TokenKind.LPAREN); } else if (")".equals(s)) { t = new Token(TokenKind.RPAREN); } } return t; } /* * Execute a binary arithmetic operation. * Pop the top two values off the evaluation stack, do the operation, * and then push the result back onto the evaluation stack. */ private void doOperation(Token t) { double y = this.eval.pop(); double x = this.eval.pop(); double temp = t.getKind().doOperation(x, y); this.eval.push(temp); } /* * Tokenizer for the input expression. */ private Scanner scanner; /* * Evaluation stack. */ private Stack<Double> eval; /* * Operator stack, for converting infix expression to postfix expression. */ private Stack<Token> ops; public static void main(String[] args) { if (args.length < 1) { System.err.println("Usage: java SimpleCalculator <expression>"); System.exit(1); } SimpleCalculator calc = new SimpleCalculator(); double result = calc.evaluate(args[0]); System.out.println(result); }
}
enum TokenKind {
// operators
ADD(1) {
public double doOperation(double x, double y) {
return x + y;
}
},
SUBTRACT(1) {
public double doOperation(double x, double y) {
return x - y;
}
},
MULTIPLY(2) {
public double doOperation(double x, double y) {
return x * y;
}
},
DIVIDE(3) {
public double doOperation(double x, double y) {
return x / y;
}
},// punctuation LPAREN(0), RPAREN(0), // number NUMBER(0); TokenKind(int bindingPower) { this.bindingPower = bindingPower; } public int getBindingPower() { return this.bindingPower; } public double doOperation(double x, double y) { return Double.NaN; // dummy, operation not supported } private int bindingPower;
}
class Token {
public Token(TokenKind kind) {
this(kind, Double.NaN);
}public Token(TokenKind kind, double value) { this.kind = kind; this.value = value; } public TokenKind getKind() { return this.kind; } public double getValue() { return this.value; } private TokenKind kind; private double value;
}[/code]
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记
- ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
- ¥15 保护模式-系统加载-段寄存器
- ¥15 电脑桌面设定一个区域禁止鼠标操作