當前位置:首頁 » 編程語言 » c語言棧括弧匹配

c語言棧括弧匹配

發布時間: 2022-10-25 05:27:07

㈠ 如何用c語言實現括弧匹配的問題

先按順序取出所有的括弧.然後循環刪除_相鄰的_差為一或二的_點.最後如果表空則匹配.
單向鏈表:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 80
typedef struct list{
char node;
struct list* next;
}list,*plist;

void iniList(plist);
int isEmpty(plist);
int listAppend(plist,char);
int delBracketsFormList(plist);

int main(int argc,char* argv[]){
char test[LEN];
int i;
list a;
plist p;
p=&a;
iniList(p);
scanf("%80s",test);
for (i=0;i<LEN;i++){
switch(test[i]){
case '[': case']': case'{': case'}': case'(': case')':
listAppend(p,test[i]);
break;
default:continue;
}
}
delBracketsFormList(p);
if (isEmpty(p)){
printf("括弧匹配!\n");
}
else
printf("括弧不配對!\n");
return 0;
}

void iniList(plist aplist){
aplist->next=NULL;
aplist->node='\0';
}

int isEmpty(plist aplist){
return aplist->next==NULL?1:0;
}

int listAppend(plist aplist,char a){
plist bplist=aplist,anode;
while (bplist->next){
bplist=bplist->next;
}
anode=(plist)malloc(sizeof(list));
if (!anode)exit(-1);
anode->node=a;
anode->next=NULL;
bplist->next=anode;
return 0;
}
int delBracketsFormList(plist aplist){
plist temp;
int has=1;
if (isEmpty(aplist))
return 0;
while(has){
has=0;
temp=aplist;
while (temp->next){
if(temp->next->next){
if((temp->next->next->node - temp->next->node == 1)||(temp->next->next->node - temp->next->node == 2)){
temp->next = temp->next->next->next;
has=1;

}
else
temp = temp->next;
}
else
temp =temp->next;
if(!has)break;
}

}
return 0;
}

㈡ 判斷圓括弧是否配對用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語言程序) 有什麼錯誤

#include<stdio.h>
#include<stdlib.h>
#include"iostream.h"
#define MAX 100

typedef char Elem;

typedef struct
{ Elem a[MAX];
int top;
}Sqstack;
Sqstack s1;

void init(Sqstack *s);
Elem pop(Sqstack *s);
void push(Sqstack *s,Elem e);

void init(Sqstack *s)
{
s->top=-1;
}
void push(Sqstack *s ,Elem e)
{
if(s->top==MAX-1)
printf("stack full");
else
{
s->top++;
s->a[s->top]=e;
}
}
Elem pop(Sqstack *s)
{
Elem x;
if(s->top==-1)
{
printf("stack underflow");// x=-1;
}
else
{
x=s->a[s->top];
s->top--;
return x;
}
}

main()
{
Sqstack s;
init(&s);
int j;
char ch;
Elem e,x;
// int top;
Sqstack s1;
printf("請輸入所有的符號,比如{{{【【【()】】】}}}是可以配對的,在比如【{】()}無法配對");
printf("括弧序列如下:\n");
do
{
ch=getchar();
switch(ch)
{
case '(': push(&s,ch); break;
case '[': push(&s,ch); break;
case '{': push(&s,ch); break;
case ')': { if(s.a[s.top]=='(')
pop(&s);
// else
// printf("no"); //return 0;
}
case ']': { if(s.a[s.top]=='[')
pop(&s);
// else
// printf("no"); //return 0;
}
case '}': { if(s.a[s.top]=='{')
pop(&s);
// else
// printf("no") ;//return 0;
}
}
}while(ch!='\n');
// printf("YES");
if(s.top==-1)
printf("配對成功");
else
printf("不成功");
}
已經調試成功,我覺得你的演算法好像有問題,我按照我自己的意思理解的,不曉得是不是對的...

㈣ C語言:表達式括弧匹配檢驗(壓棧,出棧)

演算法提示:
1)凡出現左括弧,則進棧;
2)凡出現右括弧,首先檢查棧是否空
若棧空,則表明該「右括弧」多餘,
否則和棧頂元素比較,
若相匹配,則「左括弧出棧」

否則表明不匹配。
3)表達式檢驗結束時,
若棧空,則表明表達式中匹配正確,
否則表明「左括弧」有餘。

㈤ 括弧匹配問題,要求用C語言完成,下面是源代碼,求大神解決...

你這一題其實就是要用棧的思路去解決,定義一個char數組作為棧去存儲差不多就可以了
具體代碼如下
char cs[200]; //定義字元串棧
memset(cs, 0x00 ,sizeof(cs)); //初始化棧
int count=0; //count就是合法長度的計數
int p=-1; //p就是指向棧頂的數,當p=-1時表示棧為空

