old business write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm –...
TRANSCRIPT
Old Business• Write mypaste.c and mycomm.c– mypaste is like paste & mycomm is like comm– Both take two files as arguments– Paste reads a line from each file and outputs them to
stdout on the same line– Comm compares the two lines with strcmp and outputs
the one that comes first in alphabetical order (with the appropriate number of tabs)
New Business• Please install R for next time
– http://www.r-project.org/ • Homework for next time
– Add some more functionality to calculator: • 4 2 choose• 2 4 ^ (extra credit: need pow in math.h)
– http://www.network-theory.co.uk/docs/gccintro/gccintro_17.html
• 4 ! (extra credit: first example of a function with a single arg)– Show that your calculator produces the right answers for the
example(s) above– Show parse trees in Graphviz for
• choose(5,2)• 5/(2 * 3)• Extra credit: 5!/(2! * 3!)
How Does Recursion Work?
Reverse Polish Notation
• 1 2 4 * + 3 +• (1 + (2 * 4)) + 3
• Eval: substitute <arg1> <arg2> <op> with value– 1 2 4 * + 3 +– 1 8 + 3 +– 9 3 +– 12
polish.c1+2*4+3
A Stack
polish.c1+2*4+3
Parse Tree: (1 + (2 * 4)) + 3echo ‘1 2 4 * + 3 +’ | tr ‘ ‘ ‘\n’ | ./polish2 | egrep ‘^#’ | cut –c2- > G.gv
Summary
• Wrote an interpreter in C– Input: expressions (e.g., 1 1 +)– Outputs: values (e.g., 2)
• 4-function calculator: +, -, *, /– Reverse Polish Notation
• Stack: push, pop (foundation behind recursion)• Interpreter: read (parse), eval, print• Used graphviz to display parse tree– Example of combining programs: C, Graphviz, grep