Home | | Syllabus | | Assignments | | Lecture Notes
In this problem, you will implement the polynomial calculator described in the textbook, in section 4.5. The code from the textbook is available to you in the course directory:
Copy the files from the above directory. You will have all the code for implementing the stack class, and some of the code to implement the Queue class using linked lists and the polynomial class. Your job is to complete the code in queue.cc, polyCalc.cc and polynomial.cc to generate a working polynomial calculator.
To complete the assignment, fill in the correct code for the following function stubs:
In queue.cc:
Queue :: ~Queue( ) Queue :: Queue(const Queue &original) void Queue :: operator = (const Queue &original)
In polynomial.cc:
void Polynomial :: equals_difference(Polynomial p, Polynomial q) void Polynomial :: equals_product(Polynomial p, Polynomial q) void Polynomial :: mult_term(Polynomial p, Term t) We will leave the equals_quotient() function for another day!
In polyCalc.cc:
void introduction() void instructions() void do_command( ) -- Complete the switch statement to handle '*' and '-'.Give a reasonable introduction and set of instructions for working this calculator.
Note: mult_term() should work the same way as the other Polynomial functions. That is, the result should be stored in the calling polynomial. Thus, if r and s are Polynomials, and theTerm is a Term, then mult_term could be called as follows:
s.mult_term(r, theTerm);The result of multiplying r times theTerm is stored in s. To use this in equals_product, you should create a temporary polynomial to hold the result of multiplying one of the polynomials by individual terms of the other polynomial.
Compiling your program:
Compile your program with the following compile command:
Make sure to test your programs with some sample polynomials for adding, subtracting and multiplying to make sure the calculations are done completely. Turn in a printout of the following:
Ackermann's function, defined as follows, is a standard device to determine how well recursion is implemented on a computer.
A(0, n) = n + 1 for n >= 0. A(m, 0) = A(m - 1, 1) for m > 0. A(m, n)= A(m - 1, A(m, n - 1)) for m > 0 and n > 0.
(a) Write a recursive function to calculate Ackermann's function. Name your file ackermann.cc. The main program should prompt for 2 positive integers (m and n) and read them in, then output the return value for a call to the recursive ackermann function.
Sample output:
mathcs% ackerman Enter positive integer for m: 0 Enter positive integer for n: 0 The ackermann value is 1. mathcs% ackerman Enter positive integer for m: 0 Enter positive integer for n: 9 The ackermann value is 10. mathcs% ackerman Enter positive integer for m: 1 Enter positive integer for n: 8 The ackermann value is 10. mathcs% ackerman Enter positive integer for m: 2 Enter positive integer for n: 2 The ackermann value is 7. mathcs% ackermann Enter positive integer for m: 4 Enter positive integer for n: 0 The ackermann value is 13. mathcs% ackerman Enter positive integer for m: 3 Enter positive integer for n: 2 The ackermann value is 29.
(b) Try running your program for m = 4, n= 2 and for m = 4, n=3. What happens? Explain why you think this happens, using a partial drawing of the recursion tree for m = 4, n = 2 to illustrate your answer.
Home | | Syllabus | | Assignments | | Lecture Notes
Constance Royden--croyden@mathcs.holycross.edu
Computer Science 262--Data Structures
Date Created: February 7, 2002
Last Modified: February 27, 2004
Page Expires: January 14, 2005