Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Note: The previous solution did not run well. Task : Write a complete Project fi

ID: 3888403 • Letter: N

Question

Note: The previous solution did not run well.

Task: Write a complete Project file ( main file, specification file and Implementation file). Write the program in C++. Use linked lists to represent two polynomials. Each polynomial will be an object of the Polynomial class. Each node in the linked list will represent a term of the polynomial, so data members of the nodes will include coefficient and exponent.

Processing: The program should accept appropriate values (i.e., coefficient and exponent values) from the user to populate two polynomials. Simple data checking should verify that the integer exponents are non-negative and that coefficients are not zero. Polynomials may have varying numbers of terms. The terms should be stored in the linked list in decreasing order of their exponents.

For example, valid polynomials might be:

3 x to power 7 - 1.5 x to power 4 + 3 (stored as 3 nodes)

7.1 x to power 2 - 6x (stored as 2 nodes)

12 (stored as 1 node)

The program will perform three basic operations on the polynomials: add the two original polynomials together; output the sum subtract one of the original polynomials from the other one; output the difference perform monomial multiplication on one of the original polynomials (i.e., multiply each polynomial term by a given monomial term, not necessarily a scalar); output the monomial and the result

Note: Do not change the original two polynomials during the addition, subtraction or multiplication operations. Include appropriate functions in the class (constructors, etc.) to produce an effective object-oriented program.

Input: Be sure the program is flexible enough that polynomial terms may be supplied by the user out of proper order, but still be stored in proper order.

Output: Test program should allow the user to input the two polynomials and the monomial multiplier. Then output the following:

a) each of the two original polynomials

b) the sum of the polynomials

c) the difference (first polynomial minus the second)

d) another difference (second polynomial minus the first)

e) the result for each of the two polynomials being multiplied by a (different) monomial

Make sure your program in program is well documented both internally and externally.

Explanation / Answer

#include <iostream>

#include<stdlib.h>

#include<stdio.h>

using namespace std;

class poly

{

private :

struct polynode

{

float coeff ;

int exp ;

polynode *link ;

} *p ;

public :

poly( ) ;

void poly_append ( float c, int e ) ;

void display_poly( ) ;

void poly_add( poly &l1, poly &l2 ) ;

void poly_subtract(poly &l1,poly &l2);

~poly( ) ;

} ;

poly :: poly( )

{

p = NULL ;

}

void poly :: poly_append ( float c, int e )

{

polynode *temp = p ;

if ( temp == NULL )

{

temp = new polynode ;

p = temp ;

}

else

{

while ( temp -> link != NULL )

temp = temp -> link ;

temp -> link = new polynode ;

temp = temp -> link ;

}

temp -> coeff = c ;

temp -> exp = e ;

temp -> link = NULL ;

}

//display polynomial

void poly :: display_poly( )

{

polynode *temp = p ;

int f = 0 ;

cout << endl ;

while ( temp != NULL )

{

if ( f != 0 )

{

if ( temp -> coeff > 0 )

cout << " + " ;

else

cout << " " ;

}

if ( temp -> exp != 0 )

cout << temp -> coeff << "x^" << temp -> exp ;

else

cout << temp -> coeff ;

temp = temp -> link ;

f = 1 ;

}

}

//polynomial addition

void poly :: poly_add ( poly &l1, poly &l2 )

