括号匹配的练习时不时会在习题中遇到。为啥我没有用堆栈,说白了是没有把堆栈弄透彻,不太会用。去编写这个题其实当时是觉得有点没退路。
和公司某developer吹牛,说自己在学习C语言,经常做习题,然后把当时眼前看到的括号匹配问题发给了他,还在那里吹思路。对方一句用堆栈啊,我有点懵。于是很莽撞地回说可以不用的。接下来的问题嘛,就不需要多费唇舌了。Code拿出来就知有没有。
为了证明自己还是有半把刷子的,当天我就开始编写。而一直到隔天的晚上才完成了以下这个简陋的程序。过程中被那位大哥测试出了很多bug。现在想想都觉得不好意思。
#include <stdio.h>
#define MAX 1000
char input[MAX];
int pair (char array[], int max);
int main()
{
int sign = 0;
extern char input[MAX];
gets(input);
sign = pair (input,MAX);
if (sign == 0)
printf("They are in pairs.\n");
else if (sign == 1)
printf ("They are not in pairs.\n");
else
printf("Error?\n");
return 0;
}
int pair (char array[], int max)
{
int brace = 0;
int i = 0;
int b = 0;
int flag = 0; //Set flag to 0, if braces are in pairs;
for (i = 0; i < max; i++)
{
if (array[i] =='(')
{
brace++;
for ( b = b + i + 1; b < max; b++) //Under if condition, the loop goes further to find ')', then do brace-- and replace the ')' with '0';
{
if (array[b] == ')')
{
brace--;
array[b] = '0'; //Do the replacement here;
break;
}
else
continue;
}
}
else if ( array[i] == ')') // Since the above loop will replace the first ')' it meets and breaks, we should not encounter')' here;
{
brace--;
break;
}
else
continue;
}
printf("brace = %d\n", brace); //Test the brace value;
if (brace != 0 ) //If braces are in pairs, the integer brace should be 0 in the end;
flag = 1;
return flag;
}
后来去看了堆栈思路的编写,才知道啥是强大。没见过世面就是这样。附上别人的堆栈代码。
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define STACK_INIT_SIZE 10
#define STACK_GROW_SIZE 5
#define ELEMTYPE char
#define OK 1
#define ERROR 0
typedef struct {
char * base;
char * top;
int stacksize;
} Stack;
int InitStack(Stack *s) { /*Initialize the stack*/
s->base=((char *) malloc(STACK_INIT_SIZE*sizeof(char)));
if (!s->base) /*cannot apply for the stack*/
return ERROR;
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
}
int StackEmpty(Stack *s) {
if (s->top==s->base)
return OK;
else
return ERROR;
}
int Push(Stack *s,char e){
if (s->top-s->base>=s->stacksize) {
s->base=((char *)realloc(s->base,(s->stacksize+STACK_GROW_SIZE)*sizeof(char)));
if (!s->base)
return ERROR;
s->stacksize+=STACK_GROW_SIZE;
s->top=s->base+s->stacksize; /*top的地址就是通过base的地址加上stacksize的地址得来的*/
}
*s->top++=e;
return OK;
}
int Pop(Stack *s, char *e) {
if (StackEmpty(s))
return ERROR;
*e=*(--s->top);
return OK;
}
int Comp(char a,char b) {
if ((a=='('&&b!=')')
||(a=='['&&b!=']')
||(a=='{'&&b!='}')) {
return ERROR;
}
else
return OK;
}
int Count(Stack *s) {
char e[STACK_INIT_SIZE*2];
char e1;
int i;
InitStack(s);
printf("Please enter the content:\n");
fgets(e,STACK_INIT_SIZE*2,stdin);
if ('\n'==e[strlen(e)-1])
e[strlen(e)-1]=0;
printf("%s\n",e);
for (i=0;e[i]!='\0';i++) {
switch (e[i]) {
case '(':
case '[':
case '{':
Push(s,e[i]);
break;
case ')':
case ']':
case '}':
if (StackEmpty(s)) {
printf("%*s↖右括号多余\n",i+1,"");
return(ERROR);
}
else
Pop(s,&e1);
if (!Comp(e1,e[i])) {
printf("%*s↖左右匹配出错\n",i+1,"");
return(ERROR);
}
}
}
if (!StackEmpty(s)) {
printf("%*s↖左括号多余\n",i,"");
return(ERROR);
}
else {
printf("匹配正确\n");
return(OK);
}
}
int main()
{
Stack s;
Count(&s);
free(s.base);
return 0;
}
它是帮助我理解堆栈原理入门的代码。特此记录。