|
CSIS 250 Spring 2000 Assignment #7Date Assigned: April 19, 2000 Date Due: April 26, 2000 at 10:00 AM Develop the constructor for the CPolynomial class to construct a polynomial from a given array of characters. Use the following test function unchanged in the polynomial3.cpp program and replace the CPolynomial constructor with your constructor. Your constructor should work for every valid polynomial string. Submit the following: 1. Printed source code of the constructor function for CPolynomial 2. Printed source code of the test function 3. Printed sample run 4. Diskette with the entire source code and the executable code ////////////////////////////////////////////////////////////////////// //string constructor for CPolynomial //Initializes a polynomial with value from a string ////////////////////////////////////////////////////////////////////// CPolynomial::CPolynomial(const char *cp) { //insert your code here } Test function: /*------------------------------------------------------------------*/ //testConstructorStringCPolynomial /*------------------------------------------------------------------*/ void testConstructorStringCPolynomial(void) { cout << "testConstructorStringCPolynomial\n"; cout << "--------------------------------\n"; char *test[]= { "0", "1", "x", "2*x", "x^3", "4*x^4", "x^5", "-1", "-x", "-2*x", "-x^3", "-4*x^4", "-x^5", "1+x", "1+x+2*x^2", "1+x+2*x^2+3*x^3", "1+x+2*x^2+3*x^3+4*x^4", "1+x+2*x^2+3*x^3+4*x^4+5*x^5", "1-x", "1+x-2*x^2", "1+x-2*x^2+3*x^3", "1+x-2*x^2+3*x^3-4*x^4", "1+x-2*x^2+3*x^3-4*x^4+5*x^5", "1+x+2*x+x^3+4*x^4+x^5", "-1-x+2*x-x^3-4*x^4-x^5", }; for (int i=0; i<sizeof(test)/sizeof(test[0]); i++) { cout << "input #" << i+1 << ": " << test[i] << endl; CPolynomial poly(test[i]); cout << poly << endl; cout << "========================================\n"; } char testString[80]; do { cout << "Input string ('end' to exit):"; cin >> testString; if (strcmp(testString,"end")==0) break; { cout << "input: " << testString << endl; CPolynomial poly(testString); cout << poly << endl; cout << "========================================\n"; } } while (strcmp(testString,"end")!=0); } Sample RUN: ========== testConstructorStringCPolynomial -------------------------------- input #1: 0 0 p(x,1) =0 ======================================== input #2: 1 1 p(x,1) =+1*x^0 ======================================== input #3: x x p(x,1) =+1*x^1 ======================================== input #4: 2*x 2*x p(x,1) =+2*x^1 ======================================== input #5: x^3 x^3 p(x,1) =+1*x^3 ======================================== input #6: 4*x^4 4*x^4 p(x,1) =+4*x^4 ======================================== input #7: x^5 x^5 p(x,1) =+1*x^5 ======================================== input #8: -1 -1 p(x,1) =-1*x^0 ======================================== input #9: -x -x p(x,1) =-1*x^1 ======================================== input #10: -2*x -2*x p(x,1) =-2*x^1 ======================================== input #11: -x^3 -x^3 p(x,1) =-1*x^3 ======================================== input #12: -4*x^4 -4*x^4 p(x,1) =-4*x^4 ======================================== input #13: -x^5 -x^5 p(x,1) =-1*x^5 ======================================== input #14: 1+x 1 +x p(x,2) =+1*x^1+1*x^0 ======================================== input #15: 1+x+2*x^2 1 +x +2*x^2 p(x,3) =+2*x^2+1*x^1+1*x^0 ======================================== input #16: 1+x+2*x^2+3*x^3 1 +x +2*x^2 +3*x^3 p(x,4) =+3*x^3+2*x^2+1*x^1+1*x^0 ======================================== input #17: 1+x+2*x^2+3*x^3+4*x^4 1 +x +2*x^2 +3*x^3 +4*x^4 p(x,5) =+4*x^4+3*x^3+2*x^2+1*x^1+1*x^0 ======================================== input #18: 1+x+2*x^2+3*x^3+4*x^4+5*x^5 1 +x +2*x^2 +3*x^3 +4*x^4 +5*x^5 p(x,6) =+5*x^5+4*x^4+3*x^3+2*x^2+1*x^1+1*x^0 ======================================== input #19: 1-x 1 -x p(x,2) =-1*x^1+1*x^0 ======================================== input #20: 1+x-2*x^2 1 +x -2*x^2 p(x,3) =-2*x^2+1*x^1+1*x^0 ======================================== input #21: 1+x-2*x^2+3*x^3 1 +x -2*x^2 +3*x^3 p(x,4) =+3*x^3-2*x^2+1*x^1+1*x^0 ======================================== input #22: 1+x-2*x^2+3*x^3-4*x^4 1 +x -2*x^2 +3*x^3 -4*x^4 p(x,5) =-4*x^4+3*x^3-2*x^2+1*x^1+1*x^0 ======================================== input #23: 1+x-2*x^2+3*x^3-4*x^4+5*x^5 1 +x -2*x^2 +3*x^3 -4*x^4 +5*x^5 p(x,6) =+5*x^5-4*x^4+3*x^3-2*x^2+1*x^1+1*x^0 ======================================== input #24: 1+x+2*x+x^3+4*x^4+x^5 1 +x +2*x +x^3 +4*x^4 +x^5 p(x,5) =+1*x^5+4*x^4+1*x^3+3*x^1+1*x^0 ======================================== input #25: -1-x+2*x-x^3-4*x^4-x^5 -1 -x +2*x -x^3 -4*x^4 -x^5 p(x,5) =-1*x^5-4*x^4-1*x^3+1*x^1-1*x^0 ======================================== Input string ('end' to exit):x+x input: x+x x +x p(x,1) =+2*x^1 ======================================== Input string ('end' to exit):x+x+x+2*x input: x+x+x+2*x x +x +x +2*x p(x,1) =+5*x^1 ======================================== Input string ('end' to exit):end Press any key to continue
|