forked from swaaz/basicprograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolynomial.c
More file actions
67 lines (58 loc) · 1.25 KB
/
Copy pathpolynomial.c
File metadata and controls
67 lines (58 loc) · 1.25 KB
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
#include<stdio.h>
#include<stdlib.h>
struct poly
{
int coeff;
int exp;
struct poly *next;
};
void add(struct poly **p,int c,int e) // adding nodes for each order
{
struct poly *new=*p,*temp;
new=(struct poly *)malloc(sizeof(struct poly));
new->coeff=c;
new->exp=e;
new->next=NULL;
if(*p==NULL) // if start is null
*p=new;
else
{
struct poly *temp=*p;
while(temp->next!=NULL)
temp=temp->next;
temp->next=new;
}
}
void display(struct poly **p) // for displaying each node that contains different order of X
{
struct poly*temp=*p;
while(temp!=NULL)
{
printf("+%d",temp->coeff);
if(temp->exp!=0)
printf("x^%d",temp->exp);
temp=temp->next;
}
}
int main()
{
int co,ex;
char ch='y';
struct poly *start=NULL;
printf("Enter the polynomial from highest order to lowest\n");
while(ch=='y')
{
printf("Enter the coefficient of x:\n");
scanf("%d",&co);
printf("enter the exponent of x:\n");
scanf("%d",&ex);
add(&start,co,ex); //calling add function
printf("\n\tDo u want to add more (y/n)");
getchar();
scanf("%c",&ch);
}
printf("\n\tThe polynomial expression is:\t");
display(&start); //final display
printf("\n");
return 0;
}