0% found this document useful (0 votes)
48 views4 pages

Polynomial

This C program implements polynomial operations using linked lists. It allows the user to create polynomials by entering coefficients and exponents, evaluate polynomials by entering a value for x, add and multiply two polynomials by linking nodes together, and print the polynomials. The main function contains a menu loop that calls functions for these polynomial operations and switches between them based on the user's selection.

Uploaded by

Gitanjali Gomare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views4 pages

Polynomial

This C program implements polynomial operations using linked lists. It allows the user to create polynomials by entering coefficients and exponents, evaluate polynomials by entering a value for x, add and multiply two polynomials by linking nodes together, and print the polynomials. The main function contains a menu loop that calls functions for these polynomial operations and switches between them based on the user's selection.

Uploaded by

Gitanjali Gomare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<stdio.

h>
#include<stdlib.h>
#include<math.h>
#include<malloc.h>
struct node
{
int exp,coef;
struct node *next;
};
struct node *insert(struct node *headnd,int e,int c);
struct node *mult(struct node *p1,struct node *p2);
struct node *add(struct node *p1,struct node *p2);
struct node *createpoly();
int evaluate(struct node *headnd,int x);
void printpoly(struct node *headnd);
int menu();
void main()
{
struct node *p1,*p2,*p3;
p1=p2=p3=NULL;
int choice,x,ans;
while(1)
{
choice=menu();
switch(choice)
{
case 1:
{
printf("enter first polynomial\n");
p1=createpoly();
printf("enter second polynomial\n");
p2=createpoly();
printf("first polynomial is\n");
printpoly(p1);
printf("second polynomial is\n");
printpoly(p2);
break;
}
case 2:
{
printf("first polynomial is\n");
printpoly(p1);
printf("enter the value of X\n");
scanf("%d",&x);
ans=evaluate(p1,x);
printf("evaluate value = %d",ans);
break;
}
case 3:
{
printf("first polynomial is\n");
printpoly(p1);
printf("second polynomial is\n");
printpoly(p2);
p3=add(p1,p2);
printf("addition of these 2 polynomials is\n");
printpoly(p3);
break;
}
case 4:
{
printf("first polynomial is\n");
printpoly(p1);
printf("second polynomial is\n");
printpoly(p2);
p3=mult(p1,p2);
printf("multiplication of these 2 polynomials is\n");
printpoly(p3);
break;
}
case 5:
{
exit(0);
}
default:
{
printf("wrong choice\n");
}
}
}
}
struct node *insert(struct node *headnd,int e,int c)
{
struct node *newnd,*current;
newnd=(struct node*)malloc(sizeof(struct node));
newnd->exp=e;
newnd->coef=c;
newnd->next=NULL;
if(headnd==NULL)
{
headnd=newnd;
headnd->next=headnd;
return headnd;
}
if(e<headnd->exp)//lowest exponent inserted at end
{
newnd->next=headnd->next;
headnd->next=newnd;
headnd=newnd;
return headnd;
}
if(e==headnd->exp)
{
headnd->coef+=c;
return headnd;
}
for(current=headnd;current->next!=headnd;current=current->next)
{
if(e>current->next->exp)
{
break;
}
}
if(newnd->exp==current->exp)
{
current->coef+=c;
}
else
{
newnd->next=current->next;
current->next=newnd;
}
return headnd;
}
struct node *createpoly()
{
int n,i,e,c;
struct node *head=NULL;
printf("how many terms\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%d. enter a term (coefficient,exponent)\n",i+1);
scanf("%d%d",&c,&e);
head=insert(head,e,c);
}
return head;
}
struct node *mult(struct node *p1,struct node *p2)
{
struct node *current1,*current2;
struct node *headnd=NULL;
current1=p1->next;
do
{
current2=p2->next;
do
{
headnd=insert(headnd,current1->exp+current2->exp,(current1-
>coef)*(current2->coef));
current2=current2->next;
}while(current2!=p2->next);
current1=current1->next;
}while(current1!=p1->next);
return headnd;
}
struct node *add(struct node *p1,struct node *p2)
{
struct node *current;
struct node *headnd=NULL;
int exp,coef;
current=p1->next;
do//copy first polynomial as it is
{
headnd=insert(headnd,current->exp,current->coef);
current=current->next;
}while(current!=p1->next);
current=p2->next;
do//insert second polynomial
{
headnd=insert(headnd,current->exp,current->coef);
current=current->next;
}while(current!=p2->next);
return headnd;
}
int evaluate(struct node *headnd,int x)
{
int sum=0;
struct node *current;
current=headnd->next;
do
{
sum+=current->coef*pow(x,current->exp);
current=current->next;
}while(current!=headnd->next);
return sum;
}
void printpoly(struct node *headnd)
{
struct node *current;
current=headnd->next;
do
{
printf("%dx^%d",current->coef,current->exp);
if(current!=headnd)
{
printf(" + ");
}
current=current->next;
}while(current!=headnd->next);
}
int menu()
{
int choice;
printf("MENU DRIVEN PROGRAM\n");
printf("enter your choice\n1.read polynomial\n2.evaluation of 1st
polynomial\n3.addition of 2 polynomial\n4.multiplication of 2
polynomial\n5.exit\n");
scanf("%d",&choice);
return choice;
}

You might also like