Templates in C++ / Polynomials

You need to complete Exercise 5 and create different templates/typedefs/classes. For this you'll need to take a quick look at Exercise 4. It's not a big project, you should be able to finish it in 1-2 hours.

Get Help With a similar task to - Templates in C++ / Polynomials

Login to view and/or buy answers.. or post an answer
Additional Instructions:

Exercise 5: Generic Polynomials by Templates The goal of this exercise is to implement multivariate polynomials with generic coefficient domains, as in Exercise 4. However, in contrast to Exercise 4, the implementation shall now be based on a class template; thus genericity is achieved by parametric polymorphism rather than by inheritance. In detail, your tasks are as follows: 1. First implement a class template template<class R> class Polynomial such that the objects of the resulting class represent multivariate polynomials over the coefficient domain R . The parameter R is assumed to denote a class that provides the same operations as those in Exercise 4. The representation and functionality of class template Polynomial is analogous to that of the class of Exercise 4. However, since the class resulting from the instantiation of this template is is not designed for inheritance with overriding, the operations need not be virtual. 2. Next, use class template Polynomial and class Integer (you may use the same class as in the previous exercise) to create by the definition typedef Polynomial<Integer> IntPoly; a type IntPoly that implement multivariate polynomials with integer coefficients. This class shall have the same functionality as the corresponding class of Exercise 4. Test this type in the same way as in Exercise 4. 3. Now also create a class Rational class Rational: public Ring { public: // rational with value n/m (default 0/1) Rational(long n=0, long m=1); }; that implements rational numbers as pairs of long values with the same operations as provided by class Integer. Create by the definition typedef Polynomial<Rational> RatPoly; a type RatPoly that implement multivariate polynomials with rational coefficients. Also test this class comprehensively. 2

Exercise 4: Generic Polynomials by Inheritance Write a program that implements a type Polynomial of multivariate polynomials in distributive representation: here a polynomial p ∈ R[x1, . . . , xn] symbolically written as k∑ i=1 cix e1,i 1 · · · x en ,i n is represented by a sequence [m1, . . . ,mk] of k monomials where each mi is a pair 〈ci, [e1,i, . . . , en,i]〉 of a non-zero coefficient ci ∈ R and a sequence [e1,i, . . . , en,i] of n exponents (natural numbers) representing the power product of the monomial. We sort the monomials m1, . . . ,mk in reverse lexicographic order where monomial ma occurs before monomial mb, if for some variable index j we have ea, j > eb, j and for every index j ′ < j we have ea, j′ = eb, j′. For instance, the polynomial 3xy2 + 5x2y + 7x + 11y + 13 in Z[x, y] is represented by the sequence [〈5, [2,1]〉, 〈3, [1,2]〉, 〈7, [1,0]〉, 〈11, [0,1]〉, 〈13, [0,0]〉] Every polynomial has thus a unique representation; please note that the zero polynomial is represented by the empty sequence. In our program, we allow R to be any type that supports the usual ring operations, i.e., Polynomial is generic in its coefficient domain. To achieve this, proceed as follows: 1. Take the following abstract class Ring: class Ring { public: // destructor virtual ~Ring() {} // the string representation of this element virtual string str() = 0; // the constant zero of the type of this element virtual Ring* zero() = 0; // sum of this element and c virtual Ring* operator+(Ring* c) = 0; // comparison function virtual bool operator==(Ring* c) = 0; }; 2. Implement a concrete class Integer 1 class Integer: public Ring { public: // integer with value n (default 0) Integer(long n=0); }; This class overrides all the abstract (pure virtual) operations of class Ring by concrete definitions for integer arithmetic (where integers are represented by long values). Note that the definition of the arithmetic and comparison functions the parameter c must be explicitly converted from type Ring* to type Integer*. Use the expression dynamic_cast<Integer*>(c) to receive a pointer to a Integer object (respectively 0, if the conversion is not possible; the program may then be aborted with an error message). 3. Implement a concrete class Polynomial class Polynomial: public Ring { public: // zero-polynomial in n variables with given names Polynomial(int n, string* vars); // add new term with given coefficient and exponents to this polynomial // and return this polynomial Polynomial& add(Ring* coeff, int* exps); // destructor virtual ~Polynomial(); }; which implements polynomials with generic coefficient types (i.e., coefficients that are represented by a concrete subclass of class Ring). A Polynomial object shall be represented by a) the number of variables, b) a pointer to an array of the variable names, c) a pointer to a heap-allocated array of monomials in these variables, d) the length of this array, e) the actual number of monomials in this array. Each monomial holds a pointer to its coefficient (of type Ring*) and a pointer to the array of exponents. At any time, the array shall hold as many monomials as are indicated by the number value which is less than or equal the length value; these monomials hold coefficients different from 0, are unique with respect to their exponents and are sorted in reverse lexicographic order. It might be a good idea to introduce a class Monomial such that the monomial array in Polynomial holds elements of this type (respectively pointers to such elements, depending on your design choice). When a new polynomial is created, we allocate an empty array of some default size. When a new monomial is added, we first check its coefficient; if it is zero, the monomial is ignored. Otherwise, we search in the array for the position where 2 a) either a monomial with the given exponent sequence already occurs; in this case, the given coefficient is added; if the resulting coefficient is zero, the monomial is discarded from the array and all subsequent monomials are shifted to fill the gap; b) or, if there is no such exponent sequence, a new monomial is to be inserted; the subsequent monomials have to be shifted to make room for the new monomial. Class Polynomial is itself a concrete subclass of Ring; it thus overrides the abstract (pure virtual) operations of class Ring by concrete definitions for polynomial arithmetic. Please note the following: • Class Polynomial does not use the class Integer described above, it is only based on the class Ring representing the coefficient domain. • No memory leaks shall arise from the implementation. Thus the class needs a destructor that frees the memory allocated for the object. • Check in the addition of polynomials whether the number of variables in both poly- nomials match; if not, abort the program with an error message. • For printing a polynomial, remember that the interpretation of a polynomial with an empty monomial sequence is 0. Avoid any code duplication butmake extensive use of auxiliary functions (that shall become as far as possible private member functions of Polynomial). Write the declaration of Polynomial into a file Polynomial.h and the implementation of all non-trivial member functions of Polynomial and of into a file Polynomial.cpp. Write a file PolynomialMain.cpp that uses Polynomial and tests its operations comprehen- sively. Test each operation with at least three test cases that also include special cases (such as adding a zero polynomial). Test the program also with the following piece of code: // variable names and exponent vectors ("power products") string vars[2] = { "x", "y" }; int e1[2] = {1,2}; int e2[2] = {2,1}; int e3[2] = {1,0}; int e4[2] = {0,1}; int e5[2] = {0,0}; int e6[2] = {2,2}; // construct polynomials p and q in two variables Polynomial* p = new Polynomial(2, vars); p->add(new Integer(3),e1).add(new Integer(5),e2).add(new Integer(7),e3) .add(new Integer(11),e4).add(new Integer(13),e5); Polynomial* q = new Polynomial(2, vars); q->add(new Integer(11),e4).add(new Integer(-3),e2).add(new Integer(2),e6) .add(new Integer(-2),e2); // print p and q cout << p->str() << endl << q->str() << endl; // set r to p+2*q and print it Polynomial* r = dynamic_cast<Polynomial*>(p->operator+(q)->operator+(q)); cout << r->str() << endl; 3

Related Questions

Similar orders to Templates in C++ / Polynomials
34
Views
0
Answers
Build a Simple Text Adventure Game for Beginner C++
I need a small, simple text adventure game made for my Intro to C++ class. The assignment is due at 11:30PM tonight, so it is urgent. There are a few requirements listed in the attached photo, but overall the expectation is low because it is an intro class...
44
Views
0
Answers
Build a simple text adventure game
Assignment Prompt: Build A Simple Text Adventure Game For your final project, your instructor would like to see your creativity in coding! Your instructor has tasked you with creating a text adventure game ( they were really popular in the 1980's - ht...
34
Views
0
Answers
multiple c++ programs with screen shots
Please put your name on line 4, compile it, and show the following program's output (screen capture). 1.// This program is used by General Crates, Inc. to calculate 2.// the volume, cost, customer charge, and profit of any crate 3// to be built, when g...
27
Views
0
Answers
Help Coding FIFOS Functions in C++ with Linux
The instructions are in folder with all files, please add explanations so I can study them, price can be discussed as long as you can do the task, thanks...