Created
February 27, 2014 18:19
-
-
Save jfrobbins/9255700 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; balancedparentheses.pb | |
| ; Program To check For balanced parentheses in an expression using stack | |
| ; To run you only need PureBasic 5.21 LTS installed | |
| ; | |
| ; To exectue: | |
| ; compile w/ PB and run | |
| ; | |
| ; modified from the code here: http://www.lifengadget.com/lifengadget/program-To-check-For-balanced-parentheses-in-an-expression/ | |
| ;;;;;;;;;; | |
| Global Window_0 | |
| Global edtExpression, bnAnalyze, canvasResult | |
| Procedure OpenWindow_0(x = 0, y = 0, width = 590, height = 400) | |
| Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered) | |
| edtExpression = EditorGadget(#PB_Any, 10, 10, 570, 330) | |
| bnAnalyze = ButtonGadget(#PB_Any, 240, 350, 120, 40, "Analyze") | |
| canvasResult = CanvasGadget(#PB_Any, 380, 350, 200, 40) | |
| EndProcedure | |
| Procedure.i bracketsame(opening.c, closing.c) ; function checks If opening And closing brackets are same | |
| If(opening = '(' And closing = ')') | |
| ProcedureReturn #True | |
| ElseIf(opening = '{' And closing = '}') | |
| ProcedureReturn #True | |
| ElseIf(opening = '[' And closing = ']') | |
| ProcedureReturn #True | |
| EndIf | |
| ProcedureReturn #False | |
| EndProcedure | |
| Procedure.i balancedparantheses(exp.s) ;checks If parentheses are balanced Or Not | |
| Protected NewList stack.c() | |
| Protected *c.Character | |
| For *c = @exp To (@exp + (Len(exp) * SizeOf(Character))) Step SizeOf(Character) | |
| If (*c\c = '(' Or *c\c = '{' Or *c\c = '[') | |
| AddElement(stack()) | |
| stack() = *c\c | |
| PushListPosition(stack()) | |
| ElseIf(*c\c = ')' Or *c\c = '}' Or *c\c = ']') | |
| If(ListSize(stack()) = 0 Or Not bracketsame(stack(),*c\c)): | |
| ProcedureReturn #False | |
| Else | |
| PopListPosition(stack()) | |
| DeleteElement(stack()) | |
| EndIf | |
| EndIf | |
| Next *c | |
| If ListSize(stack()) = 0 | |
| ProcedureReturn #True | |
| Else | |
| ProcedureReturn #False | |
| EndIf | |
| EndProcedure | |
| Procedure showStatus(status.i = -1) | |
| Protected cWidth = GadgetWidth(canvasResult) | |
| Protected cHeight = GadgetHeight(canvasResult) | |
| Protected buffer.i = 2 | |
| Protected color | |
| Protected text.s | |
| If status < 0 | |
| color = RGB(240,240,240) ;grayish | |
| ElseIf status = #True | |
| color = RGB(0, 255, 0) | |
| text = "balanced" | |
| ElseIf status = #False | |
| color = RGB(255, 0, 0) | |
| text = "UNbalanced" | |
| EndIf | |
| If StartDrawing(CanvasOutput(canvasResult)) | |
| Box(buffer,buffer,cWidth - (buffer * 2), cHeight - (buffer * 2), color) | |
| DrawText(cWidth / 2 - 3, cHeight/2, text) | |
| StopDrawing() | |
| EndIf | |
| EndProcedure | |
| Procedure Window_0_Events(event) | |
| Select event | |
| Case #PB_Event_CloseWindow | |
| ProcedureReturn #False | |
| Case #PB_Event_Menu | |
| Select EventMenu() | |
| EndSelect | |
| Case #PB_Event_Gadget | |
| Select EventGadget() | |
| Case bnAnalyze | |
| showStatus(-1) ;clear | |
| If balancedparantheses(GetGadgetText(edtExpression)) | |
| showStatus(#True) | |
| Debug "true" | |
| Else | |
| showStatus(#False) | |
| Debug "false" | |
| EndIf | |
| EndSelect | |
| EndSelect | |
| ProcedureReturn #True | |
| EndProcedure | |
| OpenWindow_0() | |
| Repeat | |
| Until Window_0_Events(WaitWindowEvent()) = #False | |
| End |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment