Postfix Evaluator : Postfix Infix « Development « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Dojo toolkit
7. Event
8. Event onMethod
9. Ext JS
10. Form Control
11. GUI Components
12. HTML
13. Javascript Collections
14. Javascript Objects
15. Javascript Properties
16. jQuery
17. Language Basics
18. Mochkit
19. Mootools
20. Node Operation
21. Object Oriented
22. Page Components
23. Rico
24. Scriptaculous
25. Security
26. SmartClient
27. Style Layout
28. Table
29. Utilities
30. Window Browser
31. YUI Library
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
JavaScript DHTML » Development » Postfix Infix 
Postfix Evaluator

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Postfix Evaluator</title>
<style type="text/css">
.header{font-family:verdana,arial,helvetica; font-weight:bold; font-size:22pt; color:#0066CC; filter:DropShadow(color=#CCCCFF, offX=2, offY=2, positive=1); width:100%}
.form_in{background:#FFFFFF; border:#0066CC solid 1px}
.text_box{font-weight:bold; background:#EEEEFF; border:#0066CC solid 1px; height:20px}
.button{background:#0066CC; font-weight:bold; color:#FFFFFF; border:#0066CC solid 1px; height:20px; cursor:hand}
.link{color:#0066CC}
.link:hover{color:#0099FF}
</style>
</head>
<body bgcolor="#FFFFFF">

<!--BEGIN POSTFIX EVALUATOR JAVASCRIPT-->
<script language="JavaScript">
/*
  Postfix Evaluator
  - Evaluates a Postfix(Postorder) expression
  - For eg. '12*3+' results in 5
  - Valid Operators are +,-,*,/
  - No Error Handling in this version
  JavaScript Implementation
  - CopyRight 2002 Premshree Pillai
  See algorithm at
  -http://www.qiksearch.com/articles/cs/postfix-evaluation/index.htm
  Created : 02/09/02 (dd/mm/yy)
  Web : http://www.qiksearch.com
  E-mail : qiksearch@rediffmail.com
*/

function push_stack(stackArr,ele)
{
 stackArr[stackArr.length]=ele;
}

function pop_stack(stackArr)
{
 var _temp=stackArr[stackArr.length-1];
 delete stackArr[stackArr.length-1];
 stackArr.length--;
 return(_temp);
}

function isOperand(who)
{
 return(!isOperator(who)true false);
}

function isOperator(who)
{
 return((who=="+" || who=="-" || who=="*" || who=="/" || who=="(" || who==")")true false);
}

function topStack(stackArr)
{
 return(stackArr[stackArr.length-1]);
}

function PostfixSubEval(num1,num2,sym)
{
 var returnVal;
 if(sym=="+")
  returnVal=num1+num2;
 if(sym=="-")
  returnVal=num1-num2;
 if(sym=="*")
  returnVal=num1*num2;
 if(sym=="/")
  returnVal=num1/num2;
 return(returnVal);
}

function PostfixEval(postfixStr)
{
 var stackArr=new Array();
 postfixStr=postfixStr.split('');
 for(var i=0; i<postfixStr.length; i++)
 {
  if(isOperand(postfixStr[i]))
  {
   push_stack(stackArr,postfixStr[i]);
  }
  else
  {
   var temp=parseFloat(topStack(stackArr));
   pop_stack(stackArr);
   var pushVal=PostfixSubEval(parseFloat(topStack(stackArr)),temp,postfixStr[i]);
   pop_stack(stackArr);
   push_stack(stackArr,pushVal);
  }
 }
 return(topStack(stackArr));

</script>
<!--END POSTFIX EVALUATOR JAVASCRIPT-->

<center><span class="header">Postfix Evaluator</span></center>

<!--BEGIN FORM-->
<center>
<form name="input_form">
<table class="form_in" cellspacing="0" cellpadding="3">
 <tr bgcolor="#0066CC">
  <td><font face="verdana,arial,helvetica" size="-2" color="#FFFFFF">Postfix Expression :</font></td>
  <td><font face="verdana,arial,helvetica" size="-2" color="#FFFFFF">Result :</font></td>
  <td></td>
 </tr>

 <tr>
  <td><input type="text" name="postfixVal" class="text_box" value=""></td>
  <td><input type="text" name="resultVal" class="text_box" value=""></td>
  <td align="bottom"><input type="button" onClick="document.input_form.resultVal.value=PostfixEval(document.input_form.postfixVal.value)" value="Evaluate Postfix" class="button"></td>
 </tr>
</table>
</form>
</center>
<!--END FORM-->

<table width="485" align="center"><tr><td>
 <font face="verdana,arial,helvetica" size="-1" color="#000000">

 This JavaScript evaluates a Postfix(Postorderexpression.
 <br><br><a href="http://www.qiksearch.com/articles/cs/postfix-evaluation/index.htm" class="link">Click here</a> for the algorithm used.
 <hr style="color:#003366; height:1px">
 &#169 2002 <a href="http://www.qiksearch.com" class="link" title="Click here to visit Qiksearch.com">Premshree Pillai</a>.
 </font>
</td></tr></table>

</body>
</html>

           
       
Related examples in the same category
1. Infix to Postfix Conversion
2. Postfix to Infix Conversion
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.