Entry
TSE: Simple: How to convert infix to suffix? [Bracket]
Jan 9th, 2004 21:35
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 04 January 2004 - 06:11 pm - 06:28 pm----------
TSE: Simple: How to convert infix to suffix? [Bracket]
---
Steps: Overview:
1. -Create a new TSE macro
2. -Fill in the following code
--- cut here ---------------------------------------------------------
FORWARD PROC PROCStringConvertInfixToPrefix( STRING s1 )
PROC Main()
PROCStringConvertInfixToPrefix( "((2*3)^6+(4*5)+(6*7))^7" )
PROCStringConvertInfixToPrefix( "(3+4)*5+6-7" )
PROCStringConvertInfixToPrefix( "(a+b)*c+d-e" )
END
PROC PROCStringConvertInfixToPrefix( STRING expressionS )
STRING tabletokenS[255] = "#-
+*/^abcdefghijklmnopqrstuvwxyz0123456789()"
STRING tableinputS[255]
= "03344599999999999999999999999999999999999992"
STRING tablestackS[255]
= "03344599999999999999999999999999999999999929"
STRING stackS[255] = ""
STRING kexpressionS[255] = ""
STRING kstackS[255] = ""
STRING resultS[255] = ""
INTEGER expressionI = 1 - 1
INTEGER stackI = 1 - 1
INTEGER posI = -1
INTEGER priorityinputI = -1
INTEGER prioritystackI = -1
INTEGER I = 0
stackI = stackI + 1
stackS = "#"
WARN( "infix = " + expressionS )
REPEAT
expressionI = expressionI + 1
kexpressionS = SUBSTR( expressionS, expressionI, 1 )
posI = POS( kexpressionS, tabletokenS )
IF posI == 0
Warn( kexpressionS + " not found in tabletokenS " + tabletokenS )
RETURN()
ENDIF
priorityinputI = VAL( SUBSTR( tableinputS, posI, 1 ) )
kstackS = SUBSTR( stackS, 1, 1 )
posI = POS( kstackS, tabletokenS )
IF posI == 0
WARN( kstackS + " not found in tabletokenS " + tabletokenS )
RETURN()
ENDIF
prioritystackI = VAL( SUBSTR( tablestackS, posI, 1 ) )
IF priorityinputI > prioritystackI
stackS = kexpressionS + stackS
ELSE
REPEAT
kstackS = SUBSTR( stackS, 1, 1 )
IF kstackS <> "(" AND kstackS <> ")"
resultS = resultS + kstackS
ENDIF
stackS = SUBSTR( stackS, 2, LENGTH( stackS ) - 1 )
kstackS = SUBSTR( stackS, 1, 1 )
posI = POS( kstackS, tabletokenS )
IF posI == 0
WARN( kstackS + " not found in tabletokenS " + tabletokenS )
RETURN()
ENDIF
prioritystackI = VAL( SUBSTR( tablestackS, posI, 1 ) )
UNTIL ( priorityinputI > prioritystackI )
stackS = kexpressionS + stackS
ENDIF
UNTIL expressionI >= LENGTH( expressionS )
I = 1 - 1
REPEAT
I = I + 1
kstackS = SUBSTR( stackS, I, 1 )
IF kstackS <> "#" AND kstackS <> "(" AND kstackS <> ")"
resultS = resultS + kstackS
ENDIF
UNTIL I >= LENGTH( stackS )
WARN( "suffix = " + resultS )
END
--- 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
----------------------------------------------------------------------