Webb - all rights reserved - ©2004

Author - Jann Scheuer
Email: scheuer@coco.ihi.ku.dk
Webpage:
Summary: Two suggestions for infix calculators, a simple and an
advanced one Instructions: Infix calculators
Two suggestions for infix calculators. There is not a lot to say about them. Infix notation is silly and contra-intuitive, but programming is fun. Therefore I made them. The simple one behaves like your husband's average 2$ calculator, evaluating expressions without consideration to operator hierarchies, etc. The enter key corresponds to the '=' key. The advanced one uses an operator hierarchy when evaluating expressions. It maintains two stacks: The numbers in input expressions are put on the RPN stack. The operators (and parentheses) are put on a stack consisting of the RPN variables (i.e. xa through xy, xz is used as a stack index). The operator hierarchy is: P > *,/ > +,-. This means that 3 + 3 * 2 = 9 (and not 8) 4 + 3 * 2 ^ 2 = 16 (and not 196, ^ meaning power) An input string like 3 + 3 is not evaluated before enter is pressed or an additional operator at the same or lower level in the heirarchy is typed in. Try it, and you'll see what I mean. It features parentheses too. You can have about 12 open left parentheses in your expression before the application gets dizzy. Parentheses syntax and some operator syntax is checked. Warning: The advanced calculator deletes the RPN stack at the beginning of each calculation. So do not start the function set with an RPN stack you want to keep. If the advanced calculator malfunctions (I do not know why it should, it is pretty robust, but anyway...) you may try the reset button. It empties both stacks. (Press "infix", then "reset".) Code:
RPN.2.a \ Infix simple [r]xac(/:*:-:+:P); [s]UhUeh1>(Cr); {b}Vv4=0(Cs0Xa) v#'10'=0(Cs1Xa) v#'16'=0(Cs2Xa) v#'22'=0(Cs3Xa) v#'11'=0(Cs4Xa) v#'12'=0(Cs)Ud; "Infix" "Infix"D'Square calculator|Ok|'d1; RPN.2.z \ Infix advanced {o}0Xz; [p]c(0:1:1:2:2); [r]c(P:/:*:-:+); [u]xz1+XzxzX@; [o]xzx@xz1-Xz; [t]xz(xzx@:1n); [l]xz0V{Ct0 [k]xz0V{Ct0<(B:Co8<(v1+V)}Xzv; [e]{CtCpCoVCtCpvCu&(CoVCoCrvCu:B)}; [E]Ce{Cl(CoCr:B)}; [f]CuUeUhCkh1->(3Cm.)Ce; [m]c(D'Right parenthesis excpected!|Ok|':D'Misplaced parenthesis!|Ok|': D'No matching left parenthesis!|Ok|':D'Misplaced operator!|Ok|')CcUd; [M]D'Square calculator|?|Reset|Ok|'c(D'Operator hierarchy: P>*/>+- \and parentheses!|Ok|'d1:Cc:); [a]c(0:4:5:#'10':#'11':#'12':#'16':#'17':#'21':#'22':#'23':44P); [b]V0Xz{xzCag1v=0(d10B:v>(1B)xz1+Xz}0Xz; [c]hV{_vd1}0Xz; {b}Vh(xz!(vCb(Cc))) v4=0(1Cf: v#'10'=0(2Cf: v#'16'=0(3Cf: v#'22'=0(4Cf: v#'11'=0(0Cf: v#'12'=0(UeUh xzCk-(0Cm.)hCk=0(3Cm.) xz(CE)))))))Ud; "Infix" "("hCk=0(8Cu:1Cm); "Infix"CM; ")"hCk=0(1Cm.)xzCk>!(2Cm.)CECod1; |