Entry
Language: Computer: Java: Simple: How to convert infix to suffix? [Bracket]
Jan 10th, 2004 20:35
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 04 January 2004 - 05:57 pm --------------------
Language: Computer: Java: Simple: How to convert infix to suffix?
[Bracket]
---
Steps: Overview:
1. -Create a new Java (stand alone) application
2. -Fill in the following code
--- cut here ---------------------------------------------------------
import java.io.*;
//
class CLASSStringConvertInfixToSuffix {
private String expressionS;
private String tabletokenS;
private String tableinputS;
private String tablestackS;
private String stackS;
private String kexpressionS;
private String kstackS;
private String resultS;
private int expressionI;
private int stackI;
private int posI;
private int priorityinputI;
private int prioritystackI;
private int I;
// constructor for this class
public CLASSStringConvertInfixToSuffix( String expressionS )
{
this.expressionS = expressionS;
tabletokenS = "#-+/*^abcdefghijklmnopqrstuvwxyz0123456789()";
tableinputS = "03344599999999999999999999999999999999999992";
tablestackS = "03344599999999999999999999999999999999999929";
stackS = "";
kexpressionS = "";
kstackS = "";
resultS = "";
expressionI = 0 - 1;
stackI = 0 - 1;
posI = -1;
priorityinputI = -1;
prioritystackI = -1;
I = 0;
stackI = stackI + 1;
stackS = "#";
}
// destructor for this class
protected void finalize() {
}
//
public String FNStringConvertInfixToPrefixS() {
do
{
expressionI = expressionI + 1;
kexpressionS = expressionS.substring( expressionI, expressionI +
1 );
posI = tabletokenS.indexOf( kexpressionS, 0 );
if ( posI == -1 ) {
System.out.println( kexpressionS + " not found in tabletokenS " +
tabletokenS );
return( "error" );
}
priorityinputI = Integer.valueOf( tableinputS.substring( posI, posI
+ 1 ) ).intValue();
kstackS = stackS.substring( 0, 1 );
posI = tabletokenS.indexOf( kstackS, 0 );
if ( posI < 0 ) {
System.out.println( kstackS + " not found in tabletokenS " +
tabletokenS );
return( "error" );
}
prioritystackI = Integer.valueOf( tablestackS.substring( posI, posI
+ 1 ) ).intValue();
if ( priorityinputI > prioritystackI ) {
stackS = kexpressionS + stackS;
}
else {
do {
if ( ( kstackS.charAt( 0 ) != '(' ) && ( kstackS.charAt( 0 ) !
= ')' ) ) {
resultS = resultS + kstackS;
}
stackS = stackS.substring( 1, stackS.length() );
kstackS = stackS.substring( 0, 1 );
posI = tabletokenS.indexOf( kstackS, 0 );
if ( posI < 0 ) {
System.out.println( kstackS + " not found in tabletokenS " +
tabletokenS );
return( "error" );
}
prioritystackI = Integer.valueOf( tablestackS.substring( posI,
posI + 1 ) ).intValue();
} while ( priorityinputI <= prioritystackI );
stackS = kexpressionS + stackS;
}
} while ( expressionI < ( expressionS.length() - 1 ) );
I = 0 - 1;
do {
I = I + 1;
kstackS = stackS.substring( I, I + 1 );
if ( ( !kstackS.equals( "#" ) ) && ( !kstackS.equals( "(" ) ) && ( !
kstackS.equals( ")" ) ) ) {
resultS = resultS + kstackS;
}
} while ( I < ( stackS.length() - 1 ) );
return( resultS );
}
}
//
class CLASSStringConvertInfixToSuffixMain {
public static void main( String args[] ) throws IOException {
String infixS = "((2*3)^6+(4*5)+(6*7))^7";
BufferedReader kbd = new BufferedReader( new InputStreamReader(
System.in ) );
do {
CLASSStringConvertInfixToSuffix OBJECTrpn = new
CLASSStringConvertInfixToSuffix( infixS );
System.out.println( "infix = " + infixS );
System.out.println( "suffix = " +
OBJECTrpn.FNStringConvertInfixToPrefixS() );
System.out.println( "Type in a new infix String (type Q to quit),
for example (3+4)*5+6-7 ");
try {
infixS = kbd.readLine();
}
catch ( Exception e ) {
System.out.println( "an input error occurred" );
}
if ( infixS.equals( "" ) ) {
infixS = "(3+4)*5+6-7";
}
} while ( !infixS.toLowerCase().equals( "q" ) );
}
}
--- 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,
e.g. that should show 34+5*6+7-
---
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)
---
---
Note:
Compiled successfully as a console application in:
- SUN Java command line compilers javac.exe and java.exe
---
---
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
----------------------------------------------------------------------