问题是在字符数组中检查括号是否匹配。
在我的代码中,我没有得到任何输出也没有任何错误,所以我无法找到我的错误。
首先,我为堆栈创建了一个结构。然后我编写了检查堆栈是否为空或满的函数,以防止堆栈溢出或下溢等情况。
然后我为圆括号检查器设置了函数
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
//Initializing the stack with structures
struct stack
{
int size;
int top;
char *arr;
};
//function to check whether stack is empty
int isEmpty(struct stack *ptr)
{
if (ptr->top == -1)
{
return 1;
}
else
{
return 0;
}
}
//function to check whether stack is full
int isFull(struct stack *ptr)
{
if (ptr->top == (ptr->size - 1))
{
return 1;
}
else
{
return 0;
}
}
//Function for push
void push(struct stack *ptr, char val)
{
if (isFull(ptr))
{
printf("Stack Overflow, Cannot push more elementsn");
}
else
{
ptr->top++;
ptr->arr[ptr->top] = val;
}
}
//Function for pop
char pop(struct stack *ptr)
{
if (isEmpty(ptr))
{
printf("Stack Underflow, Unable to pop elementsn");
return -1;
}
else
{
char val;
val = ptr->arr[ptr->top];
ptr->top--;
return val;
}
}
//Function for parenthesis matching
int parenthesisChecker(char *exp)
{
struct stack *st;
st->size = 100;
//stack is empty for now
st->top = -1;
st->arr = (char *)malloc(st->size * sizeof(char));
int n_push = 0, n_pop = 0;
for (int i = 0; exp[i] != ' '; i++)
{
//for open bracket push
if (exp[i] == '(')
{
push(st, '(');
n_push++;
}
//for closed bracket pop
else if (exp[i] == ')')
{
if (isEmpty(st))
{
return 0;
}
else
{
pop(st);
n_pop++;
}
}
}
printf("%d times pushn", n_push);
printf("%d times popn", n_pop);
if (isEmpty(st))
{
return 1;
}
else
{
return 0;
}
}
int main()
{
char *c = "akajvd)(()";
if (parenthesisChecker(c))
{
printf("Parenthesis is matched");
}
else
{
printf("Parenthesis is not matched");
}
return 0;
}
函数parenthesisChecker
中的指针st
未初始化,且值不确定
struct stack *st;
对指针进行解引用比如下面这条语句
st->size = 100;
调用未定义行为。
该函数还会产生内存泄漏,因为它没有释放为结构struct stack
的数据成员arr
分配的内存。
函数形参应该有限定符const
,因为传递的字符串在函数内没有被改变。
int parenthesisChecker( const char *exp );
而pop
、push
等函数不应该输出任何消息。是函数的调用方决定是否输出消息。
我将按照下面的方式编写程序。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stack
{
size_t size;
size_t top;
char *arr;
};
struct stack make_stack( size_t size )
{
struct stack st = { .size = size, .top = 0, .arr = malloc( size ) };
if ( st.arr == NULL ) st.size = 0;
return st;
}
void free_stack( struct stack *st )
{
free( st->arr );
st->size = 0;
st->top = 0;
}
int isEmpty( const struct stack *st )
{
return st->top == 0;
}
int isFull( const struct stack *st )
{
return st->top == st->size;
}
int push( struct stack *st, char val )
{
int success = !isFull( st );
if ( success )
{
st->arr[st->top++] = val;
}
return success;
}
int pop( struct stack *st, char *val )
{
int success = !isEmpty( st );
if ( success )
{
*val = st->arr[--st->top];
}
return success;
}
int parenthesisChecker( const char *s )
{
int success = 1;
if ( *s != ' ' )
{
struct stack st = make_stack( strlen( s ) );
success = st.size != 0;
for ( ; success && *s; ++s )
{
if ( *s == '(' )
{
success = push( &st, *s );
}
else if ( *s == ')' )
{
char c;
success = pop( &st, &c );
}
}
success = success && isEmpty( &st );
free_stack( &st );
}
return success;
}
int main(void)
{
const char *s = "akajvd)(()";
if ( parenthesisChecker( s ) )
{
puts( "Parentheses are matched" );
}
else
{
puts( "Parentheses are not matched" );
}
return 0;
}
程序输出为
Parentheses are not matched