#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();
void evaluate_polynomial();
void display_polynomial();
};
polynomial *head, *head1,*head2, *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)
{
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;
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()
{
}
void polynomial:: evaluate_polynomial(){}
void polynomial::display_polynomial()
{
temp = NULL;
newnode = NULL;
if (head != 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)
{
//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;
}
}
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();
}
t.addition_of_polynomial();
t.display_polynomial();
_getch();
}