Answered step by step
Verified Expert Solution
Question
1 Approved Answer
intmodp.h /* The class intmodp exports modular integer arithmic, * modulo some prime number. */ #ifndef __MODINT_H__ #define __MODINT_H__ #include class intmodp { public: static
intmodp.h /* The class intmodp exports modular integer arithmic, * modulo some prime number. */ #ifndef __MODINT_H__ #define __MODINT_H__ #includeclass intmodp { public: static int prime; // The prime number used as modulus // is a class wide variable. intmodp ( void ); // Without arguments, the constructor // sets value of the number to zero. intmodp ( int n ); // The value of the number is n % prime. int value ( void ) const; // Returns the value of the number. intmodp operator+ ( const intmodp& n ); // Adds n to the number. intmodp operator- ( const intmodp& n ); // Subtracts n from the number. intmodp operator* ( const intmodp& n ); // Multiplies the number with n. intmodp inverse ( void ) const; // Returns the inverse of the nonzero number. intmodp operator/ ( const intmodp& n ); // Divides the number by the nonzero n. friend std::ostream& operator> ( std::istream& os, intmodp& n ); // Defines the input of the number n. private: int modpvalue; // the value of the number }; #endif
_____________________________________________________________________________________________________
polynomialtest.cpp #include#include #include #include "intmodp.h" #include "polynomial.h" using namespace std; polynomial random ( int deg ); /* * Returns a random polynomial of degree deg. */ void do_arithmetic ( polynomial p, polynomial q ); /* * Tests the arithmetic on the polynomials p and q. */ void test_arithmetic ( void ); /* * Prompts the user for two polynomials and shows * the sum, difference, and product. */ int main ( void ) { cout > deg; polynomial first = random(deg); cout > deg; polynomial second = random(deg); cout the first random polynomial : " the second random polynomial : " > first; cout your first polynomial : " > second; cout your second polynomial : " The purpose of this project is to apply the STL vector class to represent polynomials in one variable and to define the basic arithmetic operations with operator overloading. Instead of working with rational or floating-point coefficients, the coefficients of the polynomials are integer numbers modulo some prime number. These numbers are defined in the class intmodp, available via the course web site. You must use this class. The UML class diagrams are shown below. polynomial intmodp - degree + prime - vector - modpvalue + polynomial() + intmodp + polynomial(c,d) + intmodp(n) + deg + value() + cff(a) + operator+(n) + operator+(p) + operator-(n) + operator-(p) + operator*(p) + operator*(n) + divmod (p,q,r) + inverse() + operator/(p) + operator/(n) + operator%(p) + operator>(i,n) + operator>>(i,p) For the explanation of the intmodp class, see the posted file intmodp.h. The two private data attributes of a polynomial are its degree d and its coefficients, stored as a vector of d + 1 many integers modulo some prime. The number at position k in the vector is the coefficient with the monomial zk. There are two constructors. The constructor without argument makes the number zero. The degree of the number zero is -1. The other constructor has two arguments: a nonzero coefficient c and the degree d for the monomial erd. The method deg() returns the degree of the polynomial. The method cff(d) returns the coefficient of the monomial z", which may be zero, for example when d is larger than the degree of the polynomial. The basic arithmetical operations on polynomials are defined by the operators +, -, *, /, and %. An addition and subtraction may cause the cancellation of the leading coefficients. The operators must ensure that the degree of the result is correct, that is: for any polynomial p, p.cff (p.deg() is not equal to zero. The method divmod() has three arguments: on input is a divisor polynomial p and on output are the quotient q and the remainder r of the division. The input and output of polynomials are defined respectively by the operators >. The name of the variable is always x. The terms in the polynomial are separated by '+'. A term is either a constant c, or of the form ctx, or in general c*x d with coefficient c and degree d. Except for the constant term, there is always a * in every term, x is written and entered as 1*x. You may assume that all input provided by the user is correct. The program to test the polynomial arithmetic operates either on randomly generated polynomials, or on polynomials provided by the user. A first session on random polynomials is below. Project Two> ./poly Testing polynomial arithmetic ... Test on random polynomials ? (y) y Give the degree of the first polynomial : 4 Give the degree of the second polynomial : 2 -> the first random polynomial : 2*x^4 + 31*x^3 + 74*x^2 + 39+x -> the second random polynomial : 79*x^2 + 61*x + 10 degree of the sum: 4 the sum : 2*x^4 + 31*x^3 + 56*x^2 + 3*x + 10 sum - second : 2*x^4 + 31*x^3 + 74*x^2 + 39*x first : 2*x^4 + 31*x^3 + 74*x^2 + 39*x the product : 61*x 6 + 49*x 5 + 94*x^4 + 48*x^3 + 15*x^2 + 2*x divmod quotient : 43*x^2 + 47*x + 12 quotient with : 43*x^2 + 47*x + 12 divmod remainder : 1*x + 74 remainder with % : 1*x + 74 check : 2*x^4 + 31*x^3 + 74*x^2 + 39*x In the second session, listed below, the user types in two polynomials. Project_Two> ./poly Testing polynomial arithmetic ... Test on random polynomials ? (y) n Give the first polynomial : 4*x^3 + 2*x + 9 -> your first polynomial : 4*x^3 + 2*x + 9 Give the second polynomial : 77*x + 8 -> your second polynomial : 77*x + 8 degree of the sum : 3 the sum: 4*x^3 + 79*x + 17 sum - second : 4*x^3 + 2*x + 9 first : 4*x^3 + 2*x + 9 the product : 17*x^4 + 32*x^3 + 57*x^2 + 30*x + 72 divmod quotient : 58*x 2 + 62*x + 15 quotient with : 58*x2 + 62*x + 15 divmod remainder : 83 remainder with %: 83 check: 4*x^3 + 2*x + 9 Project_Two> The source code for poly is posted as polynomialtest.cpp. To solve this project, you have to write the definition of the class and to implement the methods of the class polynomial. The purpose of this project is to apply the STL vector class to represent polynomials in one variable and to define the basic arithmetic operations with operator overloading. Instead of working with rational or floating-point coefficients, the coefficients of the polynomials are integer numbers modulo some prime number. These numbers are defined in the class intmodp, available via the course web site. You must use this class. The UML class diagrams are shown below. polynomial intmodp - degree + prime - vector - modpvalue + polynomial() + intmodp + polynomial(c,d) + intmodp(n) + deg + value() + cff(a) + operator+(n) + operator+(p) + operator-(n) + operator-(p) + operator*(p) + operator*(n) + divmod (p,q,r) + inverse() + operator/(p) + operator/(n) + operator%(p) + operator>(i,n) + operator>>(i,p) For the explanation of the intmodp class, see the posted file intmodp.h. The two private data attributes of a polynomial are its degree d and its coefficients, stored as a vector of d + 1 many integers modulo some prime. The number at position k in the vector is the coefficient with the monomial zk. There are two constructors. The constructor without argument makes the number zero. The degree of the number zero is -1. The other constructor has two arguments: a nonzero coefficient c and the degree d for the monomial erd. The method deg() returns the degree of the polynomial. The method cff(d) returns the coefficient of the monomial z", which may be zero, for example when d is larger than the degree of the polynomial. The basic arithmetical operations on polynomials are defined by the operators +, -, *, /, and %. An addition and subtraction may cause the cancellation of the leading coefficients. The operators must ensure that the degree of the result is correct, that is: for any polynomial p, p.cff (p.deg() is not equal to zero. The method divmod() has three arguments: on input is a divisor polynomial p and on output are the quotient q and the remainder r of the division. The input and output of polynomials are defined respectively by the operators >. The name of the variable is always x. The terms in the polynomial are separated by '+'. A term is either a constant c, or of the form ctx, or in general c*x d with coefficient c and degree d. Except for the constant term, there is always a * in every term, x is written and entered as 1*x. You may assume that all input provided by the user is correct. The program to test the polynomial arithmetic operates either on randomly generated polynomials, or on polynomials provided by the user. A first session on random polynomials is below. Project Two> ./poly Testing polynomial arithmetic ... Test on random polynomials ? (y) y Give the degree of the first polynomial : 4 Give the degree of the second polynomial : 2 -> the first random polynomial : 2*x^4 + 31*x^3 + 74*x^2 + 39+x -> the second random polynomial : 79*x^2 + 61*x + 10 degree of the sum: 4 the sum : 2*x^4 + 31*x^3 + 56*x^2 + 3*x + 10 sum - second : 2*x^4 + 31*x^3 + 74*x^2 + 39*x first : 2*x^4 + 31*x^3 + 74*x^2 + 39*x the product : 61*x 6 + 49*x 5 + 94*x^4 + 48*x^3 + 15*x^2 + 2*x divmod quotient : 43*x^2 + 47*x + 12 quotient with : 43*x^2 + 47*x + 12 divmod remainder : 1*x + 74 remainder with % : 1*x + 74 check : 2*x^4 + 31*x^3 + 74*x^2 + 39*x In the second session, listed below, the user types in two polynomials. Project_Two> ./poly Testing polynomial arithmetic ... Test on random polynomials ? (y) n Give the first polynomial : 4*x^3 + 2*x + 9 -> your first polynomial : 4*x^3 + 2*x + 9 Give the second polynomial : 77*x + 8 -> your second polynomial : 77*x + 8 degree of the sum : 3 the sum: 4*x^3 + 79*x + 17 sum - second : 4*x^3 + 2*x + 9 first : 4*x^3 + 2*x + 9 the product : 17*x^4 + 32*x^3 + 57*x^2 + 30*x + 72 divmod quotient : 58*x 2 + 62*x + 15 quotient with : 58*x2 + 62*x + 15 divmod remainder : 83 remainder with %: 83 check: 4*x^3 + 2*x + 9 Project_Two> The source code for poly is posted as polynomialtest.cpp. To solve this project, you have to write the definition of the class and to implement the methods of the class polynomial
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started