\documentclass[12pt]{article} \usepackage{times} \topmargin -0.3truein \headheight 0.3truein \headsep 0.3truein \footskip 0.6in \newcommand{\headerfont}{\fontfamily{phv}\fontseries{b}\fontshape{n}% \fontsize{11}{13pt}\selectfont} \newcommand{\ttb}{\fontfamily{pcr}\fontseries{b}\fontshape{n}% \fontsize{12}{14pt}\selectfont} \newcommand{\largebold}{\fontfamily{ptm}\fontseries{b}\fontshape{n}% \fontsize{15}{17pt}\selectfont} \newcommand{\Largebold}{\fontfamily{ptm}\fontseries{b}\fontshape{n}% \fontsize{19}{21pt}\selectfont} \newcommand{\Largeboldital}{\fontfamily{ptm}\fontseries{b}\fontshape{it}% \fontsize{19}{21pt}\selectfont} % %%%%%%%%%%%%%%%%%%% My Headings %%%%%%%%%%%%%%%%%%%%% % \makeatletter % Make '@' a letter to allow access to private macros % \def\ps@myheadings{ \def\@oddhead{\framebox[\mywidth]{\headerfont \rule[3.1mm]{0mm}{0mm} \hspace{0.7mm}\lefthead, \today \hfill Page~~\thepage~~of~~\pageref{'thatsall'}~~}} \def\@oddfoot{} \def\@evenhead{} \def\@evenfoot{} } \makeatother % Remove '@' a non-letter to disallow access to pvt macros %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % next line must come after the headings stuff! \pagestyle{myheadings} % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \newcommand{\mywidth}{6.0in} \newcommand{\lefthead}{CS1723: Data Structures, Assignment 3} \newcommand{\st}{\rule[-.8mm]{0mm}{2mm}} \newcommand{\longst}{\rule[-3mm]{0mm}{2mm}} \newcommand{\outerbaselinesep}{14pt} \topmargin 0.0in \oddsidemargin 0.25in \evensidemargin 0.25in \textwidth \mywidth \textheight 8.0in \parindent 0mm %10mm \parskip 3mm \fboxrule=0.3mm \begin{document} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % make everything sloppy \sloppy \begin{center} {\largebold The University of Texas at San Antonio \\ Computer Science Program \\ San Antonio, Texas 78249} \\ \vspace{0.4in} {\Largebold CS 1723, Data Structures \\ Spring Semester, 1997\longst \\ Programming Assignment 3, Due February 19, 1997\longst } \\ {\Largeboldital Translation to Reverse Polish} \end{center} \baselineskip=\outerbaselinesep \vspace{-0.1in} {\bf The Assignment:} For this assignment you will write {\em two} separate programs. Together, these should evaluate an input arithmetic expression. The first program, {\tt translate}, will translate the arithmetic expression to {\em reverse Polish} form. The second program, {\tt evaluate}, will find the value of the reverse Polish produced by the first program. %%%, and will carry out any indicated assignment operators. For the final runs, you can pipe the output of the first in as input to the second, so that you use the command: \hspace*{17mm}{\tt runner\% {\ttb translate }.) \end{itemize} \vspace{-0.1in} The translation algoritm will be discussed in class. Notice that for each non-whitespace character, the program must decide whether to output it or to stack it. Operands (digits and letters) are always output immediately. Operators (including parentheses) are always stacked, but in some cases not before one or more other operators are popped and output. The following rules apply: \vspace{-0.1in} \begin{enumerate} \item Always push a left parenthesis. \item Always push an operator if the stack is empty, if an operator of lower precedence is on top, or if an operator of equal precedence is on top and right-to-left associativity applies. \item Otherwise pop and output the stacktop, and try rules 1 and 2 again. \item A left and a right parenthsis (right above left) on the stack are always simply deleted. \item A \# character terminates the current input. Ouput a \# and terminate the program. \end{enumerate} \vspace{-0.1in} The result of the sample input above should be: \vspace{-0.2in} \begin{verbatim} b2^4a*c*-12/^b-2a*/# 32^41*2*-12/^3-21*/# \end{verbatim} \vspace{-0.1in} {\bf The initial {\ttb makefile} for {\ttb translate.c}:} \vspace{-0.1in} \begin{verbatim} # makefile for the translate program translate: translate.c tstack.c tstack.h cc -g -o translate translate.c tstack.c tlint: lint -m -u translate.c tstack.c \end{verbatim} \vspace{-0.1in} {\bf The second program {\ttb evaluate.c}:} Organize this second program as (at least) {\em three} files: {\tt evaluate.c}, {\tt estack.h}, and {\tt estack.c}. The evaluation of one of these RPN strings can proceed as described in the text and in class: Use an evaluation stack. Operands are pushed on the stack, and operators pop their arguments off the stack and push the result. When the final \# is encountered, the remainder of the stack is popped and printed. (It should just be a single value.) The arithmetic should be done using {\tt double}s. The stack {\em must} be implemented using linked lists like the other stack. (Notice that this stack holds {\tt double}s, while the other stack holds characters.) Thus the output of the second example should be the following: (Unless you are doing the Extra Credit part, the first example uses variable names which will not have a value and so cannot be evaluated.) \vspace{-0.2in} \begin{verbatim} -1.000000 \end{verbatim} \vspace{-0.2in} A single character that is a digit can be converted to a {\tt double} with \vspace{-0.2in} \begin{verbatim} #include ... if (isdigit(c)) return (double) (c - '0'); ... \end{verbatim} \vspace{-0.2in} The raise-to-a-power operator can be handled with the built-in function {\tt pow(x, y)} (see the white book, page 251). For this to work you need to include {\tt }, and (on ringer) you need to add {\tt -lm} to the compiler options, so that the math library will be searched, as shown in the {\tt makefile} below. The {\tt lint} program also needs this option as shown---without it {\tt lint} will produce 600 lines of warnings. {\bf The new {\ttb makefile} for both {\ttb translate.c} and {\ttb evaluate.c}:} \vspace{-0.1in} \begin{verbatim} # makefile for translate and evaluate programs all: translate evaluate translate: translate.c tstack.c tstack.h cc -g -o translate translate.c tstack.c evaluate: evaluate.c estack.c estack.h cc -g -o evaluate evaluate.c estack.c -lm tlint: lint -m -u translate.c tstack.c elint: lint -m -u evaluate.c estack.c -lm \end{verbatim} \vspace{-0.1in} {\bf Required Execution:} It will be enough to successfully execute the test program above, in two ways, once showing the intermediate RPN, and once using a pipe between {\tt translate} and {\tt evaluate}. However, for initial runs, you should test these separately and with very simple inputs. (Don't try the full input on the first run, and debug the two parts separately.) {\bf Extra Credit, if Bored with the Assignment:} You should include the assignment operator, so that your input can be a {\em sequence} of expressions, each separated by a semicolon, but with the whole still terminated by a {\tt \#}. Translation is similar, except that you flush the stack and start over with each {\tt ;}. For evaluation, one assumes that each variable is given a value before its use. During evaluation you will need to maintain a table of values of variables. An assignment causes a variable to take on a value. An expression without an assignment operator has its value printed. The hardest part of this extra credit part is maintaining the table of values of variables and keeping track of things on the evaluation stack, which you may want to expand to hold more information. Here is more elaborate sample input: \vspace{-0.2in} \begin{verbatim} a = 1; b = 3; c = 2; d = (b^2 - 4*a*c)^(1/2); r = (0 - b + d)/(2*a); s = (0 - b - d)/(2*a); r; s;# \end{verbatim} For this, the final evaluator would produce -2.0000 and -1.0000, while the intermediate reverse Polish form would be: \vspace{-0.2in} \begin{verbatim} a1=b03-=c2=db2^4a*c*-12/^=r0b-d+2a*/=s0b-d-2a*/=rs# \end{verbatim} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{'thatsall'} \end{document}