Chinaunix首页 | 论坛 | 博客
  • 博客访问: 232475
  • 博文数量: 35
  • 博客积分: 659
  • 博客等级: 上士
  • 技术积分: 357
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-01 21:16
文章分类
文章存档

2012年(12)

2011年(23)

分类: C/C++

2011-09-15 20:47:48

  1. 用链栈实现字符匹配.

  2. /*----------------------------括号匹配.c-----------------------------
  3.     
  4.   Program Description:利用栈来判断括号是否匹配
  5.   Date:2011.9.14

  6.  --------------------------------------------------------------------*/

  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>

  10. #define TRUE 1
  11. #define FALSE 0
  12. #define N 100


  13. /*定义数据结构体*/
  14. typedef struct elem
  15. {
  16.     char c;
  17. }Element;

  18. /*定义链表栈*/
  19. typedef struct node
  20. {
  21.     Element data;
  22.     struct node *next;
  23. }LinkStackNode;
  24. typedef LinkStackNode *LinkStack;


  25. int InitStack(LinkStack *top);
  26. int Push(LinkStack top, char element);
  27. int Pop(LinkStack top, char *element);
  28. int GetTop(LinkStack top, char *element);
  29. int match(char element_l, char element_r);
  30. int IsEmpty(LinkStack top);

  31. int main(void)
  32. {
  33.     int i = 0;
  34.     int lenth = 0;
  35.     char str[N];
  36.     char ch;
  37.     LinkStack top;
  38.     
  39.     if (FALSE == InitStack(&top))
  40.     {
  41.         return FALSE;
  42.     }

  43.     printf("请输入括号:\n");
  44.     scanf("%s", str);

  45.     lenth = strlen(str);
  46.     for (i = 0; i < lenth; i++)
  47.     {
  48.         switch (str[i])
  49.         {
  50.             case '(':
  51.             case '[':
  52.             case '{':
  53.                 Push(top, str[i]);
  54.                 break;
  55.             case ')':
  56.             case ']':
  57.             case '}':
  58.                 if (FALSE == IsEmpty(top))
  59.                 {
  60.                     printf("右括号多余\n");
  61.                     exit(0);
  62.                 }
  63.                 else
  64.                 {
  65.                     GetTop(top, &ch);
  66.                     if (TRUE == match(ch, str[i]) )
  67.                     {
  68.                         Pop(top, &ch);
  69.                     }
  70.                     else
  71.                     {
  72.                         printf("对应的左右括号不匹配\n");
  73.                         exit(FALSE);
  74.                     }
  75.                 }/*if*/
  76.                 break;
  77.             default:
  78.                 exit(FALSE);
  79.         }/*switch*/
  80.     }/*for*/

  81.     if (FALSE == IsEmpty(top))
  82.     {
  83.         printf("括号匹配\n");
  84.     }
  85.     else
  86.     {
  87.         printf("左括号多余\n");
  88.         exit(FALSE);    
  89.     }

  90.     return 0;
  91. }

  92. int InitStack(LinkStack *top)
  93. {

  94.     *top = (LinkStack)malloc(sizeof(LinkStackNode));
  95.     if (NULL == top)
  96.     {
  97.         return FALSE;
  98.     }

  99.     (*top)->next = NULL;
  100.     
  101.     return TRUE;
  102. }

  103. int Push(LinkStack top, char element)
  104. {    
  105.     LinkStack temp;

  106.     temp = (LinkStack)malloc(sizeof(LinkStackNode));
  107.     if (NULL == temp)
  108.     {
  109.         return FALSE;
  110.     }
  111.     
  112.     temp->data.c = element;

  113.     temp->next = top->next; //头插法
  114.     top->next = temp;
  115. //    top = top->next;     //top不需要改变,一直指向头指针.

  116.     return TRUE;
  117. }

  118. int Pop(LinkStack top, char *element)
  119. {
  120.     LinkStack temp;

  121.     if (NULL == top->next)
  122.     {
  123.         return FALSE;
  124.     }

  125.     temp = top->next;
  126.     *element = temp->data.c;
  127.     top->next = temp->next;

  128.     free(temp);

  129.     return TRUE;
  130. }

  131. int GetTop(LinkStack top, char *element)
  132. {
  133.     LinkStack temp;

  134.     if (NULL == top->next)
  135.     {
  136.         return FALSE;
  137.     }

  138.     temp = top->next;
  139.     *element = temp->data.c;

  140.     return TRUE;
  141. }

  142. int IsEmpty(LinkStack top)
  143. {
  144.     if (NULL == top->next)
  145.     {
  146.         return FALSE;
  147.     }
  148.     else
  149.     {
  150.         return TRUE;
  151.     }
  152. }

  153. int match(char element_l, char element_r)
  154. {
  155.     if (element_l == '(')
  156.     {
  157.         if (element_r == ')')
  158.         {
  159.             return TRUE;
  160.         }
  161.         else
  162.         {
  163.             return FALSE;
  164.         }
  165.     }
  166.     else if (element_l == '[')
  167.     {
  168.         if (element_r == ']')
  169.         {
  170.             return TRUE;
  171.         }
  172.         else
  173.         {
  174.             return FALSE;
  175.         }
  176.     }
  177.     else if (element_l == '{')
  178.     {
  179.         if (element_r == '}')
  180.         {
  181.             return TRUE;
  182.         }
  183.         else
  184.         {
  185.             return FALSE;
  186.         }
  187.     }
  188.     else
  189.     {
  190.         exit(FALSE);
  191.     }
  192.     
  193. }
阅读(3536) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~