> >@=7 )bjbjUU ">77)l&&&&&&&: :2 ?AAAAAA A& A
&& V
& & ?
?

Vo@&&3 @q:$
3l0x
(3
::&&&&In this program you will ask the user to input a mathematical expression in infix notation. You will convert the expression to Reverse Polish Notation (postfix notation), display it and its value when calculated. Your program should be controlled with a loop so the user can enter a new expression until he or she wants to quit. The symbols in the top row of the following table are the only operators allowed. You will have to put an exclamation mark at the beginning and end of each expression that has been entered.
Symbol in top row is symbol at the switch
!+*/()!4111115+22211122221112*2222212/2222212(5111113
Symbol in left column is most recent symbol on Texas line
Ca ____________________________________ switch _______________________________________NY         TexasUse your imagination and pretend there are a series of railroad cars travelling from New York to California. Pretend also that each car contains a variable (a number) or an arithmetic operator. From left to right the cars form a mathematical expression in infix notation. For example, in the expression (4 + 2) / 10, there are seven cars. The car closest to Californiathe leftmost car contains '(' and the car farthest from Californiathe rightmost carconatains 10. Every train contains a start and end symbol, in this case '!'. When the first car approaches the switch the start symbol is sent to Texas. Every car containing a variable ( a number) goes directly to California. Use the table and the following rules to determine what happens to the operators when they enter the switch.
Car at switch goes to Texas.
Most recent car on Texas line turns and goes to California.
Both the car at the switch and the most recent car on the Texas line disappearare deleted.
Stop. Cars in California represent Reveerse Polish Formula read from left to right.
Stop. Errorformula was not properly balanced.
The Texas line is, of course, a stack. And you will push or pop from the stack using the above rules. Practise with these expressions and see if you get the correct Reverse Polish expressions. Remember, anything at the switch that isn't an operator goes straight to California, so just check for operators (!, +,  , *, /, (, ) ), seven of them.
A + B * C ABC*+
A * B + C AB*C+
A * B + C * D AB*CD*+
(A + B) / (C  D) AB+CD/
When you prompt for an expression in your program, the user will input numbers instead of variables. Make sure all symbols input are space delimited.
Hint 1: Although we haven't discussed them and won't learn how they are implemented until later, you'll be better off if you use a twodimentional array like this:
@rules = ( [0,'!','+','','*','/','(',')'], ['!',4,1,1,1,1,1,5], ['+',2,2,2,1,1,1,2], ['',2,2,2,1,1,1,2], ['*',2,2,2,2,2,1,2], ['/',2,2,2,2,2,1,2], ['(',5,1,1,1,1,1,3] );
This is a 7 X 8 (7 rows and eight columns) array or table. To print the table:
for($row=0;$row<7;$row++) { for($col=0;$col<9;$col++) { print "$rules[$row][$col] "; } print "\n"; }
So the rule for a + sign at the switch and a ! on the Texas line is $rule[1][2] ( which is rule 1). The first index is the row and the second the column, but remember arrays always begin with an index of 0. This is an example of a lookup table. In this program you will be pushing and popping the Texas line, shifting the input line, and pushing onto the RPN expression. Once you have the RPN expression, your next step is to evaluate it.To evaluate the RPN expression do the following:
Set $n to 0  the index to the first symbol in the RPN expression.
If $rpn[$n] is a variable, push it onto an evaluation stack. If it's an operator, pop the two top itmes off the stack, perform the operation and push the value back on the stack.
If $n == $#rpn ( if its the last symbol in the RPN expression) stop and display the last element on the stack; else, add 1 to $n and goto step 2.
!"123BC
%Cl)CJ
5\aJCJaJ5\aJ05 $$Ifa$$a$)D$$IfִT$
:JJJJJJ;0?6 34ab $$Ifa$D$$IfִT$
:JJJJJJ;0?6 34ab $$Ifa$D$$IfִT$
:JJJJJJ;0?6 34ab
$$Ifa$D$$IfִT$
:JJJJJJ;0?6 34ab $$Ifa$ !D$$IfִT$
:JJJJJJ;0?6 34ab !#%')+/1 $$Ifa$12D$$IfִT$
:JJJJJJ;0?6 34ab 2468:<>@B $$Ifa$BC$$IfִT$
:JJJJJJ;0?6 34ab CB%CDr(
&Fdd[$\$
&Fdd[$\$
&Fdd[$\$$a$() 1h/ =!"#$%
i8@8NormalCJ_HaJmH sH tH F`"F Heading 2dd@&[$\$5CJ$\aJ$<A@<Default Paragraph Font6Z`6
Plain Textdd[$\$)>5
!#%')+/12468:<>@BC
B%
CDr(+000000000000000000000000000000000000000000000000000000000000000000000 0 0 0 0 00 0 0 0 00000000 0 0 00)
!12BC()) &1dgEJ+BNL
O
[
]
EJ X+333333333333333"#RR(+Mwert4C:\Documents and Settings\Mwert\Desktop\optional.doc=QDirbnqwޣ^`.^`.pp^p`.@@^@`.^`.^`.^`.^`.PP^P`.^`CJOJQJo(^`CJOJQJo(opp^p`CJOJQJo(@@^@`CJOJQJo(^`CJOJQJo(^`CJOJQJo(^`CJOJQJo(^`CJOJQJo(PP^P`CJOJQJo(^`.^`.pp^p`.@@^@`.^`.^`.^`.^`.PP^P`.nqwDi=QPl;8Xjv>Dc"pN;rj$9݈f@tgODlHk~͎$~)Y#5.]"z:Y
!#%')+/12468:<>@BC+@)0@UnknownGz Times New Roman5Symbol3&z Arial?5 z Courier New;Wingdings"qhMMr%!20l(2ZIn this program you will ask the user to input a mathematical expression in infix notationMwertMwertOh+'0$0@ P\
x
[In this program you will ask the user to input a mathematical expression in infix notationn tMwerts werwerNormal Mwert 3erMicrosoft Word 9.0 @Ik@`0q@2Лqr՜.+,0Hhp
'none %l
[In this program you will ask the user to input a mathematical expression in infix notationTitle
!"#$%&'()*+,./012346789:;<?Root Entry FpLqA1Table WordDocument">SummaryInformation(DocumentSummaryInformation85CompObjjObjectPoolpLqpLq
FMicrosoft Word Document
MSWordDocWord.Document.89q