problem solving techniques using the concepts of linked list.

help me i want to implement this .....


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

1. ADD polynomial
2. MULTIPLY polynomial
3. EVALUATE polynomial
4. QUIT

Enter choice # => 1
Enter the file containing the polynomial to add => ptest2
8.0x^4 + 4.0x^3 + -3.0x + 9.0
Sum: 4.0x^5 + 8.0x^4 + 2.0x^3 + -1.0x + 12.0

1. ADD polynomial
2. MULTIPLY polynomial
3. EVALUATE polynomial
4. QUIT

Enter choice # => 2
Enter the file containing the polynomial to multiply => ptest2
8.0x^4 + 4.0x^3 + -3.0x + 9.0
Product: 32.0x^9 + 16.0x^8 + -16.0x^7 + -20.0x^6 + 52.0x^5 + 38.0x^4 + -6.0x^3 + -6.0x^2 + 9.0x + 27.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.
Last edited on
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.
#include<iostream>
#include<conio.h>

using namespace std;

class polynomial
{
private:
int expo;
int coeff;
polynomial *next;
int num_of_terms;
public:
polynomial();
polynomial(int e, int c);
void polynomial1();
void polynomial2();
void getdata();
void read_polynomial();
void addition_of_polynomial();
void polynomial::multiplication_of_polynomial();
void polynomial::evaluate_poly();
};

polynomial *head, *head1,*head2, *temp, *newnode = NULL;

polynomial::polynomial(){ expo = 0; coeff = 0; }

polynomial::polynomial(int e, int c){ expo = e; coeff = c; }

void polynomial:: polynomial1()
{
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()
{

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()
{
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;
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;
}
if (newcoeff != 0)
{
newnode = new polynomial;

}
}
}
}
void polynomial::multiplication_of_polynomial(){}
void polynomial:: evaluate_poly(){}
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.
Last edited on
#include<iostream>
#include<conio.h>

using namespace std;

class polynomial
{
private:
int expo;
int coeff;
polynomial *next;
int num_of_terms;
public:
polynomial();
polynomial(int e, int c,int n);
void polynomial1();
void polynomial2();
//void write_polynomial();
//void read_polynomial();
void addition_of_polynomial();
void multiplication_of_polynomial();
int evaluate_polynomial();
void display_polynomial();
};

polynomial *head, *head1,*head2,*head3, *temp, *newnode = NULL;

polynomial::polynomial()
{
expo = 0;
coeff = 0;
num_of_terms = 0;
}

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;

if (head3 == NULL)
{
//cout << "if k andar jo if hai " << endl;
head3 = newnode;
cout << newcoeff << "x^" << newexpo << endl;
}
else
{
//cout << "else wali" << endl;
temp = head3;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;
cout << newcoeff << "x^" << newexpo << endl;
}
}
}
//cout << "jahan p main p ka next aur p1 main haead dalna hai" << endl;
p = p->next;
p1 = head1;
}
}
}
int polynomial:: evaluate_polynomial()
{
int input=0;
temp = NULL;
newnode = NULL;
//int newcoeff=0, newexpo = 0;
int evaluation_result = 0;
temp = head;
cout << "Enter value of X : " ;
cin >> input;
while (temp != NULL)
{
//evaluation_result = evaluation_result + pow((coeff*input), expo);
evaluation_result = (evaluation_result) + (temp->coeff) * (pow(input, temp->expo));
temp = temp->next;
}
return evaluation_result;
}
void polynomial::display_polynomial()
{
temp = NULL;
newnode = NULL;
if (head != NULL)
{
temp = NULL;
//polynomial *temp;
temp = head;
cout << endl << "--poly1--" << endl << endl;
while (temp->next != NULL)
{
cout << temp->coeff << "x^" << temp->expo << " + ";
temp = temp->next;
}
cout << temp->coeff << "x^" << temp->expo;
}
temp = NULL;
newnode = NULL;
if (head1 != NULL)
{
temp = NULL;
newnode = NULL;
//polynomial *temp1;
temp = head1;
cout << endl << endl << "--poly2--" << endl << endl;
while (temp->next != NULL)
{
cout << temp->coeff << "x^" << temp->expo << " + ";
temp = temp->next;
}
cout << temp->coeff << "x^" << temp->expo;
}
temp = NULL;
newnode = NULL;
if (head2 != NULL)
{
temp = NULL;
//polynomial *temp3;
temp = head2;
cout << endl << endl << "--sum--" << endl << endl;
while (temp->next != NULL)
{
cout << temp->coeff << "x^" << temp->expo << " + ";
temp = temp->next;
}
cout << temp->coeff << "x^" << temp->expo;
}
//cout << "multi k if say pehlay " << endl;
temp = NULL;
newnode = NULL;
if (head3 != NULL)
{
//cout << "if k andar " << endl;
temp = NULL;
//polynomial *temp3;
temp = head3;
cout << endl << endl << "--multiplication--" << endl << endl;
while (temp->next != NULL)
{
//cout << "while k andar" << endl;
cout << temp->coeff << "x^" << temp->expo << " + ";
temp = temp->next;
}
cout << temp->coeff << "x^" << temp->expo;
}
//cout << "puray if k baad " << endl;
}

void main()
{
polynomial t;
char ch;
//clrscr();
cout << "do u want enter poly expression 1 (y/n) :";
cin >> ch;
if (ch == 'y')
{
t.polynomial1(); //function call
}
//cout << "do u want enter poly expression 2 (y/n) :";
//cin >> ch;
//if (ch == 'y')
//{
//t.polynomial2();
//}
cout << t.evaluate_polynomial();
//t.addition_of_polynomial();
//t.display_polynomial();
//t.multiplication_of_polynomial();
//t.display_polynomial();
_getch();
}



almost done my code ... check out this .....
check out this

That is awful to read as is. Please add code tags and intuitive indentation. They enhance the readability significantly.
Topic archived. No new replies allowed.