int len=strlen(inp); //獲取輸入字元串的長度
if (len == 0) //當長度是0時表示是空字元串,則計數count是0
count=0;
else for (int i=0;i<len;i++)
{
if ((p==-1)&&((inp[i] == ')')||(inp[i] == ']')))
continue;
else if ((inp[i] == '[')||(inp[i] == '('))
{
p++;
cs[p]=inp[i];
}
else if ((inp[i] == ']')&&(cs[p] == '['))
{
p-=1;
count+=2;
}
else if ((cs[p] == '(')&&(inp[i] == ')'))
{
p-=1;
count+=2;
}
}

㈥ c語言括弧匹配問題(棧) 我寫的程序感覺有點錯 找不到原因

大概有這3個問題:

  1. 資源沒有清理

  2. 部分邏輯錯誤

  3. 棧操作錯誤

帶注釋修改如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

structNode
{
charfuhao;
structNode*next;
};
structS
{
structNode*top;
structNode*bottom;
};

voidstart(structS*s);
voidclean(structS*s);
voidend(structS**s);

charyulan(structS*s);
voidruzhan(structS*s,chara);
charchuzhan(structS*s);
intpanan(structS*s);

intmatch(chara,charb);

intmain()
{
intn,a,i;
chararr[256],in,out;
scanf("%d",&n);
fflush(stdin);//清空輸入緩存
structS*s=(structS*)malloc(sizeof(structS));
start(s);//創建第一個node(bottom),可以放在while外面
while(n>0)
{
intonlyr=0;//如果表達式消除後只剩右*符號,則設為1
gets(arr);
a=strlen(arr);
for(i=0;i<a;i++)
{
charfh=arr[i];
if(fh=='('||fh=='['||fh=='{')
{
in=fh;
ruzhan(s,in);
}
elseif(fh==')'||fh==']'||fh=='}')
{
out=yulan(s);
if(out=='')
onlyr=1;
if(!match(out,fh))
break;
else
chuzhan(s);
}
}
if(panan(s)&&!onlyr)
printf("YES ");
else
printf("NO ");

clean(s);
n--;
}
end(&s);
return0;
}

//預覽top,s->top在end之前都會有效
charyulan(structS*s)
{
if(s->top)
returns->top->fuhao;
return'';
}

intmatch(chara,charb)
{
return((a=='('&&b==')')||
(a=='['&&b==']')||
(a=='{'&&b=='}'));
}

//這個棧在end之前始終有個node,bottom始終指向這個node
voidstart(structS*s)
{
structNode*p=(structNode*)malloc(sizeof(structNode));
p->fuhao='';
p->next=0;

s->top=p;
s->bottom=p;
}

//只保留bottom,清空其他node
voidclean(structS*s)
{
structNode*p=s->top;
while(p&&p!=s->bottom){
structNode*q=p->next;
free(p);
p=q;
}
//除了bottom其他都已經清除了,把top指針指向bottom
s->top=s->bottom;
}

//全部清除,包括s棧本身
voidend(structS**s)
{
clean(*s);
free((*s)->bottom);
free(*s);
*s=0;
}

voidruzhan(structS*s,chara)
{
structNode*p;
p=(structNode*)malloc(sizeof(structNode));
p->next=s->top;
p->fuhao=a;

s->top=p;
}

charchuzhan(structS*s)
{
//空棧直接返回''
if(s->top==s->bottom){
return'';
}
charc;
structNode*p;
p=s->top;
c=s->top->fuhao;

s->top=s->top->next;

free(p);
returnc;
}

intpanan(structS*s)
{
return(s->top==s->bottom);
}

㈦ 用棧檢測括弧匹配

#include <stdio.h>
#include <string.h>

#define MaxSize 100

typedef char ElemType;//定義數據類型

//定義順序棧
typedef struct
{
ElemType data[MaxSize];//數據域
int top;//棧頂指針
}SeqStack;

//棧初始化
int InitStack(SeqStack *s)
{
s->top=-1;//初始化棧頂,指向空
return 1;
}

//入棧
int Push(SeqStack *s,ElemType x)
{
if (s->top == MaxSize -1 )
{
printf("棧已滿,不能入棧.\n");
return 0;
}
else
{
s->top++;//棧頂指針上移
s->data[s->top] = x;//數據元素入棧
}
return 1;
}

//出棧
int Pop(SeqStack *s,ElemType *x)
{
if (s->top == -1)
{
printf("棧為空,不能出棧.\n");
return 0;
}
else
{
*x=s->data[s->top];//取出棧頂元素值
s->top--;//棧頂指針下移
}
return 1;
}

//取棧頂值
int GetTop(SeqStack *s,ElemType *x)
{
if (s->top == -1)
{
printf("棧為空,不能取值.\n");
return 0;
}
else
{
*x=s->data[s->top];//取出棧頂元素值
}
return 1;
}

//判斷棧是否為空
int IsEmpty(SeqStack *s)
{
if(s->top==-1)
return 1;

return 0;
}

