python代数式括号有效性检验示例代码

时间:2022-09-15 22:02:19

思路:

利用栈实现代数式中括号有效行的的检验:

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
class mychain(object): #利用链表建立栈,链表为父类
 length=0
 def __init__(self,value=None,next=None):#创建链表,长度并不包含头部
  self.value=value
  self.next=next
  #mychain.length=mychain.length+1
 def append(self,value=None):
  while self.next!=None:
   self=self.next
  self.next=mychain(value)
  mychain.length=mychain.length+1 #追加时,链表长度增加
 def travle(self):#遍历链表
  print(self.value)
  if self.next!=None:
   self.next.travle()
 def drop (self,value):#删除特定值的第一个匹配节点
  while self.next!=None:
   if self.next.value!=value:
    self=self.next
   else:
    self.next=self.next.next
    mychain.length=mychain.length-1 #删除时,链表长度减小
    break
 def pop(self):#删除未节点
  if self.next!=None:#并不删除头结点
   while self.next.next!=None:
    self=self.next
   self.next=None
   mychain.length=mychain.length-1#弹出为节点,并减小长度,头结点不弹出
 
 
 
class stock(mychain):#栈类
 bottom=None   #栈底
 top=None     #栈顶
 n_count=0    #计数
 def Max(self):  #*最大值
  if self.next!=None:
   tmp = self.next.value
   while self.next.next!=None:
    self=self.next
    if self.next.value>tmp:
     tmp=self.next.value
   return tmp
  else:
   print('栈为空!')
 def Min(self):#栈中的最小值
  if self.next!=None:
   tmp = self.next.value
   while self.next.next!=None:
    self=self.next
    if self.next.value<tmp:
     tmp=self.next.value
   return tmp
  else:
   print('栈为空!')
 
 def push(self,value): #压栈
  while self.next != None:
   self = self.next
  self.next = mychain(value)
  stock.top=self.next
  stock.length=stock.length+1
  stock.n_count=stock.n_count+1
 def __init__(self,value='',next=None):
  self.value=value
  self.next=next
  stock.bottom=self
  stock.top=self
  #stock.n_count=stock.n_count+1
  #stock.length=stock.length+1
 def append(self,value=''):#取消追加函数
  print('请使用Push()!')
 def pop(self):
  if self.next!=None:#并不删除头结点
   while self.next.next!=None:
    self=self.next
   self.next=None
   stock.top=self
   stock.length=stock.length-1#弹出为节点,并减小长度,头结点不弹出
class solution(object):
 def validationofbrackets(self,astr=''):#检验串中的括号合法性
  braketsstock=stock()
  for i in astr:
   if i in ['{','(','[']:
    braketsstock.push(i)
   else:
    if i==')':
     if braketsstock.top.value=='(':
      braketsstock.pop()
     else:
      return False
    elif i==']':
     if braketsstock.top.value=='[':
      braketsstock.pop()
     else:
      return False
    elif i=='}':
     if braketsstock.top.value=='{':
      braketsstock.pop()
     else:
      return False
    else:
     pass
  print(astr)
  print(braketsstock.length)
  if braketsstock.length==0:
   return True
  else:
   return False

运行:

?
1
2
3
bstr='([{((({{}})))}]){{}}{{}{}{}[][]()(123)(((sin5)))}'
f=solution()
print(f.validationofbrackets(bstr))

总结

到此这篇关于python代数式括号有效性检验的文章就介绍到这了,更多相关python代数式括号有效性检验内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/wjqsdwm/p/13765413.html