faqts : Computers : Programming : Languages : C#

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 3 people (100%) answered Yes
Recently 3 of 3 people (100%) answered Yes

Entry

Language: Computer: C#: Simple: How to convert infix to suffix? [Bracket]

Jan 10th, 2004 14:27
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 10 January 2004 - 08:00 pm --------------------

Language: Computer: C#: Simple: How to convert infix to suffix? 
[Bracket]

---

Steps: Overview:

 1. -Create a new C# application

 2. -Fill in the following code

--- cut here ---------------------------------------------------------

namespace ConsoleApplication3 {

 using System;

 //

 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

 ~CLASSStringConvertInfixToSuffix() {

 }

 public string FNStringConvertInfixToPrefixS() {

  do

  {

   expressionI = expressionI + 1;

   kexpressionS = expressionS.Substring( expressionI, 1 );

   posI = tabletokenS.IndexOf( kexpressionS, 0 );

   if ( posI == -1 ) {

    Console.WriteLine( kexpressionS + " not found in tabletokenS " + 
tabletokenS );

    return( "error" );

   }

   priorityinputI = int.Parse( tableinputS.Substring( posI, 1 ) );

   kstackS = stackS.Substring( 0, 1 );

   posI = tabletokenS.IndexOf( kstackS, 0 );

   if ( posI < 0 ) {

    Console.WriteLine( kstackS + " not found in tabletokenS " + 
tabletokenS );

    return( "error" );

   }

   prioritystackI = int.Parse( tablestackS.Substring( posI, 1 ) );

   if ( priorityinputI > prioritystackI ) {

    stackS = kexpressionS + stackS;

   }

   else {

    do {

     if ( ( kstackS != "(" ) && ( kstackS != ")" ) ) {

      resultS = resultS + kstackS;

     }

     stackS = stackS.Substring( 1, stackS.Length - 1 );

     kstackS = stackS.Substring( 0, 1 );

     posI = tabletokenS.IndexOf( kstackS, 0 );

     if ( posI < 0 ) {

      Console.WriteLine( kstackS + " not found in tabletokenS " + 
tabletokenS );

      return( "error" );

     }

     prioritystackI = int.Parse( tablestackS.Substring( posI, 1 ) );

    } while ( priorityinputI <= prioritystackI );

    stackS = kexpressionS + stackS;

   }

  } while ( expressionI < ( expressionS.Length - 1 ) );

  I = 0 - 1;

  do {

   I = I + 1;

   kstackS = stackS.Substring( I, 1 );

   if ( ( kstackS != "#" ) && ( kstackS != "(" ) && ( kstackS !
= ")" ) ) {

    resultS = resultS + kstackS;

   }

  } while ( I < ( stackS.Length - 1 ) );

  return( resultS );

 }

 }

 //

 class CLASSStringConvertInfixToSuffixMain {

  public static void Main() {

   string infixS = "((2*3)^6+(4*5)+(6*7))^7";

   do {

    CLASSStringConvertInfixToSuffix OBJECTrpn = new 
CLASSStringConvertInfixToSuffix( infixS );

    Console.WriteLine( "infix = " + infixS );

    Console.WriteLine( "suffix = " + 
OBJECTrpn.FNStringConvertInfixToPrefixS() );

    Console.Write("Type in a new infix string (type Q to quit), for 
example (3+4)*5+6-7 ");

    infixS = Console.ReadLine();

    if ( infixS == "" ) {

     infixS = "(3+4)*5+6-7";

    }

   } while ( infixS.ToLower() != "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:

- Microsoft Visual C# command line compiler csc.exe

- Microsoft Visual Studio C#.NET

---
---

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

----------------------------------------------------------------------