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

This is the second time I am asking the same question. I need exact answer and e

ID: 3630087 • Letter: T

Question

This is the second time I am asking the same question. I need exact answer and explanation please...            (Class and Link list required)
Question Details
Problem
Create a class named polynomial that uses a linked-list bag to perform the following arithmetic for
polynomials, p1 and p2.
1. p1 + p2 (adding polynomials)
2. p1 * p2 (multiplying polynomials)
3. p1 (n), n ? Z

Background:A polynomial is a function of the following form:P(n) , where d is an integer and the are the coefficients with ( ? 0) .P(n) is said to be a polynomial in n of degree d
-----------------------
Input
1. A text file consisting of two rows of space-delimited integers. Each row represents the
coefficients, for a polynomial p. The first number on each row represents and the last number .Notice that any (except ) may be 0. No coefficient will be missing, even if it is
0. The name of the text file will be available in positional parameter, argv[1].

2. A number, n ? Z input on the command line in positional parameter argv[2] that is used to
evaluate polynomial p1. For example, if p1 = (1, 0, 3, 0, 0) and n=2, then p1(2) = 28. Notice
that argv[2] is type char*, so it must be converted to type int by your program (a global function).
Class requirements
1. Use the linked-list bag to implement your polynomial.
2. Define a constant member to evaluate a polynomial at a value n.
3. Define a “setter” to set any coefficient requested.
4. Define a “getter” to return any coefficient requested.
5. Overload operators + and * for the polynomial class.
6. Overload operator << for polynomial class.
a. Operator << should display the polynomial as an n-tuple in the order highest term to
lowest term. For example,
(3, -2, 0, 0, 7) = 3x^4– 2x^3+ 7
Driver
1. Greet the user.
2. Instantiate p1 and p2.
3. Read the text file and set the coefficients of p1 and p2.
4. Perform the polynomial arithmetic per the problem specification and display the annotated
results.
5. Convert argv[2] to an int, evaluate p1 at this value, and display the annotated result.
Notes
1. Use an appropriate annotation with all output.
2. Make sure that main is short. It should basically be making top-level function calls.
3. Use good program style that includes, but is not limited to, header comments, pre and post
conditions, macro guards, whitespace and indentation, self-documenting names, symbolic
constants where appropriate (use all caps for the name of each symbolics), separation of
interface and implementation, correctly submitted program file (tar archive), and a greeting to
start the program.
4. The text file will be provided by the grader. You should create your own text file for testing
purposes, but do not submit it.
5. You are not restricted to the polynomial members listed above; that is, you may define other
members that you feel necessary. For example, you may find that operator [ ] is useful for the
polynomial class

Explanation / Answer

#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;

//Declaring the class for the linked list with it's two required
//integer variables that need to be linked for the polynomials
class polynomial{
     public:
         int coeff;
         int expo;
         class polynomial *next;
//Declaring the functions within the class
         polynomial input();
         polynomial output();
         polynomial addition(polynomial poly);
         polynomial multiplication(polynomial poly);
         polynomial subtraction(polynomial poly);
};
class polynomial x, y, z;

int main()
{
     polynomial x, y, z;
     char ch, math;
     do
     {
//Input of the first and second polynomials
             printf(" First polynomial:");
         x.input();//input(poly);
         printf(" Second polynomial:");
             y.input();//input(poly);
//output of the first and second polynomials
         printf(" First polynomial:");
             x.output();//output(poly);
         printf(" Second polynomial:");
         y.output();//output(poly);

//Allows the user to input the symbol for the mathematical function that the user would like to use
         printf(" Do you want to add, subtract, or multiply the polynomials? (+,-,*)");
         math = getch();
         if(math == '+')
             z = x.addition(y);
         else if(math == '-')
             z = x.subtraction(y);
         else if(math == '*')
             z= x.multiplication(y);
         else{   //if the user enters a different symbol, the program goes into a while loop until an applicable symbol is entered
             while(math != '+' && math != '-' && math != '*') {
                 printf(" You have entered an incorrect symbol, please try again. (+,-.*)");
                 math = getch();
             }
             if(math == '+')
                 z = x.addition(y);
             else if(math == '-')
                 z = x.subtraction(y);
             else if(math == '*')
                 z= x.multiplication(y);
         }
//Outputs the combination of the polynomials
         printf(" The new polynomial is: ");
         z.output();
//allows the user to repeat the program or end it
         printf(" Add two more polynomials, or exit the program: (y,n)");
         ch=getch();
     }
     while(ch=='y'||ch=='Y');

     printf(" ");

     return ;
}

//Allows the user to enter the coefficient and the exponent for any number of integers in the polynomial
polynomial polynomial::input()
{
     polynomial*node;
     char ch;
     do
     {
         printf(" Enter the coefficient:");
         scanf("%d",&node->coeff); //input for the coefficient
         printf(" Enter the exponent:");
         scanf("%d",&node->expo); //input for the exponent
         node->next=(class polynomial *)malloc(sizeof(class polynomial));
         node=node->next;
         node->next=NULL; //sets the next to NULL
         printf(" Continue?(Y/N):"); //allows the user to enter more integers in the polynomial
         ch=getch();
     }
     while(ch=='y' || ch=='Y');
     return *node;
}

//Adds the two polynomials together
polynomial polynomial::addition(class polynomial poly)
{
     class polynomial * p;
     class polynomial * q;
     class polynomial * r;
// q=*poly;
     //Organizes the exponents of the varibles in the polynomials
     while(p->next && q->next)
     {
         if(p->expo>q->expo)
         {
             r->expo=p->expo;
             r->coeff=p->coeff;
             p=p->next;
         }
         else if(p->expo<q->expo)
         {
             r->expo=q->expo;
             r->coeff=q->coeff;
             q=q->next;
         }
         else
         {
             r->expo=p->expo;
             r->coeff=p->coeff+q->coeff;
             p=p->next;
             q=q->next;
         }
         r->next=(class polynomial *)malloc(sizeof(class polynomial));
         r=r->next;
         r->next=NULL;
     }
     //Actually adds the coefficients together if the exponents are equal
     while(p->next || q->next)
     {
         if(p->next)
         {
             r->expo=p->expo;
             r->coeff=p->coeff;
             p=p->next;
         }
         if(q->next)
         {
             r->expo=q->expo;
             r->coeff=q->coeff;
             q=q->next;
         }
         r->next=(class polynomial *)malloc(sizeof(class polynomial));
         r=r->next;
         r->next=NULL;
     }
     return *r;
}

//Organizes the polynomials by exponents, then subtracts the two polynomials
polynomial polynomial::subtraction(class polynomial poly)
{
     return poly;
}

//Organizes the polynomials by exponents, then multiplies the two polynomials
polynomial polynomial::multiplication (class polynomial poly)
{
     return poly;
}


//Outputs the sum of the polynomials
polynomial polynomial::output()
{
     polynomial*node;
     //while loop to output the linked list variables
     while(node->next!=NULL)
     {
         printf("%dx^%d",node->coeff,node->expo);

         node=node->next;
   
         if(node->next!=NULL)
             printf("+");
     }
     return *node;
}

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