Polynomial may be represented as a linked list as follows: for every term in the polynomial there is one entry in the linked list consisting of the term's coefficient and degree. The entries are ordered according to ASCENDING values of degree; zero-coefficient terms are not stored. For example, the following polynomial (the symbol '^' is used to mean 'raised to the power'): 4x^5 - 2x^3 + 2x +3 can be represented as the linked list of terms: (3,0) -> (2,1) -> (-2,3) -> (4,5) where each term is a (coefficient, degree) pair. Write a C++ class called Polynomial with the following functionality:
• Read the polynomials from a file.
• Addition of two polynomials.
• Multiplication of two polynomials.
• Evaluation of a polynomial at a given point.
Sample Output:
Enter the name of the polynomial file => ptest1
4.0x^5 + -2.0x^3 + 2.0x + 3.0
Please note, that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.
We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.
'm looking for some assistance on an exercise for my C++ programming class. Unfortunately, I was rather ill the previous week an was unable to attend class, meaning I have only been able to use the textbook as a resource. I have done my best to complete the exercises, but, both because of my absence in class and my beginner status, I feel the code is rather flawed. I would really appreciate any corrections and suggestions, even moreso if you can elaborate on them.
Can you please provide evidence of an attempt. If you are asking for a way to understand the problem (such as before your edit) I would recommend going through the question, reading carefully, and highlighting the parts that are most useful and try to create a flowchart of how the program works. Since a sample output is given you can look at the sample output and try to devise what the program needs to do from that.
this is my approach to solve this problem and i have not yet complete it if there any issue plzzz help me i would give update of this time to time .... and if there any better approach do tell me kindly ....
That looks like it might get a bit confusing, at least to me. I would do something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
struct PolynomialPart{ //this is a linked list
int expo;
int coeff;
PolynomialPart *next;
}
class Polynomial{
PolunomialPart *data;
public:
void load();
Polynomial add(Polynomial other); //maybe overload operator+ instead?
Polynomial multiply(Polynomial other); //maybe overload operator* instead?
void evaluate(); //i dont know what this needs to do.
}
But there are many other ways you could do it, and this might not satisfy the criteria.
polynomial::polynomial(int e, int c, int n)
{
expo = e;
coeff = c;
num_of_terms = n;
}
void polynomial:: polynomial1()
{
temp = NULL;
newnode = NULL;
//polynomial *head, *temp, *newnode = NULL;
cout << "Enter number of terms : ";
cin >> num_of_terms;
for (int i = 0; i < num_of_terms; i++)
{
cout << "Enter your coefficient : ";
cin >> coeff;
cout << "Enter your exponent : ";
cin >> expo;
//newnode = new polynomial;
if (newnode == NULL)
{
newnode = new polynomial;
newnode->coeff = coeff;
newnode->expo = expo;
newnode->next = NULL;
head = newnode;
}
else
{
temp = head;
newnode = new polynomial;
while (temp->next != NULL)
{
temp = temp->next;
}
newnode->coeff = coeff;
newnode->expo = expo;
temp->next = newnode;
newnode->next = NULL;
}
}
}
void polynomial::polynomial2()
{
temp = NULL;
newnode = NULL;
cout << "Enter number of terms : ";
cin >> num_of_terms;
for (int i = 0; i < num_of_terms; i++)
{
cout << "Enter your coefficient : ";
cin >> coeff;
cout << "Enter your exponent : ";
cin >> expo;
//newnode = new polynomial;
if (newnode == NULL)
{
newnode = new polynomial;
newnode->coeff = coeff;
newnode->expo = expo;
newnode->next = NULL;
head1 = newnode;
}
else
{
temp = head1;
newnode = new polynomial;
while (temp->next != NULL)
{
temp = temp->next;
}
newnode->coeff = coeff;
newnode->expo = expo;
temp->next = newnode;
newnode->next = NULL;
}
}
}
//void polynomial::getdata(){}
//void polynomial::read_polynomial(){}
void polynomial::addition_of_polynomial()
{
temp = NULL;
newnode = NULL;
if (head == NULL && head1 == NULL)
{
cout << "first polynomial is empty." << endl;
cout << "second polynomial is empty." << endl;
}
else if (head == NULL)
{
cout << "first polynomial is empty." << endl;
return;
}
else if (head1 == NULL)
{
cout << "second polynomial is empty." << endl;
return;
}
else if ((head != NULL) && (head1 != NULL))
{
int newcoeff = 0;
int newexpo = 0;
polynomial *p, *p1=NULL;
p = head;
p1 = head1;
while ((p != NULL) && (p1 != NULL))
{
if ((p->expo) == (p1->expo))
{
newcoeff = (p->coeff) + (p1->coeff);
newexpo = p1->expo;
p = p->next;
p1 = p1->next;
}
else if ((p->expo) < (p1->expo))
{
newcoeff = p1->coeff;
newexpo = p1->expo;
p1 = p1->next;
}
else if ((p->expo) > (p1->expo))
{
newcoeff = p->coeff;
newexpo = p->expo;
p = p->next;
}
else if (p == NULL)
{
newcoeff = p1->coeff;
newexpo = p1->expo;
p1 = p1->next;
}
else if (p1 == NULL)
{
newcoeff = p->coeff;
newexpo = p->expo;
p = p->next;
}
if (newcoeff != 0)
{
newnode = new polynomial;
newnode->coeff = newcoeff;
newnode->expo = newexpo;
newnode->next = NULL;
if (head2 == NULL)
{
head2 = newnode;
cout << newcoeff << "x^" << newexpo << endl;
}
else
{
temp = head2;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;
cout << newcoeff << "x^" << newexpo << endl;
}
}
}
}
}
void polynomial::multiplication_of_polynomial()
{
temp = NULL;
newnode = NULL;
if (head == NULL && head1 == NULL)
{
cout << "first polynomial is empty." << endl;
cout << "second polynomial is empty." << endl;
}
else if (head == NULL)
{
cout << "first polynomial is empty." << endl;
return;
}
else if (head1 == NULL)
{
cout << "second polynomial is empty." << endl;
return;
}
else if (head != NULL && head1 != NULL)
{
//cout << "Else if k andar." << endl;
polynomial *p, *p1 = NULL;
int counter=0, counter1 = 0;
p = head;
p1 = head1;
newnode = new polynomial;
int newcoeff, newexpo = 0;
while (p != NULL)
{
//cout << "pehla while loop" << endl;
counter++;
p = p->next;
}
while (p1 != NULL)
{
//cout << "2nd while loop" << endl;
counter1++;
p1 = p1->next;
}
p = head;
p1 = head1;
while (p != NULL)
{
//cout << "third while loop" << endl;
while (p1 != NULL)
{
//cout << "fourth while loop" << endl;
newcoeff = (p->coeff)*(p1->coeff);
newexpo = (p->expo) + (p1->expo);
p1 = p1->next;
if (newcoeff != 0)
{
//cout << "fourth while loop k baad jo if statement hai " << endl;
newnode = new polynomial;
newnode->coeff = newcoeff;
newnode->expo = newexpo;
newnode->next = NULL;