判定一个字符串中的‘(’和‘)’是否匹配

来源:互联网 发布:网络老虎机骗局 编辑:程序博客网 时间:2024/06/04 17:52
#include<stdio.h> int IsPiPei(char * pString,int pLen){int a[32],i = 0,sum = 0,j = 0;while( * pString != '\0'){if( * pString == '('){a[i] = -1;i++;}else if( * pString == ')'){a[i] = 1;i++;}pString++;}for(j = 0;j < i;j++){sum += a[j];if(sum > 0){return 0;}}if(sum == 0){return 1;}return 0;}void main(){char * pString = "a+b*((a+b)))(";char * pString1 = pString;int pLen = 0;while(* pString != '\0'){pLen++;pString++;}printf("%d",IsPiPei(pString1,pLen));}


注意:当时上机时,也考虑到了会出现)(这种的情况,但当时想到了栈来实现,但觉得栈有点难,就想了简单的方法只求了(和)的个数,如果个数相等就认为是匹配的。其实考虑)(这种情况也可以用上面的方法简单实现。不知是否考虑周全。

 

下面这种方法也可以实现:

#include<stdio.h>#include<string.h>// 判断圆括号是否匹配 int check(char *input, int len) {     char *src = input;     int left = 0;     int right = 0;     int i;     for (i = 0; i < len; i++)     {         if (*src == '(')             left++;         else if (*src == ')')             right++;         src++;         // 左扩号必须在前面         if (right > left)             return 0;     }     if (left == right && left > 0)         return 1;     return 0; } int main(void) {     char str[100] = {0};     int ret;     while (1)     {         scanf("%s", str);         ret = check(str, strlen(str));         printf("%d\n", ret);     }     return 0; }

这样的判断也考虑了右括号在前面,它多考虑了没有()的情况,让其返回0认为不匹配,这倒是无所谓的事。

 

 

下面是用链表栈实现的方式,貌似在上机考试时完成真不容易。

#include<stdio.h>#include<stdlib.h>  //malloc\freetypedef struct node{char ch;node *next;}Linkstack;Linkstack *Setstack(){  //创建空链栈Linkstack *S;S=(Linkstack *)malloc(sizeof(Linkstack));S->next=NULL;return S;}Linkstack *Pushstack(Linkstack *S,char c){  //入栈Linkstack *p;p=(Linkstack *)malloc(sizeof(Linkstack));p->ch=c;p->next=S->next;S->next=p;return S;}Linkstack *Popstack(Linkstack *S){  //出栈Linkstack *p;p=S->next;S->next=p->next;free(p);return S;}char Gettop(Linkstack *S){   //取栈顶数据if(S->next!=NULL)return S->next->ch;elsereturn ' ';}int Judgepair( ){   //判断圆括号是否正确配对Linkstack *p;char c;int sign=1;p=Setstack();printf("请输入算术表达式,并以'#'结束!\n");c=getchar();while(c!='#'){ switch(c){case'(':     //扫描到'('入栈p=Pushstack(p,c);break;case')':     //扫描到')',判断栈顶是否是'('if(Gettop(p)=='(')     //若栈顶是'(',则出栈p=Popstack(p);else   //若栈顶不是'(',则配对错误sign=0;break;}if(sign==0)break;elsec=getchar();}if(p->next!=NULL)  //最后查看栈中是否为空sign=0;return sign;}void Judgeout(int a){   //判断结果输出if(a==1)printf("算术表达式圆括号配对正确!\n");if(a==0)printf("算术表达式圆括号配对错误!\n");}void main(){Judgeout(Judgepair());}


 

 

栈的数组实现:

 


 

#include<stdio.h>#include<stdlib.h>#include<string.h>struct stack;typedef struct stack * pstack;struct stack{int topstack;char stackcon[100];};pstack createstack(){ pstack pstackc = (pstack)malloc(sizeof(stack));pstackc->topstack = -1;memset(pstackc->stackcon,0,100);return pstackc;}void push(pstack pstackc,char con){(pstackc->topstack)++;pstackc->stackcon[pstackc->topstack] = con;}void pop(pstack pstackc){(pstackc->topstack)--;}char gettop(pstack pstackc){return pstackc->stackcon[pstackc->topstack];}int main(){char str[100];int count = 0;scanf("%s",str);char * pstr = str;pstack pstackmain = createstack();while(* pstr){if('(' == (* pstr)){push(pstackmain,(* pstr));count++;}else if(')' == (* pstr)){if('(' == gettop(pstackmain)){pop(pstackmain);}else{return 0;}}pstr++;}if(-1 == pstackmain->topstack){printf("%s %d %d","RIGHT",count,count);}return 0;}