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

**************java*************** A polynomial of a real valued variable x is an

ID: 666424 • Letter: #

Question

**************java***************
A polynomial of a real valued variable x is an expression of the form anxn + an-1xn-1 + … + a1x1 + a0 where a0, …, an are real constants, called coefficients. For instance, 2.5x3+1.2x-5 is a polynomial of x with a3=2.5, a2=0, a1=1.2 and a0=-5. In this project, you are required to develop a class named Polynomial that supports symbolic manipulation of polynomials of a fixed variable x, that is, all polynomials are polynomials of the same variable x. Your class must at least support operations for
1. Constructing a polynomial given an array of coefficients. For example, given the float point number array {2.5,0.0,1.2,-5}, the operation constructs a Polynomial object representing 2.5x3+1.2x-5. This is a constructor.
2. Adding one polynomial to another. For example, if this object represents 2.5x3+1.2x-5 then adding another object representing 7x4+2x3+x2+3 to this object changes this object so that it represents 7x4+5.5x3+x2+1.2x–2. Note that the ranks (the largest exponent) of the two polynomials are not necessary the same.
3. Multiplying one polynomial by another. For example, if this object represents 2x2+x-5 then multiplying it by another object representing 3x3+x2+3 changes this object so that it represents 6x5+5x4-14x3+x2+3x-15. Again the ranks of the two polynomials are not necessary the same.
4. Evaluating a polynomial for a given value for x. For instance, if this object represents 2x2+x-5 then evaluating it for x=2 yields 5.
5. Converting a Polynomial object to a string so that the Polynomial object can be displayed. If this object represents 3x3+x2-1 then the method should return
“3*x^3 + x^2 - 1” although “3*x^3 + 1*x^2 + 0*x^1 + (-1)” is acceptable
1. Specify, design and implement Polynomial. Your implementation must use linked lists to represent polynomials.
2. Write an interactive test program that tests all the public methods of the Polynomial class.
Please submit the following. Analysis: test data; Design:
1. Class diagrams showing representation of data;
2. A class invariant for each class;
3. Pre/Post conditions for required operations;
4. Algorithms for required operations. Algorithms can be described in English, Code; flow diagrams, or sequence diagrams;
Screen snapshots of test runs.

Explanation / Answer

For Creating Polynomial and Evaluating a polynomial

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10

void main()
{
    int array[MAXSIZE];
    int i, num, power;
   Float polySum;
  

     /* Read the coefficients into an array */
    printf("Enter %d coefficients ", num + 1);
    for (i = 0; i <= num; i++)
    {
        scanf("%d", &array[i]);
    }
    polySum = array[0];
    for (i = 1; i <= num; i++)
    {
        polySum = polySum * x + array[i];
    }
    power = num;

    printf("Given polynomial is: ");
    for (i = 0; i <= num; i++)
    {
        if (power < 0)
        {
            break;
        }
        /* printing proper polynomial function */
        if (array[i] > 0)
            printf(" + ");
        else if (array[i] < 0)
            printf(" - ");
        else
            printf(" ");
        printf("%dx^%d ", abs(array[i]), power--);
    }

}
  

For Addition

#include<stdio.h>
#include<stdlib.h>

/**
* The structure for the polynomial
* This is a linked list with two variable
* int coeff The Coefficient
* int pow The power of x
*/
typedef struct link {
    int coeff;
    int pow;
    struct link * next;
} my_poly;

/** The prototypes */
void my_create_poly(my_poly **);
void my_show_poly(my_poly *);
void my_add_poly(my_poly **, my_poly *, my_poly *);

/**
* The simple menu driven main function
*/
int main(void) {
    int ch;
    do {
        my_poly * poly1, * poly2, * poly3;

        printf(" Create 1st expression ");
        my_create_poly(&poly1);
        printf(" Stored the 1st expression");
        my_show_poly(poly1);

        printf(" Create 2nd expression ");
        my_create_poly(&poly2);
        printf(" Stored the 2nd expression");
        my_show_poly(poly2);

        my_add_poly(&poly3, poly1, poly2);
        my_show_poly(poly3);

        printf(" Add two more expressions? (Y = 1/N = 0): ");
        scanf("%d", &ch);
    } while (ch);
    return 0;
}

