第五周项目三

来源:互联网 发布:李强强 php 编辑:程序博客网 时间:2024/05/17 08:19
  1. 烟台大学计算机学院   
  2.   
  3. 作者:王雪行
  4.   
  5. 问题描述:判断表达式中的各种左括号是否与右括号匹配 
  6.  
  7. 输入描述:表达式 
  8.  
  9. 输出描述:是否配对正确。 
  10.  
  11. 用到了stlist.h算法库 
  12.   
  13. */   
  14.   
  15.   
  16.   
  17. #include <stdio.h>  
  18. #include "stlist.h"  
  19. int main()  
  20. {  
  21.     char c;//出栈用到  
  22.     char st[50];  
  23.     int d=1, i;//d用来记录是否配对  
  24.   SqStack *s;  
  25.     InitStack(s);  
  26.     printf("请输入表达式:");  
  27.     scanf("%s", st);  
  28.     for(i=0; st[i]!='\0'&&d; i++)//读表达式符号  
  29.     {  
  30.         switch(st[i])  
  31.         {  
  32.         case'(':  
  33.         case'[':  
  34.         case'{':  
  35.             Push(s, st[i]);//符号为左括号入栈  
  36.             break;  
  37.         case')'://为右括号出栈比较  
  38.             Pop(s, c);  
  39.             if(c!='(') d=0;  
  40.             break;  
  41.         case']':  
  42.             Pop(s, c);  
  43.             if(c!='[') d=0;  
  44.             break;  
  45.         case'}':  
  46.             Pop(s,c);  
  47.             if(c!='{') d=0;  
  48.             break;  
  49.         }  
  50.     }  
  51.     if(StackEmpty(s)&&d==1)  
  52.         printf("配对正确!!\n");  
  53.     else  
  54.         printf("配对错误!!\n");  
  55.     return 0;  
  56. }  
原创粉丝点击