括号匹配c语言代码
❶ 判断圆括号是否配对用C语言如何实现
1、设计原理:
主要是利用了栈的结构,在表达式的输入过程中实现对括号是否匹配的判断。根据其括号的原则:小括号之中不能含有大括号或中括号,中括号中不能含有大括号。再由紧密性,左边括号和右边括号是紧密相连的。否则判断为错。 其操作为:每输入一个字符打一下回车,若输入括号顺序错误则跳出,并显示错误!
2、例程:
#include<stdio.h>
#defineMAX100
#defineTRUE1
#defineFALSE0
#defineEa
typedefstructtransition/*建立一个栈*/
{
charsq[MAX];
inttop;
}sqstack;
sqstackbt;
intemptysqstack(sqstackbt)/*判栈空*/
{
if(bt.top==-1)
returnTRUE;
else
returnFALSE;
}
voidpushsqstack(sqstackbt,charsh)/*入栈*/
{
if(bt.top==MAX-1)
{
printf("overflow");
exit(0);
}
bt.top++;
bt.sq[bt.top]=sh;
}
voidpopsqstack(sqstackbt)/*出栈*/
{
intsh;
if(bt.top==-1)
{
printf("empty");
exit(0);
}
sh=bt.sq[bt.top];
bt.top--;
returnsh;
}
Search(sqstackbt)/*查找括号是否匹配*/
{
charc=0;
printf("Ifyouwanttobreak,pleaseinput'a' ");
while(c!=E&&bt.top<=MAX&&c!='('&&c!='['&&c!='{')
{
c=getchar();
pushsqstack(bt,c);
}
SearchA(bt,c);
SearchB(bt,c);
SearchC(bt,c);
}
SearchA(sqstackbt,charc)/*查找小括号是否匹配*/
{
if(c=='(')
{
while(c!=')'&&c!='['&&c!=']'&&c!='{'&&c!='}')
{
c=getchar();
pushsqstack(bt,c);
}
if(c=='(')
printf("right ");
elseif(c=='['||c==']'||c=='{'||c=='}')
printf("wrong ");
}
}
SearchB(sqstackbt,charc)/*查找中括号是否匹配*/
{
if(c=='[')
while(c!=']'&&c!='('&&c!=')'&&c!='{'&&c!='}')
{
c=getchar();
pushsqstack(bt,c);
}
if(c==')'||c=='{'||c=='}')
printf("wrong ");
elseif(c=='(')
SearchA(bt,c);
elseif(c==']')
printf("right ");
else
printf("wrong ");
}
SearchC(sqstackbt,charc)/*查找大括号是否匹配*/
{
if(c=='{')
while(c!='}'&&c!='['&&c!=']'&&c!='('&&c!=')')
{
c=getchar();
pushsqstack(bt,c);
}
if(c==']'||c==')')
printf("wrong ");
elseif(c=='[')
SearchB(bt,c);
elseif(c=='(')
SearchA(bt,c);
elseif(c=='}')
printf("right ");
else
printf("wrong ");
}
main()
{
inti;
bt.top=-1;
i=emptysqstack(bt);
if(i)
{
Search(bt);
}
else
exit(0);
}
❷ 求用C语言做简单的括号匹配程序
头文件:(另存为SeqStack.h)
typedef struct
{
DataType stack[MaxStackSize];
int top;
} SeqStack;
void StackInitiate(SeqStack *S) /*初始化顺序堆栈S*/
{
S->top = 0; /*定义初始栈顶下标值*/
}
int StackNotEmpty(SeqStack S)
/*判顺序堆栈S非空否,非空则返回1,否则返回0*/
{
if(S.top <= 0) return 0;
else return 1;
}
int StackPush(SeqStack *S, DataType x)
/*把数据元素值x压入顺序堆栈S,入栈成功则返回1,否则返回0 */
{
if(S->top >= MaxStackSize)
{
printf("堆栈已满无法插入! \n");
return 0;
}
else
{
S->stack[S->top] = x;
S->top ++;
return 1;
}
}
int StackPop(SeqStack *S, DataType *d)
/*弹出顺序堆栈S的栈顶数据元素值到参数d ,出栈成功则返回1,否则返回0*/
{
if(S->top <= 0)
{
printf("堆栈已空无数据元素出栈! \n");
return 0;
}
else
{
S->top --;
*d = S->stack[S->top];
return 1;
}
}
int StackTop(SeqStack S, DataType *d)
/*取顺序堆栈S的当前栈顶数据元素值到参数d ,成功则返回1,否则返回0*/
{
if(S.top <= 0)
{
printf("堆栈已空! \n");
return 0;
}
else
{
*d = S.stack[S.top - 1];
return 1;
}
}
括号问题
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MaxStackSize 100
typedef char DataType;
#include "SeqStack.h"
void ExpIsCorrect(char exp[], int n)
//判断有n个字符的字符串exp左右括号是否配对正确
{
SeqStack myStack; //定义链式堆栈
int i;
char c;
StackInitiate(&myStack);
for(i = 0; i < n; i++)
{
if((exp[i] == '(') || (exp[i] == '[') || (exp[i] == '{'))
StackPush(&myStack, exp[i]); //入栈
else if(exp[i] == ')' && StackNotEmpty(myStack)
&& StackTop(myStack, &c) && c == '(')
StackPop(&myStack, &c); //出栈
else if(exp[i] == ')' && StackNotEmpty(myStack)
&& StackTop(myStack, &c) && c != '(')
{
printf("左右括号配对次序不正确!\n");
return;
}
else if(exp[i] == ']' && StackNotEmpty(myStack)
&& StackTop(myStack, &c) && c == '[')
StackPop(&myStack, &c); //出栈
else if(exp[i] == ']' && StackNotEmpty(myStack)
&& StackTop(myStack, &c) && c != '[')
{
printf("左右括号配对次序不正确!\n");
return;
}
else if(exp[i] == '}' && StackNotEmpty(myStack)
&& StackTop(myStack, &c) && c == '{')
StackPop(&myStack, &c); //出栈
else if(exp[i] == '}' && StackNotEmpty(myStack)
&& StackTop(myStack, &c) && c != '{')
{
printf("左右括号配对次序不正确!\n");
return;
}
else if(((exp[i] == ')') || (exp[i] == ']') || (exp[i] == '}'))
&& !StackNotEmpty(myStack))
{
printf("右括号多于左括号!\n");
return;
}
}
if(StackNotEmpty(myStack))
printf("左括号多于右括号!\n");
else
printf("左右括号匹配正确!\n");
}
void main(void)
{
char a[] = "(())abc{[)(]}"; //测试例子1。左右括号配对次序不正确
char b[] = "(()))abc{[]}"; //测试例子2。右括号多于左括号
char c[] = "(()()abc{[]}"; //测试例子3。左括号多于右括号
char d[] = "(())abc{[]}"; //测试例子4。左右括号匹配正确
int n1 = strlen(a);
int n2 = strlen(b);
int n3 = strlen(c);
int n4 = strlen(d);
ExpIsCorrect(a, n1);
ExpIsCorrect(b, n2);
ExpIsCorrect(c, n3);
ExpIsCorrect(d, n4);
}
二者放于同一目录下即可