Question

Add and subtract polynomials using linked lists:

The output should look like the following:

First polynomial: 1.3x^2 + 2.5x^4 + 5.3x^2 + 2.8x^1 + 3.8 Second polynomial: 2.5x^6 + 3.5x^4 -4.5x 3 + 4.9x^2 + 7.5x41 Result

The code for the header file, Polynomial.h, is given as such:

***********HEADER*************

#include <iostream>
#include <cstdlib>

using namespace std;


class polyll { //this is class POLYLL, but all lower case
private:
struct polynode {
float coeff;
int exp;
polynode* link;
} * p;

public:
polyll();
void poly_append(float c, int e);
void display_poly();
void poly_add(polyll& l1, polyll& l2);
void poly_subtract(polyll& l1, polyll& l2);
~polyll();
};
polyll::polyll()
{
p = NULL;
}
void polyll::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;
}
void polyll::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;
}
}

void polyll::poly_add(polyll& l1, polyll& l2) { //this function is POLYLL& L1, POLYLL& L2, but all lower case

}

void polyll::poly_subtract(polyll& l1, polyll& l2) { // Subtract the polynomials

}

polyll::~polyll()
{
polynode* q;
while (p != NULL) {
q = p->link;
delete p;
p = q;
}
}

Code in the main.cpp file is given as such:

**********MAIN**********

#include "Polynomial.h"
#include <iostream>
using namespace std;

int main()
{
polyll p1;
p1.poly_append(1.3, 7);
p1.poly_append(2.5, 4);
p1.poly_append(5.3, 2);
p1.poly_append(2.8, 1);
p1.poly_append(3.8, 0);
cout << "\n\n First polynomial:\n" ;
p1.display_poly();
polyll p2;
p2.poly_append(2.5, 6);
p2.poly_append(3.5, 4);
p2.poly_append(-4.5,3);
p2.poly_append(4.9, 2);
p2.poly_append(7.5, 1);
cout << "\n\n\n Second polynomial:\n";
p2.display_poly();
polyll p3;
p3.poly_add(p1, p2);
cout << "\n\n\n Resultant polynomial addition: \n";
p3.display_poly();

polyll p4;
p4.poly_subtract(p1, p2);
cout << "\n\n\n Resultant polynomial subtraction: \n";
p4.display_poly();

getchar();
}

Just need help with the add and subtract parts in the header. Thank you very much!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

void polyll::poly_add(polyll& l1, polyll& l2) // Add Polynomials
{
polyll *l1ptr,*l2ptr;
int coeff,exp;
ptrn=ptrp=temp=NULL;
l1ptr=l1.temp;
l2ptr=l2.temp;
while(l1ptr!=NULL && l2ptr!=NULL)
{
if(l1ptr->exp==l2ptr->exp) // If coefffficients are equal
{
   coeff=l1ptr->coeff+l2ptr->coeff;
   exp=l1ptr->exp;
   l1ptr=l1ptr->link;
   l2ptr=l2ptr->link;
}
else if(l1ptr->exp>l2ptr->exp)
{
   coeff=l1ptr->coeff;
   exp=l1ptr->exp;
   l1ptr=l1ptr->link;
}
else if(l1ptr->exp<l2ptr->exp)
{
   coeff=l2ptr->coeff;
   exp=l2ptr->exp;
   l2ptr=l2ptr->link;
}
ptrn=new polyll;
if(temp==NULL)
   temp=ptrn;
ptrn->coeff=coeff;
ptrn->exp=exp;
ptrn->link=NULL;
ptrp->link=ptrn;
ptrp=ptrn;
} // End of While
if(l1ptr==NULL)
{
while(l2ptr!=NULL)
{
   coeff=l2ptr->coeff;
   exp=l2ptr->exp;
   l2ptr=l2ptr->link;
   ptrn=new polyll;
   if(temp==NULL)
   temp=ptrn;
   ptrn->coeff=coeff;
   ptrn->exp=exp;
   ptrn->link=NULL;
   ptrp->link=ptrn;
   ptrp=ptrn;
}
}
else if(l2ptr==NULL)
{
while(l1ptr!=NULL)
{
   coeff=l1ptr->coeff;
   exp=l1ptr->exp;
   l1ptr=l1ptr->link;
   ptrn=new polyll;
   if(temp==NULL)
   temp=ptrn;
   ptrn->coeff=coeff;
   ptrn->exp=exp;
   ptrn->link=NULL;
   ptrp->link=ptrn;
   ptrp=ptrn;
}
}
} // End of addition

