CSCI 132 Data Structures--Spring 2014

    Assignment 4

    Due at the beginning of class, Friday, March 14, 2014

    Home | | Syllabus | | Assignments | | Lecture Notes


    Programming Style: You will be graded not only on whether your programs work correctly, but also on programming style, such as the use of informative names, good comments, easily readable formatting, and a good balance between compactness and clarity (e.g. do not define lots of unnecessary variables) Create appropriate functions for tasks that can be encapsulated.


    Polynomial Calculator

    In this problem, you will implement the polynomial calculator described in the textbook, in section 4.5. Specifically, you will complete questions P2 (Write the Polynomial method equals_difference and integrate it into the calculator), P3 (Write the Polynomial auxilliary method mult_term to multiply a polynomial by a term, t, and integrate it into the calculator), P4 (Write a function, equals_product, that uses the mult_term and equals_sum functions to multiply two polynomials together), and P11 (Write a function that, given a polynomial and a real number, evaluates the polynomial at that number, and include this capability as a new command). The code from the textbook is available to you in the course directory:

      ~csci132/assignments/assign4

    Copy the files ( stack.h, stack.cc, queue.h, queue.cc, queue_array.h, queue_array.cc, polynomial.h, polynomial.cc and polyCalc.cc) from the above directory. The files include the code for the Stack class, code to implement the Queue class with an array, some of the code to implement the Queue class using linked lists, the Polynomial class, and the client program. 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 (the destructor, the copy constructor and the overloaded assignment operator):

      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 :: mult_term(Polynomial p, Term t)
      void Polynomial :: equals_product(polynomial p, Polynomial q)
      double Polynomial :: evaluate (double number)
      

    In polyCalc.cc:

      void do_command( ) -- Complete the switch statement to handle '-', '*', and 'e'.
      

    Note 1: 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.

    Note 2: A working Queue class is required for you to test your Polynomial class and the client program, polyCalc.cc, therefore, you should use the Queue class implemented with an array to test your homework. Currently, the include statement in polynomial.h is #include "queue_array.h". Use this implementation of the Queue class as you implement and test your polynomial calculator. Once you are sure the polynomial calculator is working, you can work on the queue.cc functions. Once you complete the methods in queue.cc you should change the include in polyCalc.cc to be #include "queue.h". This will allow you to re-test your polynomial calculator using the Queue class you just implemented.

    Compiling your program:
    Compile your program with the following compile command before you complete the methods in queue.cc:

      g++ -g -Wall queue_array.cc stack.cc polynomial.cc polyCalc.cc -o polyCalc


    Compile your program with the following compile command once you have completed the methods in queue.cc:

      g++ -g -Wall queue.cc stack.cc polynomial.cc polyCalc.cc -o polyCalc

    Make sure to test your programs with some sample polynomials for adding, subtracting, multiplying and evaluating polynomials to make sure the calculations are done completely. Turn in a printout of the following:

      polynomial.cc
      queue.cc
      polyCalc.cc

    Sample Output:

    Here is an example of what your output may look like:

    Welcome to the polynomial calculator! 
    This program allows you to perform arithmetic operations on polynomials
     and evalute them.
    
    First enter a command, ?, =, +, -, *, e, or q 
    Each command allows specific operations as follows: 
    ?: Allows entry of a polynomial.  
       Each term of the polynomial is specified by two integers.
       The first integer specifies the coefficient and 
       the second specifies the degree of the term.
       Terms must be entered in order of decreasing degree.
       Terms with coefficient of zero should be left out.
       End of entry is indicated with a zero coefficient.
    
    =: Print out polynomial at the top of the stack (the result of the last operation). 
    +: Add the two polynomials at the top of the stack.
    -: Subtract the two polynomials at the top of the stack. 
    *: Multiply the two polynomials at the top of the stack. 
    e: Evaluate the polynomial at the top of the stack. 
    Enter a command: ?, =, +, -, *, e, or q
    ?
    Enter the coefficients and exponents for the polynomial, 
    one pair per line.
    Exponents must be in descending order.
    Enter a coefficient of 0 or an exponent of 0 to terminate.
       coefficient? 3
       exponent? 3
       coefficient? 1
       exponent? 2
       coefficient? 2
       exponent? 0
    Enter a command: ?, =, +, -, *, e, or q
    =
    3 X^3 +  X^2 + 2
    Enter a command: ?, =, +, -, *, e, or q
    ?
    Enter the coefficients and exponents for the polynomial, 
    one pair per line.
    Exponents must be in descending order.
    Enter a coefficient of 0 or an exponent of 0 to terminate.
       coefficient? 3
       exponent? 3
       coefficient? 2
       exponent? 1
       coefficient? -2
       exponent? 0
    Enter a command: ?, =, +, -, *, e, or q
    =
    3 X^3 + 2 X - 2
    Enter a command: ?, =, +, -, *, e, or q
    -
    Enter a command: ?, =, +, -, *, e, or q
    =
     X^2 - 2 X + 4
    Enter a command: ?, =, +, -, *, e, or q
    ?
    Enter the coefficients and exponents for the polynomial, 
    one pair per line.
    Exponents must be in descending order.
    Enter a coefficient of 0 or an exponent of 0 to terminate.
       coefficient? 1
       exponent? 1
       coefficient? 2
       exponent? 0
    Enter a command: ?, =, +, -, *, e, or q
    =
     X + 2
    Enter a command: ?, =, +, -, *, e, or q
    *
    Enter a command: ?, =, +, -, *, e, or q
    =
     X^3 + 8
    Enter a command: ?, =, +, -, *, e, or q
    e
    Give an argument at which to evaluate the polynomial: 5
    5
    The polynomial:  X^3 + 8
        at 5 evaluates to 133
    Enter a command: ?, =, +, -, *, e, or q
    q
    Calculation finished.
    
    
    


    What to turn in:

    • Turn in a hard copy of the files:
        queue.cc
        polynomial.cc
        polyCalc.cc

      Make sure you include a file prologue with your name, class, lecture section, the date, and the assignment number, along with a description of the file contents and their purpose. Also include appropriate comments for each of the functions you implement (with proper pre and post conditions).
    • Turn in a discussion log.
    • Submit the soft copy of your program using the command:
      ~csci132/bin/submit csci132 assign4

    Home | | Syllabus | | Assignments | | Lecture Notes



    Computer Science 132--Data Structures
    Last Modified: February 27, 2014
    Page Expires: January 14, 2015