使用堆栈java测试等式中的括号

  • 本文关键字:堆栈 java 测试 java
  • 更新时间 :
  • 英文 :


我正在写一个程序,它将接受一个方程,并检查是否所有的括号对齐,它将输出它是好还是不好。

对于示例:(3+4)是好的((3*8)不好

我不允许使用java的内置push() pop()方法ext..我要自己做一个,我想我有....我认为!我遇到的问题是在Test()方法中。

首先我不确定如何写while循环,如:

while(there are still characters)

无论如何,我得到的输出是:stack is empty -1

任何帮助都是感激的。我是一个学习程序比较慢的人,我不能再努力了。谢谢。

这是我得到的:

public class Stacked {
  int top;
  char stack[];
  int maxLen;
  public Stacked(int max) {
    top = -1; 
    maxLen = max; 
    stack = new char[maxLen];
  }
  public void push(char item) {
    top++;
    stack[top] = item;
  }
  public int pop() {
    //x = stack[top];
    //top = top - 1;
    top--;
    return stack[top];
  }
  public boolean isStackEmpty() {
    if(top == -1) {
      System.out.println("Stack is empty" + top);
      return true;
    } else 
      return false;
  }
  public void reset() { 
    top = -1;
  }
  public void showStack() {
    System.out.println(" ");
    System.out.println("Stack Contents...");
    for(int j = top; j > -1; j--){
      System.out.println(stack[j]);
    }
    System.out.println(" ");
  }
  public void showStack0toTop() {
    System.out.println(" ");
    System.out.println("Stack Contents...");
    for(int j=0; j>=top; j++){
      System.out.println(stack[j]); 
    }
    System.out.println(" ");
  }
//}
  public boolean test(String p ){
    boolean balanced = false;
    balanced = false;
    //while (  ) 
    for(char i = '('; i < p.length(); i++ ){
      push('(');
    }
    for (char j = ')'; j < p.length(); j++){
      pop();
    }
    if (isStackEmpty()) {
      balanced = true;
      //return balanced;
    }
    return balanced;
  } 
  public static void main(String[] args) {
    Stacked stacks = new Stacked(100);
    String y = new String("(((1+2)*3)");
    stacks.test(y);
    //System.out.println(stacks.test(y));
  }    
}

现在我有点头绪了。我需要再次被指引在正确的方向上。谢谢大家,这帮了大忙。我还有很多事情要做,但现在这很好。最终,我需要创建另外两个方法:一个是"中缀到后缀",另一个是"评估后缀",最后,我需要从文本文件中读取答案,而不是将自己的答案放入主方法中。再次感谢,非常感谢。

除非您需要实际计算公式,否则堆栈在这里是太复杂的解决方案。您只需要一个计数器:

int openParentheses = 0;
for (int i = 0; i < p.length(); i++) {
    if (p.charAt(i) == '(') {
        openParentheses++;
    } else if (p.charAt(i) == ')') {
        openParentheses--;
    }
    //check if there are more closed than open
    if (openParentheses < 0) {
        return false;
    }
}
if (openParentheses == 0) {
    return true;
} else {
    return false;
}

如果你一定要使用堆栈,使用:

for (int i = 0; i < p.length(); i++) {
    if (p.charAt(i) == '(') {
        push('x'); //doesn't matter what character you push on to the stack
    } else if (p.charAt(i) == ')') {
        pop();
    }
    //check if there are more closed than open
    if (stackIsEmpty()) {
        return false;
    }
}
if (isStackEmpty()) {
    return true;
} else {
    return false;
}

我同意Griff的观点,但是如果闭括号没有比开括号多,你应该包括另一个检查。(x*y))(不是有效的条目。

int openParentheses = 0;
for (int i = 0; i < p.length(); i++) {
    if (p.charAt(i) == '(') {
        openParentheses++;
    } else if (p.charAt(i) == ')') {
        openParentheses--;
    }
    if(openParentheses<0)
       return false;
}
if (openParentheses == 0) {
    return true;
} else {
    return false;
}

您可能需要使用堆栈,但这可以通过一个简单的计数器来完成。这将向您展示如何遍历String的字符:

boolean test(String p) {
  int balance = 0;
  for (int idx = 0; idx < p.length(); ++idx) {
    char ch = p.charAt(idx);
    if (ch == '(')
      ++balance;
    else if (ch == ')')
      --balance;
    if (balance < 0)
      return false;
  }
  return balance == 0;
}

当然,您可以在堆栈上分别用push和pop替换自增和自减操作

对于解析,您可以在索引上使用For循环,并在特定索引处寻址字符串的字符。

但实际上不需要堆栈,一个整数变量openbrace就足够了:

  • 初始化为0
  • 对于'(',你增加变量1
  • 对于')',您将变量减1
  • 如果openbrace为<0,你立即给出一个错误
  • 如果在最后openbrace不等于0,则给出错误。

因为你应该自己做作业,所以我没有发布源代码,只有解释;)

我想你正需要这个——

for ( int i = 0 ; i < p.length(); i++ ) {
    char c = p.charAt(i);
    if ( c == '(' ) 
        push('(');
    else if ( c == ')' ) {
        if ( isStackEmpty() ) {
          // Return error here because of unbalanced close paranthesis
        }
        pop();
    }
    else {
      // do nothing
    }
}

如果你必须的话,你可以使用堆栈,但是考虑到这是多么简单,你只需要一个计数器,你可以增加和减少,并在最后检查0。如果使用计数器,则应在每次减量后检查该值是否小于0。如果是,抛出错误。

根据Ryan/Dave Ball的评论编辑。

可以这样做:

String equation = "(2+3))";
        Integer counter = 0;
        //while(equation)
        for(int i=0; i<equation.length();i++)
        {
            if(equation.charAt(i)=='(')
            {
                counter++;
            }
            else
            if(equation.charAt(i)==')')
            {
                counter--;
            }
        }
        if(counter == 0)
        {
            System.out.println("Is good!!!");
        }
        else
        {
            System.out.println("Not good!!!");
        }
    }

最新更新