faqts : Computers : Programming : Languages : Delphi

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

5 of 6 people (83%) answered Yes
Recently 5 of 6 people (83%) answered Yes

Entry

Delphi: Simple: How to convert infix to suffix? [Bracket]

Jan 9th, 2004 21:32
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 04 January 2004 - 07:26 pm - 07:58 pm ---------

Delphi: Simple: How to convert infix to suffix? [Bracket]

---

Steps: Overview:

 1. -Create a new Delphi application

 2. -Put on the form

     1. Button

     2. Editbox

 3. -Add to your Uses statement

      StrUtils

 4. -Fill in the following code for

      PROCStringConvertInfixToPrefix();

 5. Alltogether  that gives the following code:

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

unit Unit1;

interface

uses
  StrUtils,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, 
Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure PROCStringConvertInfixToPrefix( expressionS : string );
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// library: string: convert: infix: to: prefix [kn, ri, su, 04-01-2004 
19:29:46]

procedure TForm1.PROCStringConvertInfixToPrefix( expressionS : 
string );

 // e.g. PROCStringConvertInfixToPrefix( '(3+4)*5+6-7' );

 // e.g. PROCStringConvertInfixToPrefix( '(a+b)*c+d-e' );

 // e.g. PROCStringConvertInfixToPrefix( '((2*3)^6+(4*5)+(6*7))^7' );

 var tabletokenS : string;

 var tableinputS : string;

 var tablestackS : string;

 var stackS : string;

 var kexpressionS : string;

 var kstackS : string;

 var resultS : string;

 expressionI : integer;

 stackI : integer;

 posI : integer;

 priorityinputI : integer;

 prioritystackI : integer;

 I : integer;

begin

 tabletokenS := '#-+*/^abcdefghijklmnopqrstuvwxyz0123456789()';

 tableinputS := '03344599999999999999999999999999999999999992';

 tablestackS := '03344599999999999999999999999999999999999929';

 stackS := '';

 kexpressionS := '';

 kstackS := '';

 resultS := '';

 expressionI := 1 - 1;

 stackI := 1 - 1;

 posI := -1;

 priorityinputI := -1;

 prioritystackI := -1;

 I := 0;

 stackI := stackI + 1;

 stackS := '#';

 ShowMessage( 'infix := ' + expressionS );

 repeat

  expressionI := expressionI + 1;

  kexpressionS := Midstr( expressionS, expressionI, 1 );

  posI := Pos( kexpressionS, tabletokenS );

  if ( posI = 0 ) then begin

   ShowMessage( kexpressionS + ' not found in tabletokenS ' + 
tabletokenS );

   Exit;

  end;

  priorityinputI := StrToInt( Midstr( tableinputS, posI, 1 ) );

  kstackS := Midstr( stackS, 1, 1 );

  posI := Pos( kstackS, tabletokenS );

  if posI = 0 then begin

   ShowMessage( kstackS + ' not found in tabletokenS ' + tabletokenS );

   Exit;

  end;

  prioritystackI := StrToInt( Midstr( tablestackS, posI, 1 ) );

  if priorityinputI > prioritystackI then begin

   stackS := kexpressionS + stackS;

  end

  else begin

   repeat

    kstackS := Midstr( stackS, 1, 1 );

    if ( kstackS <> '(' ) and ( kstackS <> ')' ) then begin

     resultS := resultS + kstackS;

    end;

    stackS := Midstr( stackS, 2, Length( stackS ) - 1 );

    kstackS := Midstr( stackS, 1, 1 );

    posI := Pos( kstackS, tabletokenS );

    if posI = 0 then begin

     ShowMessage( kstackS + ' not found in tabletokenS ' + 
tabletokenS );

     Exit

    end;

    prioritystackI := StrToInt( Midstr( tablestackS, posI, 1 ) );

   until ( priorityinputI > prioritystackI );

   stackS := kexpressionS + stackS;

  end;

 until expressionI >= Length( expressionS );

 I := 1 - 1;

 repeat

  I := I + 1;

  kstackS := Midstr( stackS, I, 1 );

  if ( kstackS <> '#' ) and ( kstackS <> '(' ) and ( kstackS <> ')' ) 
then begin

   resultS := resultS + kstackS;

  end;

 until I >= Length( stackS );

 ShowMessage( 'suffix := ' + resultS );

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

 PROCStringConvertInfixToPrefix( Edit1.Text );

end;

end.


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

 6. -Run this code

 7. -Put e.g. in the text box

      ((2*3)^6+(4*5)+(6*7))^7

 8. -Run the program

     1. Click on the button

     2. That should show:

         23*6^45*+67*+7^

      which is respectively the infix and suffix representation.

 9. -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)

---
---

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

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