Polynomials

Summary: Create a system which can display and manipulate polynomials. For example: (2x-3)*(2x+3) = (4x^2)-9

Right away, we should limit User-Interface expectations. You need not make a very fancy output. A terminal interface suffices. Let us keep the input "language" as simple as possible and still do the job. [Read a line. Process it. Print something on the terminal.] repeat

Recognize the following kinds of lines: A polynomial. Examples: code 3x2 -7 x2 + 2x3 - 3x4 -3x0 0 x 2 -x -x +3 2x + 3x +4x -x - 8x code Once entered, polynomials should be in canonical form. The above examples give: code 3x2 -7 -3x4 + 2x3 + x2 -3 0 x -x + 2 -x + 3 0 code although while computing intermediate results, you may hold non-normalized polynomials. While debugging your code, you might want to print out non-normalized forms.

Other "commands": code ? Display the stack and anything else worth showing (unless you do this before every input, simulating a calculator which always displays its stack)

+ add the top two polynomials on the stack and push the result. - (2nd item on stack) - (top item) and push the result / (2nd item on stack) / (top item) and push the result (whole quotient) % (2nd item on stack) % (top item) and push the result (remainder) ^ (2nd item on stack) ^ (top item) and push the result (exponentiation would be cool enough even if the exponent was constrained to be a positive integer)
 * pop * pop and push the result

Duplicate the top thing on the stack s - swap the top two items ~ - forget the top item clear - clear the whole stack (requiring the whole word obviates confirmation) code In class, I was too wishy-washy in reversing the order of operands on the stack for non-commutative operators. It's NOT that hard for you to make it "right". There's good precedent and good reason to put them in the other order. Suppose you were doing physics and had an expression on top of the stack. You want to divide by 2. You'd push "2 enter /". No swapping necessary.

RPN Calculator If you have never played with an RPN calculator see one of these: [] [] [] [| http://www.ecalc.com/] This on comes up showing "ALG" (algebraic mode) at the top. Click that to toggle to RPN. Observe that the convention is to display the execution stack upside-down.

The P in RPN: []

This project encourages you to re-use several of the ideas you have developed in earlier projects.

As usual, test as you go along. For example, I defined my structure, then made some examples and got the printing to work. I implemented + and tested it. Later I worked on reading in the same kind of strings that were put out. Then I implemented * which makes repeated use of + and then normalizes. I never had more than one method (function) which had not yet been tested.

As usual, run your product through its paces and include a log or screen-shots in your write-up. Tar or zip all your files into one bundled file and submit via Moodle.

Polynomial Games. . Polynomial C program