tux javascript infix and postfix example with stack

Infix calculator
postfix calculator
infix to postfix algorithm
postfix to infix algorithm
stack methods
how to do infix and postfix notation with stack

click here for live example

1 - /*
2 -   Infix ~ Postfix Conversion
3 -   - Converts an Infix(Inorder) expression to Postfix(Postorder) and vice-versa
4 -   - Valid Operators are +,-,*,/,^,()
5 -   - No Error Handling in this version
6 -   JavaScript Implementation
7 -   - © 2002 Premshree Pillai
8 -   See algorithms at
9 -   -http://www.qiksearch.com/articles/cs/infix-postfix/index.htm
10 -   -http://www.qiksearch.com/articles/cs/postfix-evaluation/index.htm
11 -   Created : 03/08/02 (dd/mm/yy)
12 -   Modified : 23/10/02 (dd/mm/yy)
13 -   Web : http://www.qiksearch.com
14 -   E-mail : qiksearch@rediffmail.com
15 - */
16 -
17 - function push_stack(stackArr,ele)
18 - {
19 - 	stackArr[stackArr.length]=ele;
20 - }
21 -
22 - function pop_stack(stackArr)
23 - {
24 - 	var _temp=stackArr[stackArr.length-1];
25 - 	delete stackArr[stackArr.length-1];
26 - 	stackArr.length--;
27 - 	return(_temp);
28 - }
29 -
30 - function isOperand(who)
31 - {
32 - 	return((!isOperator(who) && (who!="(") && (who!=")"))? true : false);
33 - }
34 -
35 - function isOperator(who)
36 - {
37 - 	return((who=="+" || who=="-" || who=="*" || who=="/" || who=="^")? true : false);
38 - }
39 -
40 - function topStack(stackArr)
41 - {
42 - 	return(stackArr[stackArr.length-1]);
43 - }
44 -
45 - function isEmpty(stackArr)
46 - {
47 - 	return((stackArr.length==0)? true : false);
48 - }
49 -
50 - /* Check for Precedence */
51 - function prcd(who)
52 - {
53 - 	if(who=="^")
54 - 		return(5);
55 - 	if((who=="*")||(who=="/"))
56 - 		return(4);
57 - 	if((who=="+")||(who=="-"))
58 - 		return(3);
59 - 	if(who=="(")
60 - 		return(2);
61 - 	if(who==")")
62 - 		return(1);
63 - }
64 -
65 - function InfixToPostfix(infixStr,postfixStr,retType)
66 - {
67 - 	var postfixStr=new Array();
68 - 	var stackArr=new Array();
69 - 	var postfixPtr=0;
70 - 	infixStr=strToTokens(infixStr);
71 - 	for(var i=0; i
			
November 22, 2009
Posted in Algorithm — banias @ 1:26 pm

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment