我正在编写验证算术表达式的程序是否正确形成。 (例如:正确的形式"2 + (2-1)"
,不正确的形式")2+(2-1"
)获取Java程序以读取括号,括号和大括号
一旦它被验证,程序将计算结果。
目前,它可以很容易地计算括号中的任何内容。但是如果涉及到括号(例如,"2 [ 3 + (1) ]"
)程序将验证表达式是否正确,但无法计算结果。
这是我关心的
void postfixExpression() {
stk.clear(); // Re-using the stack object
Scanner scan = new Scanner(expression);
char current;
// The algorithm for doing the conversion.... Follow the bullets
while (scan.hasNext()) {
String token = scan.next();
if (isNumber(token))
{
postfix = postfix + token + " ";
} else {
current = token.charAt(0);
if (isParentheses(current))
{
if (stk.empty() || current == Constants.LEFT_NORMAL) {
// push this element on the stack;
stk.push(new Character(current));
} else if (current == Constants.RIGHT_NORMAL) {
try {
Character ch = (Character) stk.pop();
char top = ch.charValue();
while (top != Constants.LEFT_NORMAL) {
postfix = postfix + top + " ";
ch = (Character) stk.pop();
top = ch.charValue();
}
} catch (EmptyStackException e) {
}
}
} else if (isOperator(current))//
{
if (stk.empty()) {
stk.push(new Character(current));
} else {
try {
char top = (Character) stk.peek();
boolean higher = hasHigherPrecedence(top, current);
while (top != Constants.LEFT_NORMAL && higher) {
postfix = postfix + stk.pop() + " ";
top = (Character) stk.peek();
}
stk.push(new Character(current));
} catch (EmptyStackException e) {
stk.push(new Character(current));
}
}
}// Bullet # 3 ends
}
} // Outer loop ends
try {
while (!stk.empty()) // Bullet # 4
{
postfix = postfix + stk.pop() + " ";
}
} catch (EmptyStackException e) {
}
}
我创建了两个方法的代码:isBracket和isCurly。起初,我认为最合适的解决方案就是将这两种方法包含在parentheses中。像这样:(但它仍然在读的括号罚款)
if (isParentheses(current))
{
if (stk.empty() || current == Constants.LEFT_NORMAL) {
// push this element on the stack;
stk.push(new Character(current));
} else if (current == Constants.RIGHT_NORMAL) {
try {
Character ch = (Character) stk.pop();
char top = ch.charValue();
while (top != Constants.LEFT_NORMAL) {
postfix = postfix + top + " ";
ch = (Character) stk.pop();
top = ch.charValue();
}
} catch (EmptyStackException e) {
}
}
if (isCurly(current))
{
if (stk.empty() || current == Constants.LEFT_CURLY) {
// push this element on the stack;
stk.push(new Character(current));
} else if (current == Constants.RIGHT_CURLY) {
try {
Character ch = (Character) stk.pop();
char top = ch.charValue();
while (top != Constants.LEFT_CURLY) {
postfix = postfix + top + " ";
ch = (Character) stk.pop();
top = ch.charValue();
}
} catch (EmptyStackException e) {
if (isBracket(current))
{
if (stk.empty() || current == Constants.LEFT_SQUARE) {
// push this element on the stack;
stk.push(new Character(current));
} else if (current == Constants.RIGHT_SQUARE) {
try {
Character ch = (Character) stk.pop();
char top = ch.charValue();
while (top != Constants.LEFT_SQUACRE) {
postfix = postfix + top + " ";
ch = (Character) stk.pop();
top = ch.charValue();
}
} catch (EmptyStackException e) {
但该计划仍然不会考虑括号和大括号
我没有正确使用的方法从我个人理解,但我怎样才能恰当地使用它们?
您的示例“2 [3 +(1)]”不包含大括号,只包含圆括号和方括号。你的意思是使用方括号吗?此外,嵌套的括号只能嵌套在方括号中的圆括号中,还是方括号可以嵌套在圆括号内? – Bohemian 2014-11-05 21:58:20
“但无法计算结果”程序是否计算出错误或者是否有异常?你检查了2(3 +(1))是否在工作吗? – Turing85 2014-11-05 21:59:11
您是否尝试过使用调试器,或者在代码中放置一些'println'或其他东西来验证变量是您认为它们应该是的还是这些方法正在返回您认为它们应该返回的内容? – ajb 2014-11-05 22:01:49