括弧匹配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);
}
二者放於同一目錄下即可