{

polynode *z ;

if ( l1.p == NULL && l2.p == NULL )

return ;

polynode *temp1, *temp2 ;

temp1 = l1.p ;

temp2 = l2.p ;

while ( temp1 != NULL && temp2 != NULL )

{

if ( p == NULL )

{

p = new polynode ;

z = p ;

}

else

{

z -> link = new polynode ;

z = z -> link ;

}

if ( temp1 -> exp < temp2 -> exp )

{

z -> coeff = temp2 -> coeff ;

z -> exp = temp2 -> exp ;

temp2 = temp2 -> link ;

}

else

{

if ( temp1 -> exp > temp2 -> exp )

{

z -> coeff = temp1 -> coeff ;

z -> exp = temp1 -> exp ;

temp1 = temp1 -> link ;

}

else

{

if ( temp1 -> exp == temp2 -> exp )

{

z -> coeff = temp1 -> coeff + temp2 -> coeff ;

z -> exp = temp1 -> exp ;

temp1 = temp1 -> link ;

temp2 = temp2 -> link ;

}

}

}

}

while ( temp1 != NULL )

{

if ( p == NULL )

{

p = new polynode ;

z = p ;

}

else

{

z -> link = new polynode ;

z = z -> link ;

}

z -> coeff = temp1 -> coeff ;

z -> exp = temp1 -> exp ;

temp1 = temp1 -> link ;

}

while ( temp2 != NULL )

{

if ( p == NULL )

{

p = new polynode ;

z = p ;

}

else

{

z -> link = new polynode ;

z = z -> link ;

}

z -> coeff = temp2 -> coeff ;

z -> exp = temp2 -> exp ;

temp2 = temp2 -> link ;

}

z -> link = NULL ;

}

//polynomial subtraction

void poly :: poly_subtract( poly &l1, poly &l2 )

{

polynode *z ;

if ( l1.p == NULL && l2.p == NULL )

return ;

polynode *temp1, *temp2 ;

temp1 = l1.p ;

temp2 = l2.p ;

while ( temp1 != NULL && temp2 != NULL )

{

if ( p == NULL )

{

p = new polynode ;

z = p ;

}

else

{

z -> link = new polynode ;

z = z -> link ;

}

if ( temp1 -> exp < temp2 -> exp )

{

z -> coeff = temp2 -> coeff ;

z -> exp = temp2 -> exp ;

temp2 = temp2 -> link ;

}

else

{

if ( temp1 -> exp > temp2 -> exp )

{

z -> coeff = temp1 -> coeff ;

z -> exp = temp1 -> exp ;

temp1 = temp1 -> link ;

}

else

{

if ( temp1 -> exp == temp2 -> exp )

{

z -> coeff = temp1 -> coeff -temp2 -> coeff ;

z -> exp = temp1 -> exp ;

temp1 = temp1 -> link ;

temp2 = temp2 -> link ;

}

}

}

}

while ( temp1 != NULL )

{

if ( p == NULL )

{

p = new polynode ;

z = p ;

}

else

{

z -> link = new polynode ;

z = z -> link ;

}

z -> coeff = temp1 -> coeff ;

z -> exp = temp1 -> exp ;

temp1 = temp1 -> link ;

}

while ( temp2 != NULL )

{

if ( p == NULL )

{

p = new polynode ;

z = p ;

}

else

{

z -> link = new polynode ;

z = z -> link ;

}

z -> coeff = temp2 -> coeff ;

z -> exp = temp2 -> exp ;

temp2 = temp2 -> link ;

}

z -> link = NULL ;

}

poly :: ~poly( )

{

polynode *q ;

while ( p != NULL )

{

q = p -> link ;

delete p ;

p = q ;

}

}

int main( )

{

poly p1,p4;// creating object

//creating first polynomial

p1.poly_append ( 1.4, 5 ) ;

p1.poly_append ( 1.5, 4 ) ;

p1.poly_append ( 1.7, 2 ) ;

p1.poly_append ( 1.8, 1 ) ;

p1.poly_append ( 1.9, 0 ) ;

cout << " First polynomial:" ;

p1.display_poly( ) ;//displaying first polynomial

// creating second polynomial

poly p2 ;

p2.poly_append ( 1.5, 6 ) ;

p2.poly_append ( 2.5, 5 ) ;

p2.poly_append ( -3.5, 4 ) ;

p2.poly_append ( 4.5, 3 ) ;

p2.poly_append ( 6.5, 1 ) ;

cout << " Second polynomial:" ;

p2.display_poly( ) ;//displaying second polynomial

poly p3 ;//creating object p3 to stor the result of addition

p3.poly_add ( p1, p2 ) ;//addition

p4.poly_subtract(p1,p2);//subtracting p2 from p1 storing in object p4

poly p5;

p5.poly_subtract(p2,p1);//subtracting p1 from p2 storing in object p5

cout << " Resultant polynomial: " ;

p3.display_poly( );

p4.display_poly();

p5.display_poly();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote