Entry
Microsoft: .NET: Visual Studio: Visual Basic .net: Simple: How to convert infix to suffix? [Bracket]
Jan 4th, 2004 10:00
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 04 January 2004 - 08:53 am --------------------
Microsoft: .NET: Visual Studio: Visual Basic .net: Simple: How to
convert infix to suffix? [Bracket]
---
Steps: Overview:
1. -Create a new Visual Basic application in Visual Basic.NET
1. Choose for example a console application
2. -Fill in the following code
--- cut here ---------------------------------------------------------
Sub Main()
Dim expressionS As String = "((2*3)^6+(4*5)+(6*7))^7"
Dim tokenS As String = "#-
+*/^abcdefghijklmnopqrstuvwxyz0123456789()"
Dim tableinputS As String
= "03344599999999999999999999999999999999999992"
Dim tablestackS As String
= "03344599999999999999999999999999999999999929"
Dim resultS As String = ""
Dim kstackS As String = ""
Dim kexpressionS As String = ""
Dim stackS As String = "#"
Dim kS As String = ""
Dim expressionI As Integer = 1 - 1
Dim priorityinputI As Integer = 1 - 1
Dim prioritystackI As Integer = 1 - 1
Dim stackI As Integer = 1 - 1
Dim posI As Integer = -1
Dim I As Integer = 1 - 1
stackI = stackI + 1
Console.WriteLine(expressionS)
Do
expressionI = expressionI + 1
kexpressionS = Mid$(expressionS, expressionI, 1)
posI = InStr(tokenS, kexpressionS)
If posI = 0 Then Console.WriteLine(kexpressionS + " not
found in tokenS " + tokenS) : End
priorityinputI = Val(Mid$(tableinputS, posI, 1))
kstackS = Mid$(stackS, 1, 1)
posI = InStr(tokenS, kstackS)
If posI = 0 Then Console.WriteLine(kstackS + " not found
in tokenS " + tokenS) : End
prioritystackI = Val(Mid$(tablestackS, posI, 1))
If priorityinputI > prioritystackI Then PROCGreater
(kexpressionS, stackS) Else PROCWhileSmallerOrEqual(priorityinputI,
stackS, resultS, kexpressionS, tokenS, tablestackS)
Loop Until (expressionI >= Len(expressionS))
Do
I = I + 1
kS = Mid$(stackS, I, 1)
If kS <> "#" And kS <> "(" And kS <> ")" Then resultS =
resultS + kS
Loop Until (I >= Len(stackS))
Console.WriteLine("resultS is " + resultS)
Do
Loop
End Sub
Sub PROCGreater(ByVal kexpressionS As String, ByRef stackS As
String)
stackS = kexpressionS + stackS
End Sub
Sub PROCWhileSmallerOrEqual(ByVal priorityinputI As Integer, ByRef
stacks As String, ByRef resultS As String, ByVal kexpressionS As
String, ByVal tokenS As String, ByVal tablestackS As String)
Dim kS As String = ""
Dim kstackS As String = ""
Dim posI As Integer = -1
Dim prioritystackI As Integer = -1
Do
kS = Mid$(stacks, 1, 1)
If kS <> "(" And kS <> ")" Then resultS = resultS + kS
stacks = Mid$(stacks, 2, Len(stacks) - 1)
kstackS = Mid$(stacks, 1, 1)
posI = InStr(tokenS, kstackS)
If posI = 0 Then Console.WriteLine(kstackS + " not found
in tokenS " + tokenS) : End
prioritystackI = Val(Mid$(tablestackS, posI, 1))
Loop Until (priorityinputI > prioritystackI)
stacks = kexpressionS + stacks
End Sub
--- cut here ---------------------------------------------------------
3. -Run this code
That should show:
((2*3)^6+(4*5)+(6*7))^7
resultS is 23*6^45*+67*+7^
which is respectively the infix and suffix representation.
4. -Change expressionS to your own infix string
e.g.
expressionS = "(3+4)*5+6-7"
and recompile for another example
---
Note:
In order to keep it very simple and short in this
implementation:
1. Please take spaces away.
so "(3+4)*5+6-7" is OK,
but "( 3 + 4 ) * 5 + 6 - 7" is not.
2. only single characters (like a, b, c, ..., 3, 4, 5, ...)
so "a+b*c-d^f" is OK,
but "alpha*beta+gamma" is not.
3. the priorities should here be single numbers between 0 and 9
(like 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
in your tables.
4. This program is a direct translation of the Dijkstra double
priority algorithm (see below for the link and further
description)
5. all lines in the program are single lines, so all on one line
---
---
Internet: see also:
---
Algorithm: Expression: Infix: Convert: Postfix: Overview: How convert
infix to postfix expression?
http://www.faqts.com/knowledge_base/view.phtml/aid/26071/fid/585
----------------------------------------------------------------------