int Check(char str[],int len)
{
int i;
int flag=1;//合法標志0-不合法 1-合法
int exist=0;//小括弧存在標志 0-不在 1-在
ElemType x;
SeqStack s;//棧s

//棧初始化
InitStack(&s);

for(i=0;i<len;i++)
{
if(str[i]=='{')//大括弧{
{
if(IsEmpty(&s))//棧空,直接入棧
{
if(Push(&s,str[i])!=1)
{
flag=0;
break;
}
}
else//棧非空,判斷合法性
{
if(GetTop(&s,&x)!=1)//取棧頂值
{
flag=0;
break;
}
if(x=='{' || x=='[' || x== '(')//如果是{,[或者(,非法
{
flag=0;
break;
}
else//否則入棧
{
if(Push(&s,str[i])!=1)
{
flag=0;
break;
}
}
}
}
else if(str[i]=='[')//方括弧[
{
if(IsEmpty(&s))//棧空,直接入棧
{
if(Push(&s,str[i])!=1)
{
flag=0;
break;
}
}
else//棧非空,判斷合法性
{
if(GetTop(&s,&x)!=1)//取棧頂值
{
flag=0;
break;
}
if(x=='[' || x== '(')//如果是[或者(,非法
{
flag=0;
break;
}
else//否則入棧
{
if(Push(&s,str[i])!=1)
{
flag=0;
break;
}
}
}
}
else if(str[i]=='(')//小括弧(
{
//直接入棧
if(Push(&s,str[i])!=1)
{
flag=0;
break;
}
exist=1;//小括弧存在
}
else if(str[i]==')')//小括弧)
{
if(Pop(&s,&x)!=1)
{
flag=0;
break;
}
if(x!='(')//如果出棧非(,非法
{
flag=0;
break;
}
}
else if(str[i]==']')//方括弧]
{
if(Pop(&s,&x)!=1)
{
flag=0;
break;
}
if(x!='[')//如果出棧非[,非法
{
flag=0;
break;
}
if(exist==0)//小括弧不存在,單獨[],非法
{
flag=0;
break;
}
}
else if(str[i]=='}')//大括弧}
{
if(Pop(&s,&x)!=1)
{
flag=0;
break;
}
if(x!='{')//如果出棧非{,非法
{
flag=0;
break;
}
if(exist==0)//小括弧不存在,單獨[],非法
{
flag=0;
break;
}
}
else//其它字元跳過
continue;
}

if(!IsEmpty(&s))//循環完畢,棧非空,非法
flag=0;

return flag;

}
//主函數
int main(void)
{
char str[MaxSize];//錄入字元串
int i,len;

while(1)
{
printf("輸入字元串\n");
gets(str);
len=strlen(str);
if(len>=MaxSize)//超過定義長度
return 1;

if(Check(str,len)==1)
printf("匹配合法!\n");
else
printf("不合法\n");

printf("是否繼續? 1-是,0否 :");
if(scanf("%d",&i)!=1 || i!=1)
break;
fflush(stdin);
}

return 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);
}

二者放於同一目錄下即可

㈨ 括弧匹配檢驗(c語言)

函數返回值類型不能是Status,Status只是泛指類型,至於具體用什麼類型,你應該根據實際情況而定。
比如你的第一個函數Status InitStack(SqStack &S) ,
可以改為int InitStack(SqStack &S) ,其它的你自己根據情況定了。

㈩ c語言的括弧匹配問題

#include <stdio.h>

#include <stdlib.h>

int main()

{

int i,count;

char ch[10001],ch1[10001];

while(gets(ch)!=NULL)

{

count=-1;

for(i=0;ch[i]!='';i++)

{

if(ch[i]=='('||ch[i]=='['||ch[i]=='<'||ch[i]=='{')

{

ch1[++count]=ch[i];

}

else

{

if(ch[i]-ch1[count]<3)

{

count--;

}

else

{

break;

}

}

}

if(count==-1)

printf("YES ");

else

printf("NO ");

}

return 0;

}

熱點內容
字體android 發布:2025-07-12 21:30:38 瀏覽:621
資料庫中包含 發布:2025-07-12 21:25:08 瀏覽:621
艦娘緩存系統 發布:2025-07-12 21:21:21 瀏覽:100
cpu對存儲器的讀寫 發布:2025-07-12 21:21:14 瀏覽:772
如何建立一個網站需要伺服器 發布:2025-07-12 21:18:40 瀏覽:67
php登陸微信 發布:2025-07-12 21:17:55 瀏覽:14
公眾伺服器有什麼功能 發布:2025-07-12 21:11:22 瀏覽:715
健身的壓縮衣 發布:2025-07-12 21:11:12 瀏覽:754
磁碟伺服器如何管理磁碟 發布:2025-07-12 21:02:19 瀏覽:470
安卓返回鍵在哪裡取消 發布:2025-07-12 20:50:17 瀏覽:799