faqts : Computers : Programming : Languages : JavaScript

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

2 of 3 people (67%) answered Yes
Recently 2 of 3 people (67%) answered Yes

Entry

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

Apr 7th, 2008 23:40
ha mo, Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 11 January 2004 - 08:33 am --------------------

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

---

Steps: Overview:

 1. -Create a new JavaScript application

 2. -Fill in the following code

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

<SCRIPT LANGUAGE = "JavaScript">

<!--

<!-- library: math: convert: string: infix: suffix [kn, ri, su, 11-01-
2004  7:30:56]

function FNStringConvertInfixToPrefixS( expressionS ) {

 // e.g. <HTML>

 // e.g.  <BODY onLoad=' PROCMessage( FNStringConvertInfixToPrefixS
( "((2*3)^6+(4*5)+(6*7))^7" ) ); // gives e.g. 23*6^45*+67*+7^'>

 // e.g.  </BODY>

 // e.g. </HTML>

 var tabletokenS = "#-+/*^abcdefghijklmnopqrstuvwxyz0123456789()";

 var tableinputS = "03344599999999999999999999999999999999999992";

 var tablestackS = "03344599999999999999999999999999999999999929";

 var stackS = "";

 var kexpressionS = "";

 var kstackS = "";

 var resultS = "";

 var expressionI = 0 - 1;

 var stackI = 0 - 1;

 var posI = -1;

 var priorityinputI = -1;

 var prioritystackI = -1;

 var I = 0;

 var stackI = stackI + 1;

 var stackS = "#";

 do

 {

  expressionI = expressionI + 1;

  kexpressionS = expressionS.substring( expressionI, expressionI + 1 );

  posI = tabletokenS.indexOf( kexpressionS );

  if ( posI == -1 ) {

   alert( kexpressionS + " not found in tabletokenS " + tabletokenS );

   return( "error" );

  }

  priorityinputI = parseInt( tableinputS.substring( posI, posI + 1 ) );

  kstackS = stackS.substring( 0, 1 );

  posI = tabletokenS.indexOf( kstackS );

  if ( posI < 0 ) {

   alert( kstackS + " not found in tabletokenS " + tabletokenS );

   return( "error" );

  }

  prioritystackI = parseInt( tablestackS.substring( posI, posI + 1 ) );

  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 ) {

     alert( kstackS + " not found in tabletokenS " + tabletokenS );

     return( "error" );

    }

    prioritystackI = parseInt( tablestackS.substring( posI, posI + 
1 ) );

   } 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.charAt( 0 ) != '#' ) && ( kstackS.charAt( 0 ) !
= '(' ) && ( kstackS.charAt( 0 ) != ')' ) ) {

   resultS = resultS + kstackS;

  }

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

 return( resultS );

}

// -->

</SCRIPT>

--- 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:

Successful run in browser Microsoft Explorer v6.

---
---

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

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



http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com