/**
* The create polynomial function
* @param my_poly ** node The pointer to the head of the polynomial
* We will modify the parameter and will store the base address
* @return void
*/
void my_create_poly(my_poly ** node) {
    int flag; //A flag to control the menu
    int coeff, pow;
    my_poly * tmp_node; //To hold the temporary last address
    tmp_node = (my_poly *) malloc(sizeof(my_poly)); //create the first node
    *node = tmp_node; //Store the head address to the reference variable
    do {
        //Get the user data
        printf(" Enter Coeff:");
        scanf("%d", &coeff);
        tmp_node->coeff = coeff;
        printf(" Enter Pow:");
        scanf("%d", &pow);
        tmp_node->pow = pow;
        //Done storing user data

        //Now increase the Linked on user condition
        tmp_node->next = NULL;

        //Ask user for continuation
        printf(" Continue adding more terms to the polynomial list?(Y = 1/N = 0): ");
        scanf("%d", &flag);
        //printf(" FLAG: %c ", flag);
        //Grow the linked list on condition
        if(flag) {
            tmp_node->next = (my_poly *) malloc(sizeof(my_poly)); //Grow the list
            tmp_node = tmp_node->next;
            tmp_node->next = NULL;
        }
    } while (flag);
}

/**
* The show polynomial function
* Prints the Polynomial in user readable format
* @param my_poly * node The polynomial linked list
* @return void
*/
void my_show_poly(my_poly * node) {
    printf(" The polynomial expression is: ");
    while(node != NULL) {
        printf("%dx^%d", node->coeff, node->pow);
        node = node->next;
        if(node != NULL)
            printf(" + ");
    }
}

/**
* The polynomial add function
* Adds two polynomial to a given variable
* @param my_poly ** result Stores the result
* @param my_poly * poly1 The first polynomial expression
* @param my_poly * poly2 The second polynomial expression
* @return void
*/
void my_add_poly(my_poly ** result, my_poly * poly1, my_poly * poly2) {
    my_poly * tmp_node; //Temporary storage for the linked list
    tmp_node = (my_poly *) malloc(sizeof(my_poly));
    tmp_node->next = NULL;
    *result = tmp_node; //Copy the head address to the result linked list

    //Loop while both of the linked lists have value
    while(poly1 && poly2) {
        if (poly1->pow > poly2->pow) {
            tmp_node->pow = poly1->pow;
            tmp_node->coeff = poly1->coeff;
            poly1 = poly1->next;
        }
        else if (poly1->pow < poly2->pow) {
            tmp_node->pow = poly2->pow;
            tmp_node->coeff = poly2->coeff;
            poly2 = poly2->next;
        }
        else {
            tmp_node->pow = poly1->pow;
            tmp_node->coeff = poly1->coeff + poly2->coeff;
            poly1 = poly1->next;
            poly2 = poly2->next;
        }

        //Grow the linked list on condition
        if(poly1 && poly2) {
            tmp_node->next = (my_poly *) malloc(sizeof(my_poly));
            tmp_node = tmp_node->next;
            tmp_node->next = NULL;
        }
    }

    //Loop while either of the linked lists has value
    while(poly1 || poly2) {
        //We have to create the list at beginning
        //As the last while loop will not create any unnecessary node
        tmp_node->next = (my_poly *) malloc(sizeof(my_poly));
        tmp_node = tmp_node->next;
        tmp_node->next = NULL;

        if(poly1) {
            tmp_node->pow = poly1->pow;
            tmp_node->coeff = poly1->coeff;
            poly1 = poly1->next;
        }
        if(poly2) {
            tmp_node->pow = poly2->pow;
            tmp_node->coeff = poly2->coeff;
            poly2 = poly2->next;
        }
    }

    printf(" Addition Complete");
}

FOR MULTIPLICATION

=================================================================

===================================================================