In: Computer Science
provide a C code (only C please) that gives the output below:
************************************
* Menu HW #4
*
* POLYNOMIAL OPERATIONS *
* 1. Creating polynomials *
* 2. Adding polynomials *
* 3. Multiplying polynomials. *
* 4. Displaying polynomials *
* 5. Clearing polynomials. *
* 6. Quit. *
***********************************
Select the option (1 through 6): 7
You should not be in this class!
*************************************
* Menu HW #4
*
* POLYNOMIAL OPERATIONS *
* 1. Creating polynomials *
* 2. Adding polynomials *
* 3. Multiplying polynomials *
* 4. Displaying polynomials. *
* 5. Clearing polynomials *
* 6. Quit *
***********************************
Select the option (1 through 6): 4
Left Poly Pointer: 0
Right Poly Pointer: 0
Resulting Poly Pointer: 0
*************************************
* Menu HW #4
*
* POLYNOMIAL OPERATIONS *
* 1. Creating polynomials *
* 2. Adding polynomials *
* 3. Multiplying polynomials *
* 4. Displaying polynomials. *
* 5. Clearing polynomials *
* 6. Quit *
***********************************
Select the option (1 through 6): 1
/* Performing the required task(s) and your code must ALSO print
1. Description/explanation of the method or approach that you
use to create 2 polynomials; and
2. The listing of all functions involved in the
process.
*/
*************************************
* Menu HW #4
*
* POLYNOMIAL OPERATIONS *
* 1. Creating polynomials *
* 2. Adding polynomials *
* 3. Multiplying polynomials *
* 4. Displaying polynomials. *
* 5. Clearing polynomials *
* 6. Quit *
***********************************
Select the option (1 through 6): 4
Left Poly Pointer: SOME NONE ZERO ADDRESS and DISPLAYING Poly 1/1x2 + 3/4x + 5/12
Right Poly Pointer: SOME NONE ZERO ADDRESS and DISPLAYING Poly 1/1x4 – 3/7x2 + 4/9x + 2/11
Resulting Poly Pointer: 0
*************************************
* Menu HW #4
*
* POLYNOMIAL OPERATIONS *
* 1. Creating polynomials *
* 2. Adding polynomials *
* 3. Multiplying polynomials *
* 4. Displaying polynomials. *
* 5. Clearing polynomials *
* 6. Quit *
***********************************
Select the option (1 through 6): 2
/* Performing the required task(s) and your code must ALSO print
1. Description/explanation of the method or approach that you
use to add 2 polynomials; and
2. The listing of all functions involved in the
process.
*/
*************************************
* Menu HW #4
*
* POLYNOMIAL OPERATIONS *
* 1. Creating polynomials *
* 2. Adding polynomials *
* 3. Multiplying polynomials *
* 4. Displaying polynomials. *
* 5. Clearing polynomials *
* 6. Quit *
***********************************
***********************************
Select the option (1 through 6): 4
Left Poly Pointer: SOME NONE ZERO ADDRESS and DISPLAYING Poly 1/1x2 + 3/4x + 5/12
Right Poly Pointer: SOME NONE ZERO ADDRESS and DISPLAYING Poly 1/1x4 – 3/7x2 + 4/9x + 2/11
Resulting Poly Pointer: SOME NONE ZERO ADDRESS and DISPLAYING Poly 1/1x4 – 3/7x2 + 43/36x + 79/132
*************************************
* Menu HW #4
*
* POLYNOMIAL OPERATIONS *
* 1. Creating polynomials *
* 2. Adding polynomials *
* 3. Multiplying polynomials *
* 4. Displaying polynomials. *
* 5. Clearing polynomials *
* 6. Quit *
***********************************
Select the option (1 through 6): 3
/* Performing the required task(s) and your code must ALSO print
1. Description/explanation of the method or approach that you
use to multiply 2 polynomials; and
2. The listing of all functions involved in the
process.
*/
*************************************
* Menu HW #4
*
* POLYNOMIAL OPERATIONS *
* 1. Creating polynomials *
* 2. Adding polynomials *
* 3. Multiplying polynomials *
* 4. Displaying polynomials. *
* 5. Clearing polynomials *
* 6. Quit *
***********************************
Select the option (1 through 6): 4
Left Poly Pointer: SOME NONE ZERO ADDRESS and DISPLAYING Poly 1/1x2 + 3/4x + 5/12
Right Poly Pointer: SOME NONE ZERO ADDRESS and DISPLAYING Poly 1/1x4 – 3/7x2 + 4/9x + 2/11
Resulting Poly Pointer: SOME NONE ZERO ADDRESS and DISPLAYING Poly 1/1x6 + 3/4x5 – 1/84x4 + 31/252x3 + 871/924x2 + 191/594x + 5/66
*************************************
* Menu HW #4
*
* POLYNOMIAL OPERATIONS *
* 1. Creating polynomials *
* 2. Adding polynomials *
* 3. Multiplying polynomials *
* 4. Displaying polynomials. *
* 5. Clearing polynomials *
* 6. Quit *
***********************************
Select the option (1 through 6): 5
/* Releasing selected polynomial(s)
For example, clearing and releasing left polynomial
*/
*************************************
* Menu HW #4
*
* POLYNOMIAL OPERATIONS *
* 1. Creating polynomials *
* 2. Adding polynomials *
* 3. Multiplying polynomials *
* 4. Displaying polynomials. *
* 5. Clearing polynomials *
* 6. Quit *
***********************************
Select the option (1 through 6): 4
Left Poly Pointer: 0
Right Poly Pointer: SOME NONE ZERO ADDRESS and DISPLAYING
Poly
1/1x4 – 3/7x2 + 4/9x + 2/11
Resulting Poly Pointer: 0
*************************************
* Menu HW #4
*
* POLYNOMIAL OPERATIONS *
* 1. Creating polynomials *
* 2. Adding polynomials *
* 3. Multiplying polynomials *
* 4. Displaying polynomials. *
* 5. Clearing polynomials *
* 6. Quit *
***********************************
Select the option (1 through 6): 5
/* Releasing selected polynomial(s)
For example, clearing and releasing right polynomial
*/
*************************************
* Menu HW #4
*
* POLYNOMIAL OPERATIONS *
* 1. Creating polynomials *
* 2. Adding polynomials *
* 3. Multiplying polynomials *
* 4. Displaying polynomials. *
* 5. Clearing polynomials *
* 6. Quit *
***********************************
Select the option (1 through 6): 4
Left Poly Pointer: 0
Right Poly Pointer: 0
Resulting Poly Pointer: 0
*************************************
* Menu HW #4
*
* POLYNOMIAL OPERATIONS *
* 1. Creating polynomials *
* 2. Adding polynomials *
* 3. Multiplying polynomials *
* 4. Displaying polynomials. *
* 5. Clearing polynomials *
* 6. Quit *
***********************************
Select the option (1 through 6): 6
Polynomials -- Having Fun!
#include <alloc.h>
struct
polynode
{
float
coeff ;
int exp ;
struct polynode *link ;
} ;
void
poly_add ( struct polynode **, float, int ) ;
void display_poly ( struct polynode * ) ;
void multiply ( struct polynode *, struct polynode *, struct
polynode ** ) ;
void padd ( float, int, struct polynode ** ) ;
void
main( )
{
struct polynode *first, *second, *mult ;
int i = 1 ;
char ch;
scanf("%c",ch)
switch(ch) {
case 'A' :
poly_add ( &first, 3, 5 ) ;
poly_add ( &first, 2, 4 ) ;
poly_add ( &first, 1, 2 ) ;
printf("creating and adding polynomials" );
break;
case 'B' : //adding
poly_add ( &second, 1, 6 ) ;
poly_add ( &second, 2, 5 ) ;
poly_add ( &second, 3, 4 ) ;
break;
case 'C' : //multiplying
multiply ( first, second, &mult ) ;
break;
case 'D' : //display
display_poly ( first ) ;
display_poly ( second ) ;
break; default : printf("wrong" ); }
first = second = mult = NULL ; /* empty linked lists */
poly_add
( &first, 3, 5 ) ;
poly_add ( &first, 2, 4 ) ;
poly_add ( &first, 1, 2 ) ;
clrscr(
) ;
display_poly ( first ) ;
poly_add
( &second, 1, 6 ) ;
poly_add ( &second, 2, 5 ) ;
poly_add ( &second, 3, 4 ) ;
printf (
"\n\n" ) ;
display_poly ( second ) ;
printf (
"\n" );
while ( i++ < 79 )
printf ( "-" ) ;
multiply ( first, second, &mult ) ;
printf (
"\n\n" ) ;
display_poly ( mult ) ;
}
/*
adding a term to a polynomial */
void poly_add ( struct polynode **q, float x, int y )
{
struct polynode *temp ;
temp = *q ;
/*
create a new node if the list is empty */
if ( *q == NULL )
{
*q = malloc ( sizeof ( struct polynode ) ) ;
temp = *q ;
}
else
{
/* traverse the entire linked list */
while ( temp -> link != NULL )
temp = temp -> link ;
/* create new nodes at intermediate stages */
temp
-> link = malloc ( sizeof ( struct polynode ) ) ;
temp = temp -> link ;
}
/* assign coefficient and exponent */
temp
-> coeff = x ;
temp -> exp = y ;
temp -> link = NULL ;
}
/*
displays the contents of linked list representing a polynomial
*/
void display_poly ( struct polynode *q )
{
/* traverse till the end of the linked list */
while ( q != NULL )
{
printf ( "%.1f x^%d : ", q -> coeff, q -> exp ) ;
q = q -> link ;
}
printf ( "\b\b\b " ) ; /* erases the last colon(:) */
}
/*
multiplies the two polynomials */
void multiply ( struct polynode *x, struct polynode *y,
struct polynode **m )
{
struct polynode *y1 ;
float coeff1, exp1 ;
y1 = y ;
/* point to the starting of the second linked list */
if ( x == NULL && y == NULL )
return ;
/* if
one of the list is empty */
if ( x == NULL )
*m = y ;
else
{
if ( y == NULL )
*m = x ;
else/* if both linked lists exist */
{
/* for each term of the first list */
while ( x != NULL )
{
/* multiply each term of the second linked list with a
term of the first linked list */
while ( y != NULL )
{
coeff1 = x -> coeff * y -> coeff ;
exp1 = x -> exp + y -> exp ;
y = y -> link ;
/* add the new term to the resultant polynomial */
padd (
coeff1, exp1, m ) ;
}
y = y1 ;
/* reposition the pointer to the starting of
the second linked list */
x = x -> link ; /* go to the next node */
}
}
}
}
/*
adding a term to the polynomial in the descending order of the
exponent */
void padd ( float c, int e, struct polynode **s )
{
struct polynode *r, *temp = *s ;
/* if
list is empty or if the node is to be inserted before the first
node */
if ( *s == NULL || e > ( *s ) -> exp )
{
*s = r = malloc ( sizeof ( struct polynode ) ) ;
( *s ) -> coeff = c ;
( *s ) -> exp = e ;
( *s ) -> link = temp ;
}
else
{
while ( temp != NULL )
{
if ( temp -> exp == e )
{
temp -> coeff += c ;
return ;
}
if (
temp -> exp > e && ( temp -> link -> exp < e
|| temp -> link == NULL ) )
{
r = malloc ( sizeof ( struct polynode ) ) ;
r -> coeff = c;
r -> exp = e ;
r -> link = temp -> link ;
temp -> link = r ;
return ;
}
temp = temp -> link ; /* go to next node */
}
r ->
link = NULL ;
temp -> link = r ;
}
}