-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOLYNOMIAL.c
134 lines (134 loc) · 2.96 KB
/
POLYNOMIAL.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
typedef struct polynomial
{
float coeff;
int x,y,z;
struct polynomial *next;
}poly;
poly *p1,*p2,*p3;
poly* readpoly()
{
poly *temp=(poly*)malloc(sizeof(poly));
printf("\nEnter coeff:");
scanf("%f",&temp->coeff);
printf("Enter x expon:");
scanf("%d",&temp->x);
printf("Enter y expon:");
scanf("%d",&temp->y);
printf("Enter z expon:");
scanf("%d",&temp->z);
return temp;
}
poly* create()
{
int n,i;
printf("\nEnter no. of terms:");
scanf("%d",&n);
poly *temp=(poly*)malloc(sizeof(poly)),*t1=temp;
for(i=0;i<n;i++,t1=t1->next)
t1->next=readpoly();
t1->next=temp;
return temp;
}
void evaluate()
{
float sum=0;
int x,y,z;
poly *t=p1->next;
printf("\nEnter x,y&z:\n");
scanf("%d",&x);
scanf("%d",&y);
scanf("%d",&z);
while(t!=p1)
{
sum+=t->coeff*pow(x,t->x)*pow(y,t->y)*pow(z,t->z);
t=t->next;
}
printf("\nSum=%f",sum);
}
void display(poly *p)
{
poly *t=p->next;
while(t!=p)
{
if(t!=p->next&&t->coeff>0)
putchar('+');
printf("%.1fx^%dy^%dz^%d",t->coeff,t->x,t->y,t->z);
t=t->next;
}
}
poly* attach(float coeff,int x,int y,int z,poly *p)
{
poly *t=(poly*)malloc(sizeof(poly));
t->coeff=coeff;
t->x=x;
t->y=y;
t->z=z;
p->next=t;
return t;
}
poly* add()
{
printf("\nPolynomial1:\n");
p1=create();
printf("\nPolynomial2:\n");
p2=create();
int flag;
poly *t1=p1->next,*t2=p2->next,*t3;
p3=(poly*)malloc(sizeof(poly));
t3=p3;
while(t1!=p1&&t2!=p2)
{
if(t1->x>t2->x)
flag=1;
else if(t1->y<t2->y)
flag=-1;
else if(t1->z==t2->z)
flag=0;
switch(flag)
{
case 0:t3=attach(t1->coeff+t2->coeff,t1->x,t1->y,t1->z,t3);
t1=t1->next;
t2=t2->next;
break;
case 1:t3=attach(t1->coeff,t1->x,t1->y,t1->z,t3);
t1=t1->next;
break;
case -1:t3=attach(t2->coeff,t2->x,t2->y,t2->z,t3);
t2=t2->next;
break;
}
}
for(;t1!=p1;t1=t1->next)
t3=attach(t1->coeff,t1->x,t1->y,t1->z,t3);
for(;t2!=p2;t2=t2->next)
t3=attach(t2->coeff,t2->x,t2->y,t2->z,t3);
t3->next=p3;
return p3;
}
int main()
{
int ch;
printf("\n1.Represent and evaluate polynomial\n2.Add 2 polynomials\n3.Exit\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:p1=create();
display(p1);
evaluate();
break;
case 2:p3=add();
printf("\nPolynomial1:\n");
display(p1);
printf("\nPolynomial2:\n");
display(p2);
printf("\nP1+P2:\n");
display(p3);
break;
case 3:exit(0);
default:printf("\nInvalid choice...!");
}
return 0;
}