CS215 - Introduction to Program Design, Abstraction, and Problem Solving

Fall, 2002

Programming Assignment #2

Assigned: Thursday, October 24th, 2002

External Documentation Due: in class on Tuesday, November 5th, 2002

Listing and Executions Due: by e-mail by 11:59 pm on Tuesday, November 12th, 2002

Design, write, and execute a C++ program that manipulates mathematical polynomials, allowing the user to work with an ongoing, "current" polynomial by repeatedly choosing actions from the following menu:

In case you need a refresher, a polynomial is a sequence of terms added together, each term being of the form "cx^e", where c is the term's coefficient, x is the variable of the entire polynomial, and e is the term's exponent. Thus, an example polynomial might be

   3x^2 + -4x^1 + 2x^0
which is read "3 x to the 2nd power plus negative 4 x to the 1st power plus 2 x to the zeroth power". Because x^1 is really just x, and x^0 is really just 1, we can write this as
   3x^2 - 4x + 2
which is read, more simply, as "3 x squared minus 4 x plus 2".

If you need a reminder about how to add, subtract, multiply, or evaluate polynomials, you may want to take a look at the sample executions below, refer to a basic algebra textbook, and/or check with your friendly CS215 instructor.

Here are some requirements for your program:

You should write this program by extending the LList class that we are currently developing in our CS215 lecture to represent each polynomial, and by (slightly) revising the listnode class that we are currently using in lecture to represent each polynomial term. As always, in order to make your main function relatively modular and easy to read, you should also include additional methods and/or functions as necessary. Here are some hints to make your coding easier: Be sure to follow the overall requirements for programming assignments found in the document called "Programming Requirements" posted on the CS215 Web Homepage. You'll need to develop and run your own thorough set of testcases, as well as run the set of required testcases I will make available shortly before the program listings and executions are due.

Be sure to submit your External Documentation printed out on paper, using the MS Word word-processing program and the MS Word "External Documentation Outline" provided on the CS215 Web Homepage.

Be sure to submit your Listing and Executions as two separate attachments to a single piece of e-mail sent from your SWEB computer account using the "pine" e-mail program. For your Listing, you should simply attach your C++ program (which should be in a file called "program2.cc"). For your Executions, you should attach your script file (which should be in a file called "program2.script"). Make sure that your script file shows that your program compiles using the g++ command on the SWEB Unix computer system, and shows both the required testcases and your own proposed testcases being executed by your program running on the SWEB Unix computer system. Be sure to use the following Subject: line in your e-mail, based on your CS215 section number:

Following is a sample of what your program executions should look like when completed.

Good luck!



Polynomial Manipulation Package, Version 1.0 (c) 2002, (Your Name)

Current polynomial is P(x) = 0

Command (h for help): h

valid commands are:
   r    reset        reset the current polynomial by entering a new one
   a	add 	     add a new polynomial to the current polynomial
   s    subtract     subtract a new polynomial from the current polynomial
   m    multiply     multiply a new polynomial into the current polynomial
   e    evaluate     evaluate the current polynomial at a given value of x
   q    quit         quit the program

Current polynomial is P(x) = 0

Command (h for help): r

Resetting the current polynomial.

Enter coefficient and exponent (0 0 to stop): -2 1
Enter coefficient and exponent (0 0 to stop): 4 3
Enter coefficient and exponent (0 0 to stop): 5 0
Enter coefficient and exponent (0 0 to stop): 0 0

Entered polynomial is Q(x) = 4x^3 - 2x + 5

Resetting completed.

Current polynomial is P(x) = 4x^3 - 2x + 5

Command (h for help): a

Adding a new polynomial to the current polynomial.

Enter coefficient and exponent (0 0 to stop): 3 2
Enter coefficient and exponent (0 0 to stop): -2 3
Enter coefficient and exponent (0 0 to stop): 2 1
Enter coefficient and exponent (0 0 to stop): 0 0

Entered polynomial is Q(x) = -2x^3 + 3x^2 + 2x

Addition completed.

Current polynomial is P(x) = 2x^3 + 3x^2 + 5

Command (h for help): m

Multiplying a new polynomial into the current polynomial.

Enter coefficient and exponent (0 0 to stop): 3 2
Enter coefficient and exponent (0 0 to stop): 1 1
Enter coefficient and exponent (0 0 to stop): 0 0

Entered polynomial is Q(x) = 3x^2 + x

Multiplication complete.

Current polynomial is P(x) = 6x^5 + 11x^4 + 3x^3 + 15x^2 + 5x

Command (h for help): s

Subtracting a new polynomial from the current polynomial.

Enter coefficient and exponent (0 0 to stop): 8 7
Enter coefficient and exponent (0 0 to stop): 3 4
Enter coefficient and exponent (0 0 to stop): 0 0

Entered polynomial is Q(x) = 8x^7 + 3x^4

Subtraction completed.

Current polynomial is P(x) = -8x^7 + 6x^5 + 8x^4 + 3x^3 + 15x^2 + 5x

Command (h for help): r

Resetting the current polynomial.

Enter coefficient and exponent (0 0 to stop): 1 1
Enter coefficient and exponent (0 0 to stop): 2 1
Enter coefficient and exponent (0 0 to stop): 3 1
Enter coefficient and exponent (0 0 to stop): 2 3
Enter coefficient and exponent (0 0 to stop): -2 3
Enter coefficient and exponent (0 0 to stop): 0 4
Enter coefficient and exponent (0 0 to stop): 5 2
Enter coefficient and exponent (0 0 to stop): 0 0

Entered polynomial is Q(x) = 5x^2 + 6x

Resetting completed.

Current polynomial is P(x) = 5x^2 + 6x

Command (h for help): e

Evaluating the current polynomial.

Enter a value for x: 3

P(3) = 63

Evaluation completed.

Current polynomial is P(x) = 5x^2 + 6x

Command (h for help): q

Quitting Polynomial Manipulation Package.