用c语言中的堆栈检查字符串中的平衡圆括号
我正在尝试编写一个程序,其中我使用数组实现堆栈,并使用它们检查给定字符串是否有平衡括号 例如,如果输入“(()){}[()]”,程序将输出“平衡”,否则如果输入“({})[”程序将输出“不平衡” 这部分是堆栈的数组实现用c语言中的堆栈检查字符串中的平衡圆括号,c,stack,C,Stack,我正在尝试编写一个程序,其中我使用数组实现堆栈,并使用它们检查给定字符串是否有平衡括号 例如,如果输入“(()){}[()]”,程序将输出“平衡”,否则如果输入“({})[”程序将输出“不平衡” 这部分是堆栈的数组实现 #include <stdio.h> #include <stdlib.h> #define MAX 50 int stack[MAX]; int top=-1; void push(char val){ if(top==MAX-1){
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
int stack[MAX];
int top=-1;
void push(char val){
if(top==MAX-1){
printf("stack is already full,error\n");
}else{
top++;
stack[top]=val;
}
}
char pop(){
if(top==-1){
printf("not enough elements,error\n");
exit(1);
}else{
top--;
return stack[top];
}
}
这是主要功能,用户输入一个字符串,然后测试它是否“平衡”
int main(){
char str[MAX];
int flag;
printf("Enter the string with the brackets and etc.\n");
fgets(str, sizeof(str),stdin);
flag=isBalanced(str);
if(flag==1){
printf("Balanced\n");
}
else{
printf("Not balanced\n");
}
return 0;
}
例如,当我输入一个非常简单的例子时,我得到了一个错误的答案
Enter the string with the brackets and etc.
()
Not balanced
这应该是输出“平衡的”。我不明白这是怎么发生的。如果(stack==NULL)
是这里的问题,stack将永远不会为NULL。
您需要通过验证pop()中的top>0
,检查堆栈中是否仍有元素,然后再返回top元素。更改:
top--;
return stack[top];
到
此外,在isBalanced()中,堆栈从不为null,因此请删除:
if(stack==NULL){
return 0;
}
并更改平衡检查以从以下位置查找空堆栈:
if(stack==NULL){
return 1; // balanced parenthesis
}else{
return 0; // not balanced parenthesis
}
致:
在做了这些更改之后,您的代码似乎在我的框中工作。这不是我编写代码的方式,但这是使其工作的最小更改集。您实现了推/弹出组合错误。如果按一个字符
top
将变为0。如果您立即弹出它,它最终将执行top--;返回堆栈[top]
,其计算结果为堆栈[-1]
尝试此推送/弹出:
int top=-1; //idx to be popped next; <0 -> invalid
void push(char val)
{
if(top==MAX-2)
printf("stack is already full,error\n");
else
stack[++top]=val;
}
char pop()
{
if(top<0) return '\0'; //no abort, just return invalid char
return stack[top--];
}
int-top=-1;//下一个要弹出的idx;无效
无效推送(字符值)
{
if(顶部==最大-2)
printf(“堆栈已满,错误\n”);
其他的
堆栈[++top]=val;
}
char pop()
{
如果你的问题的答案已经被满意地回答了,但是作为一个不同的方法的建议,请考虑下面的内容:
由于C源代码中使用的通用存储模块数量很少,因此您可以使用递增-递减计数器轻松跟踪它们对。以下使用结构,typedef
ed tobalanced_s
,该结构封装到一个函数中以简化计算。以下是一个示例实现:
typedef struct {
int paren;
int curly;
int square;
bool bBal
}balanced_s;
balanced_s * balanced_enclosures(const char *source);
int main(void)
{
char in[5000] = {0};//you could improve this by reading file size
//first then creating array sized accordingly
FILE *fp = fopen("C:\\play\\source.c", "r");//using copy of this source to test
if(fp)
{
size_t bytes = fread(in, 1, sizeof(in), fp);
}
balanced_s * b = balanced_enclosures(in);
bool balanced = b->bBal;//If not true, inspect the rest of the
//members to see where the imbalance has occurred.
free(b);
return 0;
}
balanced_s * balanced_enclosures(const char *source)
{
balanced_s *pBal = malloc(sizeof(*pBal));
memset(pBal, 0, sizeof(*pBal));
while(*source)
{
switch(*source) {
case '(':
pBal->paren++;
break;
case ')':
pBal->paren--;
break;
case '{':
pBal->curly++;
break;
case '}':
pBal->curly--;
break;
case '[':
pBal->square++;
break;
case ']':
pBal->square--;
break;
}
source++;
pBal->bBal = (!pBal->paren && !pBal->curly && !pBal->square);
}
return pBal;
}
如果在top==0时调用pop,则将返回堆栈[-1]-我认为这是错误的…If(stack==NULL)
也是错误的…@jmq为什么错误?@user5329483我是否应该使用其他条件检查堆栈是否为空?stack[-1]错误,因为使用负数组索引将导致未定义的行为。数组的有效索引为0-49。
if(top==-1){
return 1; // balanced parenthesis
}else{
return 0; // not balanced parenthesis
}
int top=-1; //idx to be popped next; <0 -> invalid
void push(char val)
{
if(top==MAX-2)
printf("stack is already full,error\n");
else
stack[++top]=val;
}
char pop()
{
if(top<0) return '\0'; //no abort, just return invalid char
return stack[top--];
}
typedef struct {
int paren;
int curly;
int square;
bool bBal
}balanced_s;
balanced_s * balanced_enclosures(const char *source);
int main(void)
{
char in[5000] = {0};//you could improve this by reading file size
//first then creating array sized accordingly
FILE *fp = fopen("C:\\play\\source.c", "r");//using copy of this source to test
if(fp)
{
size_t bytes = fread(in, 1, sizeof(in), fp);
}
balanced_s * b = balanced_enclosures(in);
bool balanced = b->bBal;//If not true, inspect the rest of the
//members to see where the imbalance has occurred.
free(b);
return 0;
}
balanced_s * balanced_enclosures(const char *source)
{
balanced_s *pBal = malloc(sizeof(*pBal));
memset(pBal, 0, sizeof(*pBal));
while(*source)
{
switch(*source) {
case '(':
pBal->paren++;
break;
case ')':
pBal->paren--;
break;
case '{':
pBal->curly++;
break;
case '}':
pBal->curly--;
break;
case '[':
pBal->square++;
break;
case ']':
pBal->square--;
break;
}
source++;
pBal->bBal = (!pBal->paren && !pBal->curly && !pBal->square);
}
return pBal;
}