Exercise 5

You cannot submit for this problem because the homework's deadline is due.

Exercise 5

This exercise mainly focuses on exception, ADT and inheritance. The deadline is Nov. 13th.

Polynomial

An integer polynomial is a math expression in the form of \(a_0+a_1\cdot x^1+\dots+a_n\cdot x^n\).

For this exercise, you're asked to implement a polynomial class which should support the following functions:

  • Default Constructor
  • Set Coefficient
  • Get Coefficient
  • Get Degree
  • Print

The class provides the following data structures (attributes):

  • An integer array(lets say coef) which holds the coefficient of each degree. Use array indices as degree and coef[i] as coefficient of i-th degree.
  • An unsigned integer degree, representing the degree of the polynomial.

Quadratic Polynomial

A quadratic polynomial is a special type of polynomial that the degree is 2. It can be written in the form of \(ax^2+bx+c\), where \(a\neq 0\). The QuadraticPoly subtype inherits from the super-type Polynomial.

Implement the quadratic polynomial class, which inherits from the polynomial class and overwrite the print and setCoefficient function.

Program Prototype

#ifndef _POLYNOMIAL_H
#define _POLYNOMIAL_H
#include <string>

#define MAX_DEGREE 100

class Exception{
    
    std::string message="";

public:
    Exception(const std::string & message): message(message) {}
    std::string what() {return message;}
};

class Polynomial {

protected:
    // the coefficient of each term
    int coef[MAX_DEGREE] = {0}; 
    // the degree of the polynomial
    unsigned int degree;

public:

    /**
     * @brief Default Constructer. Initialize all the coefficient to be zero,
     * and degree to be 0
     */
    Polynomial();
    
    /**
     * @brief Set the Coefficient of the term for the degree given, 
     * and update the polynomial degree.
     * @param degree 
     * @param coef 
     * @throw If the given degree is greater or equal to MAX_DEGREE, throw an Exception 
     * object with error message "Exceed maximum degree!".
     */
    virtual void setCoefficient(unsigned int degree,int coef);
    
    /**
     * @brief Get the Coefficient of the term for the degree given.
     * 
     * @param degree 
     * @throw If the given degree is greater than the degree of the polynomial
     * , throw an Exception object with error message "No term of such degree!".
     */
    int getCoefficient(unsigned int degree) const;
    
    /**
     * @brief Print the polynomial with format "<coef>x^<deg>" to stdout,
     * from highest degree to lowest, skip all the zero terms.
     * Separte every term and every "+" with an additional space.
     * Add a new line after printing the whole polynomial.
     * @example 3x^2 + 4
     * @example -1x^3 + 2x^2 + -5x^1 + 1
     * @example 0
     */
    virtual void print() const;
    
    /**
     * @return unsigned int, the degree of this polynomial
    */
    unsigned int getDegree() const;
    
    ~Polynomial() = default;

};

class QuadraticPoly : public Polynomial{

public:
    /**
     * @brief Default Constructer. Initialize the polynomial in to 1x^2,
     * and degree to be 2
     */
    QuadraticPoly();

    /**
     * @brief Set the Coefficient of the term for the degree given.
     * @param degree 
     * @param coff 
     * @throw If the given degree is greater than 2, throw an Exception 
     * object with error message "Exceed maximum degree!".
     * @throw If the given degree is 2 and the given coef is 0, throw an Exception
     * object with error message "Qudratic term is zero!".
     */
    void setCoefficient(unsigned int degree,int coef);
    
    /**
     * @brief First print out "This is a quadratic polynomial!" with a newline. 
     * Then print as normal as a polynomial behaves.
     */
    void print() const;
    
    ~QuadraticPoly() = default;

};

#endif // _POLYNOMIAL_H

Submission

Implement the above functions in a file polynomial.cpp, compress the file only and submit on JOJ. The deadline is Nov. 13th.

Testing

To test the correctness of your implementation by your self, you need another driver program (say main.cpp). Below is a simple example:

#include <iostream>
#include "polynomial.h"

using namespace std;

int main(){
    int test_num;
    cin >> test_num;
    try {
        switch (test_num){
        case 1:
            {
                Polynomial p;
                p.print();
            }
            break;
        case 2:
            {
                Polynomial p;
                cout << p.getDegree();
            }    
        default:
            break;
        }
    }
    catch (Exception ex){
        cout << ex.what();
    }
    return 0;
}

Then compile the program using (or you can write Makefile on your own)

g++ -o polynomial polynomial.cpp main.cpp -Wall

Exercise 5

Not Claimed
Status
Finished
Problems
1
Open Since
2022-11-04 08:30
DDL
2022-11-13 23:59
Extension
0.0 hour(s)