array_stack.c 和array_stack.h 参展arraystack文章中的答案。
#include "stack_array.h"
#include <unistd.h>
#include <stdio.h>
int main ()
{
int result = test_parentheses_balance();
if(1 == result)
{
printf("parentheses not balanced\n");
}
else if(0 == result)
{
printf("parentheses balanced\n");
}
else
{
printf("error\n");
}
return 0;
}
int test_parentheses_balance()
{
int result = 0;
char c;
char* pstack = arraystackcreate(10);
while(0 != (result = read(STDIN_FILENO, &c, 1)))
{
if(-1 == result)
{
printf("read stdin error\n");
return -1;
}
if('(' == c || '{' == c || '[' == c)
{
arraystackpush(&pstack, c);
}
else if(')' == c || '}' == c || ']' == c)
{
arraystackpop(&pstack);
}
else
{
continue;
}
}
if(0 != arraystacksize())
{
return 1;
}
else if(0 == arraystacksize())
{
return 0;
}
else
{
return -1;
}
}