// Subtract two polynomials
void polyll::poly_subtract(polyll& l1, polyll& l2) // Subtract
{
polyll *l1ptr,*l2ptr;
int coeff,exp;
ptrn=ptrp=temp=NULL;
l1ptr=l1.temp;
l2ptr=l2.temp;
while(l1ptr!=NULL && l2ptr!=NULL)
{
if(l1ptr->exp==l2ptr->exp) // If coefffficients are equal
{
   coeff=l1ptr->coeff-l2ptr->coeff;
   exp=l1ptr->exp;
   l1ptr=l1ptr->link;
   l2ptr=l2ptr->link;
}
else if(l1ptr->exp>l2ptr->exp)
{
   coeff=l1ptr->coeff;
   exp=l1ptr->exp;
   l1ptr=l1ptr->link;
}
else if(l1ptr->exp<l2ptr->exp)
{
   coeff=0-l2ptr->coeff;
   exp=l2ptr->exp;
   l2ptr=l2ptr->link;
}
ptrn=new polyll;
if(temp==NULL)
   temp=ptrn;
ptrn->coeff=coeff;
ptrn->exp=exp;
ptrn->link=NULL;
ptrp->link=ptrn;
ptrp=ptrn;
} // End of While
if(l1ptr==NULL)
{
while(l2ptr!=NULL)
{
   coeff=0-l2ptr->coeff;
   exp=l2ptr->exp;
   l2ptr=l2ptr->link;
   ptrn=new polyll;
   if(temp==NULL)
   temp=ptrn;
   ptrn->coeff=coeff;
   ptrn->exp=exp;
   ptrn->link=NULL;
   ptrp->link=ptrn;
   ptrp=ptrn;
}
}
else if(l2ptr==NULL)
{
while(l1ptr!=NULL)
{
   coeff=l1ptr->coeff;
   exp=l1ptr->exp;
   l1ptr=l1ptr->link;
   ptrn=new polyll;
   if(temp==NULL)
   temp=ptrn;
   ptrn->coeff=coeff;
   ptrn->exp=exp;
   ptrn->link=NULL;
   ptrp->link=ptrn;
   ptrp=ptrn;
}
}
} // End of subtraction

/* I have made the function according to my taken variable please replace it using find and replace feature in compile at last I replace some of major variable like parameter and class variable.

If you find difficulties please ask

Thank You!

*/

for addition

#include<bits/stdc++.h>
using namespace std;
  
struct Node
{
int coeff;
int pow;
struct Node *next;
};
  
void create_node(int x, int y, struct Node **temp)
{
struct Node *r, *z;
z = *temp;
if(z == NULL)
{
r =(struct Node*)malloc(sizeof(struct Node));
r->coeff = x;
r->pow = y;
*temp = r;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
else
{
r->coeff = x;
r->pow = y;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
}
void polyadd(struct Node *poly1, struct Node *poly2, struct Node *poly)
{
while(poly1->next && poly2->next)
{

if(poly1->pow > poly2->pow)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
  
else if(poly1->pow < poly2->pow)
{
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
  
else
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff+poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}

poly->next = (struct Node *)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
if(poly2->next)
{
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
poly->next = (struct Node *)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
}
}

void show(struct Node *node)
{
while(node->next != NULL)
{
printf("%dx^%d", node->coeff, node->pow);
node = node->next;
if(node->next != NULL)
printf(" + ");
}
}

int main()
{
struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL;

create_node(5,2,&poly1);
create_node(4,1,&poly1);
create_node(2,0,&poly1);
  
create_node(5,1,&poly2);
create_node(5,0,&poly2);
  
printf("1st Number: ");
show(poly1);
  
printf("\n2nd Number: ");
show(poly2);
  
poly = (struct Node *)malloc(sizeof(struct Node));
  
polyadd(poly1, poly2, poly);

printf("\nAdded polynomial: ");
show(poly);
  
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Add and subtract polynomials using linked lists: The output should look like the following: The code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please provide full answer with comments this is just begining course of c++ so don't use...

    please provide full answer with comments this is just begining course of c++ so don't use advanced tequenicks I'll put main.cpp, polynomial.h, polynomial.cpp and Cimg.h at the bottom of pictures. If you help me with this will be greatly thankful thank you main.cpp #include "polynomial.h" #include "polynomial.h" #include "polynomial.h" #include "polynomial.h" #include <iostream> using std::cout; using std::endl; int main() { pic10a::polynomial p1; p1.setCoeff(0, 1.2); p1.setCoeff(3, 2.2); p1.setCoeff(7, -9.0); p1.setCoeff(7, 0.0); //degree of polynomial is now 3 cout << p1 <<...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT