Implement a C++ class supporting a very minimal set of operations on polynomials with integer coefficients. You implementation should cause the following mainline to "run correctly":
#include <cstdio> #include <cstdlib> #include "Poly.h" void usage(const char* exeName) { printf("Usage: %s <coefficient list>\n", exeName); exit(1); } int main(int argc, char* argv[]) { if ( argc < 2 ) usage(argv[0]); int *e = new int[argc-1]; for ( int i=1; i<argc; i++ ) { e[i-1] = atoi(argv[i]); } Poly *f = new Poly(e, argc-2); delete [] e; // If you new'ed an array, you have to use "delete []" printf("f(2) = %lf\n", f->print()->add(f)->print()->multiply(f)->print()->eval(2)); delete f; // If you new'ed a scalar, you just delete it }Here's an example of "running correctly." The command line arguments are polynomical coefficients, from lowest to highest order.
$ g++ -Wall -std=gnu++0x -g -o ex5 main.cc Poly.cc $ ./ex5 1 1 1 Degree 2: x^2 + x + 1 Degree 2: 2x^2 + 2x + 2 Degree 4: 4x^4 + 8x^3 + 12x^2 + 8x + 4 f(2) = 196.000000 $ ./ex5 0 Degree 0: 0 Degree 0: 0 Degree 0: 0 f(2) = 0.000000 $ ./ex5 -1 1 -2 2 Degree 3: 2x^3 - 2x^2 + x - 1 Degree 3: 4x^3 - 4x^2 + 2x - 2 Degree 6: 16x^6 - 32x^5 + 32x^4 - 32x^3 + 20x^2 - 8x + 4 f(2) = 324.000000
The main point of this exercise is to define a class and get over the C++ hurdles to compile and run it; we're not all that fixated on the format of the output.
You're free to use any C++ you want. All we're expecting, though, is that you write C code and package it into a C++ class.