Polynomial_Addition
Polynomial_Addition
//This program was executed in Embarcadero Dev C++ 6.3 editor (freeware) on Windows 7 OS
#include<stdio.h>
#include<stdlib.h>
//reading polynomial-1 details from the user and storing them in a Linked List
printf("\nEnter number of terms in the polynomial-1: ");
scanf("%d", &n);
//reading polynomial-2 details from the user and storing them in a Linked List
printf("\nEnter number of terms in the polynomial-2: ");
scanf("%d", &n);
return 0;
}
return Head;
}
//Definition of display() function. It will display details of polynomial
void Display(struct Poly_Node *Head)
{
struct Poly_Node *tp;
tp = Head;
while(tp != NULL)
{
if(tp->coeff > 0)
{
printf("+");
}
printf("%dx(%d)", tp->coeff, tp->power);
tp = tp->Next;
}
}
//Definition of AddPoly() function. It adds two given polynomials and stores the result
//in third polynomial
struct Poly_Node * AddPoly(struct Poly_Node *Head1, struct Poly_Node *Head2, struct Poly_Node
*Head3)
{
struct Poly_Node *tp1, *tp2;
int co, po;
//Adding two polinomials by comparing the powers of each term in two polinomials
tp1 = Head1;
tp2 = Head2;
if(tp1 != NULL) //1st ploy is not completed so, copy its remaining terms into 3rd poly
{
while(tp1 != NULL)
{
co = tp1->coeff;
po = tp1->power;
Head3 = CreateLL(Head3, co, po);
tp1 = tp1->Next;
}
}
if(tp2 != NULL) //2ndt ploy is not completed so, copy its remaining terms into 3rd poly
{
while(tp2 != NULL)
{
co = tp2->coeff;
po = tp2->power;
Head3 = CreateLL(Head3, co, po);
tp2 = tp2->Next;
}
}
return Head3;
}
Output:
Enter number of terms in the polynomial-1: 4
Enter, co-efficient and power of 1 term of Polynomial-1: 5 4
Enter, co-efficient and power of 2 term of Polynomial-1: -3 2
Enter, co-efficient and power of 3 term of Polynomial-1: 9 1
Enter, co-efficient and power of 4 term of Polynomial-1: -6 0