File tree 1 file changed +68
-0
lines changed
1 file changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <math.h>
4
+ // representation of polynomial
5
+ struct Node {
6
+ int coeff ; // coefficient of poly
7
+ int exp ; // exponential of poly
8
+ struct Node * next ; // pointer to next node
9
+ }* poly = NULL ;
10
+
11
+ void Create ()
12
+ {
13
+ struct Node * t , * last = NULL ; // temp node to new node ; and ptr to last nnode
14
+ int num , i ;
15
+ printf ("Enter number of terms " );
16
+ scanf_s ("%d" , & num );
17
+ printf ("Enter each terms with there coeff and exp\n" );
18
+ for (i = 0 ; i < num ; i ++ )
19
+ {
20
+ t = (struct Node * )malloc (sizeof (struct Node )); // for each term we need to create node
21
+ scanf_s ("%d %d" , & t -> coeff , & t -> exp ); // eading two value
22
+ t -> next = NULL ; // last node will be null
23
+ if (poly == NULL ) // if it is 1st node
24
+ {
25
+ poly = last = t ; // all sud point on 1st
26
+ }
27
+ else {
28
+ last -> next = t ; // moving upto last and inserion
29
+ last = t ;
30
+ }
31
+ }
32
+
33
+
34
+ }
35
+ void Display (struct Node * p )
36
+ {
37
+ while (p )
38
+ {
39
+ printf ("%dx%d +" , p -> coeff , p -> exp );
40
+ p = p -> next ;
41
+ }
42
+ printf ("\n" );
43
+ }
44
+
45
+ long eval (struct Node * p , int x )
46
+ {
47
+ double sum = 0 ;
48
+ while (p )
49
+ {
50
+ sum += p -> coeff * pow (x , p -> exp );
51
+ p = p -> next ;
52
+ }
53
+ return sum ;
54
+
55
+ }
56
+
57
+
58
+
59
+
60
+
61
+
62
+ int main ()
63
+ {
64
+ Create ();
65
+ Display (poly );
66
+ printf ("%ld\n" , eval (poly , 2 ));
67
+ return 0 ;
68
+ }
You can’t perform that action at this time.
0 commit comments