码迷,mamicode.com
首页 > 其他好文 > 详细

栈的简单应用1-平衡符号

时间:2014-11-21 01:15:15      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   使用   sp   on   

因为在编程中,使用{}()[]等都是成对出现的,因此可以使用栈来进行一些匹配判断。

原理:

读取一段字符串,如果遇到{([这些开放字符,就将其压入栈中,如果读到})]这些封闭字符,就与当前栈顶的符号进行比较,如果栈顶的符号正好是其对应的开放符号,将栈顶元素弹出,继续读取。否则,出现警告。但是如果弹出栈顶后新的栈顶是与其匹配的开放符号,那么就是它出错了。如果全部字符串读取完毕但是栈不是空的的话,那么栈剩下的字符串都缺少匹配。

简单代码,因为使用的输入流问题对程序实现有较大误差,但思路没有问题,需要在C下修改使用,因为时间关系今天就结束这个问题。此代码没有实现对于全部字符读取完毕的检测,还有一些严重问题没有处理,所以此程序应该是失败的。请读者自己根据实际实现。

main.cpp

 1 #include "stack.h"
 2 
 3 char matchSymbal(char s)
 4 {
 5     if (s == ))
 6         return (;
 7     if (s == })
 8         return {;
 9     if (s == ])
10         return [;
11     return  ;
12 }
13 
14 int main(void)
15 {
16 
17     stack *symbal = createStack(10);
18     char s;
19     char tmp;
20     while (std::cin>>s)
21     {
22         if (s == { || s == ( || s == [)
23         {
24             push(symbal, s);
25             continue;
26         }
27         if (s == } || s == ) || s == ])
28         {
29             if (isEmpty(symbal))
30             {
31                 log("空表错误");
32             }
33             else
34             {
35                 tmp = top(symbal);
36                 if (top(symbal) == matchSymbal(s))
37                 {
38                     pop(symbal);
39                     continue;
40                 }
41                 else
42                 {
43                     pop(symbal);
44                     if (top(symbal) == matchSymbal(s))
45                     {
46                         std::cout << "" << tmp << " 处发生错误" << std::endl;
47                         continue;
48                     }
49                     std::cout << "" << s << " 处发生错误" << std::endl;
50                 }
51             }
52         }
53     }
54     system("pause");
55     return 0;
56 }

 

stack.h

 1 /*数组栈*/
 2 #include "iostream"
 3 #include "stdlib.h"
 4 
 5 #define  minSize 5
 6 #define  emptyStack -1
 7 #define  fullStack -2
 8 #define log(s); std::cout<<s<<std::endl;
 9 
10 typedef struct _stack_
11 {
12     int capacity;
13     int topOfStack;
14     char *Array;
15 }stack;
16 
17 stack *createStack(int maxSize)
18 {
19     stack *s;
20     if (maxSize < minSize)
21     {
22         //log("Stack size is too small");
23         return nullptr;
24     }
25     s = (stack *)malloc(sizeof(stack));
26     s->Array = (char *)malloc(sizeof(char) * maxSize);
27     s->capacity = maxSize;
28     s->topOfStack = emptyStack;/*初始化为空栈*/
29     return s;
30 }
31 
32 int isFull(stack *s)/*检测是否为满栈*/
33 {
34     if (s == nullptr)
35     {
36         //log("the stack has not inital");
37         return fullStack;
38     }
39     return s->topOfStack == s->capacity;
40 }
41 
42 int isEmpty(stack *s)/*是否为空栈*/
43 {
44     if (s == nullptr)
45     {
46         //log("the stack has not inital");
47         return emptyStack;
48     }
49     return s->topOfStack == emptyStack;
50 }
51 
52 void push(stack *s, char data)/*压栈*/
53 {
54     if (isFull(s))
55     {
56         //log("Full of Stack");
57         return;
58     }
59     ++s->topOfStack;
60     s->Array[s->topOfStack] = data;
61 }
62 
63 void pop(stack *s)/*弹出栈*/
64 {
65     if (isEmpty(s))
66     {
67         //log("Out of Stack");
68         return;
69     }
70     --s->topOfStack;
71 }
72 
73 char top(stack *s)/*访问栈顶元素*/
74 {
75     if (isEmpty(s))
76     {
77         //std::cout << "Out of Stack,Code is ";
78         return emptyStack;
79     }
80     return s->Array[s->topOfStack];
81 }
82 
83 void makeEmpty(stack *&s)/*置空栈*/
84 {
85     free(s->Array);
86     free(s);
87     s = nullptr;
88 }

 

抱歉。

以上。

栈的简单应用1-平衡符号

标签:style   blog   io   ar   color   os   使用   sp   on   

原文地址:http://www.cnblogs.com/lhyz/p/4111804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!