使用堆栈java测试等式中的括号
我正在写一个程序,它将接受一个等式,检查所有括号是否对齐,它将输出是否正确 例如:使用堆栈java测试等式中的括号,java,Java,我正在写一个程序,它将接受一个等式,检查所有括号是否对齐,它将输出是否正确 例如:(3+4)很好 ((3*8)不好 我不允许使用java内置的push()pop()方法。。 我必须做我自己的,我想我得到了…我想! 我遇到的问题是Test()方法 首先,我不确定如何编写while循环,如: while(仍有字符) 无论如何,我得到的输出是:堆栈为空-1 非常感谢您的帮助。我是学习程序较慢的学生之一,我不能再努力了。谢谢 以下是我得到的: public class Stacked { int
(3+4)
很好
((3*8)
不好
我不允许使用java内置的push()pop()方法。。
我必须做我自己的,我想我得到了…我想!
我遇到的问题是Test()方法
首先,我不确定如何编写while循环,如:
while(仍有字符)
无论如何,我得到的输出是:堆栈为空-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 top;
字符堆栈[];
int-maxLen;
公共堆叠(整数最大值){
top=-1;
最大值=最大值;
堆栈=新字符[maxLen];
}
公共无效推送(字符项){
top++;
堆栈[顶部]=项目;
}
公共int-pop(){
//x=堆栈[顶部];
//top=top-1;
顶部--;
返回堆栈[顶部];
}
公共布尔值为空(){
如果(顶部==-1){
System.out.println(“堆栈为空”+顶部);
返回true;
}否则
返回false;
}
公共无效重置(){
top=-1;
}
公共void showStack(){
System.out.println(“”);
System.out.println(“堆栈内容…”);
对于(int j=top;j>-1;j--){
System.out.println(stack[j]);
}
System.out.println(“”);
}
公开作废showStack0toTop(){
System.out.println(“”);
System.out.println(“堆栈内容…”);
对于(int j=0;j>=top;j++){
System.out.println(stack[j]);
}
System.out.println(“”);
}
//}
公共布尔测试(字符串p){
布尔平衡=假;
平衡=错误;
//而()
对于(char i='(';i
int open圆括号=0;
对于(int i=0;i
如果您必须使用堆栈,请使用以下选项:
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;
}
for(int i=0;i
我想你只需要这个--
for(int i=0;i
如果必须,您可以使用堆栈,但考虑到这是多么简单,您只需要一个递增和递减的计数器,并在最后检查0。
如果确实使用计数器,则应在每次减量后检查值是否小于0。如果是,则抛出错误
根据Ryan/Dave Ball的评论编辑您可能需要使用堆栈,但这可以通过一个简单的计数器来完成。这将向您展示如何迭代
字符串的字符
:
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;
}
布尔测试(字符串p){
国际收支平衡=0;
对于(int idx=0;idx
当然,可以在堆栈上分别用push和pops替换递增和递减。解析时,可以在索引上使用For循环,并在特定索引处寻址字符串的字符 但实际上不需要堆栈,整数变量openBraces就足够了:
- 用0初始化
- 对于“(”,将变量1递增
- 对于“'),您将递减变量1
- 如果openBraces是的话,我同意Griff的观点,除了如果你没有比open(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; }
int open圆括号=0; 对于(int i=0;i
如果(开括号可以这样做: 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!!!"); } }
String等式=“(2+3))”; 整数计数器=0; //while(等式)
for(inti=0;iDo)必须使用堆栈,还是允许使用递归?字符上的循环应该使用for循环--for(inti=0;iString 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!!!"); } }