1: /*
2: ==============================================================
3: Name : Polynomial Using CLL
4: Author : Mujahid, Shivkumar
5: Version :
6: Copyright : Your copyright notice
7: Description : Hello World in C, Ansi-style
8: ===========================================================
9: */
10: #include<stdio.h>
11: #include<stdlib.h>
12: struct node
13: {
14: float coef;
15: int expo;
16: struct node *next;
17: };
18: struct node *create(struct node *);
19: struct node *insert_s(struct node *,float,int);
20: struct node *insert(struct node *,float,int);
21: void display(struct node *ptr);
22: void poly_add(struct node *,struct node *);
23: void poly_mult(struct node *,struct node *);
24: int main( )
25: {
26: int op;
27: struct node *start1=NULL,*start2=NULL;
28: do{
29: printf("1.Create\n2.Display\n3.Addition\n4.Multiplication\n5.Exit\nEnter Option : ");
30: scanf("%d",&op);
31: switch(op)
32: {
33: case 1:
34: printf("Enter polynomial 1 :\n");
35: start1=create(start1);
36: printf("Enter polynomial 2 :\n");
37: start2=create(start2);
38: printf("\nPolynomial 1 is : ");
39: display(start1);
40: printf("\nPolynomial 2 is : ");
41: display(start2);
42: break;
43: case 2:
44: printf("\nPolynomial 1 is : ");
45: display(start1);
46: printf("\nPolynomial 2 is : ");
47: display(start2);
48: break;
49: case 3:
50: poly_add(start1, start2);
51: break;
52: case 4:
53: poly_mult(start1, start2);
54: break;
55: case 5:
56: exit(0);
57: break;
58: default:
59: printf("\nX Wrong Input X!");
60: }
61: }while(op!=5);
62: return 0;
63: }
64: struct node *create(struct node *start)
65: {
66: int i,n,exp;
67: float coeff;
68: printf("Enter the number of terms : ");
69: scanf("%d",&n);
70: for(i=1;i<=n;i++)
71: {
72: printf("Enter coeficient for term %d : ",i);
73: scanf("%f",&coeff);
74: printf("Enter exponent for term %d : ",i);
75: scanf("%d",&exp);
76: start=insert_s(start,coeff,exp);
77: }
78: return start;
79: }
80: struct node *insert_s(struct node *start,float co,int ex)
81: {
82: struct node *ptr,*tmp;
83: tmp=(struct node *)malloc(sizeof(struct node));
84: tmp->coef=co;
85: tmp->expo=ex;
86: /*list empty or exp greater than first one */
87: if(start==NULL || ex > start->expo)
88: {
89: tmp->next=start;
90: start=tmp;
91: }
92: else
93: {
94: ptr=start;
95: while(ptr->next!=NULL && ptr->next->expo >= ex)
96: ptr=ptr->next;
97: tmp->next=ptr->next;
98: ptr->next=tmp;
99: }
100: return start;
101: }
102: struct node *insert(struct node *start,float co,int ex)
103: {
104: struct node *ptr,*tmp;
105: tmp=(struct node *)malloc(sizeof(struct node));
106: tmp->coef=co;
107: tmp->expo=ex;
108: /*If list is empty*/
109: if(start==NULL)
110: {
111: tmp->next=start;
112: start=tmp;
113: }
114: else /*Insert at the end of the list*/
115: {
116: ptr=start;
117: while(ptr->next!=NULL)
118: ptr=ptr->next;
119: tmp->next=ptr->next;
120: ptr->next=tmp;
121: }
122: return start;
123: }
124: void display(struct node *ptr)
125: {
126: if(ptr==NULL)
127: {
128: printf("\nZero polynomial\n");
129: return;
130: }
131: printf("\n");
132: while(ptr!=NULL)
133: {
134: printf("(%.1fx^%d)", ptr->coef,ptr->expo);
135: ptr=ptr->next;
136: if(ptr!=NULL)
137: printf(" + ");
138: else
139: printf("\n");
140: }
141: }
142: void poly_add(struct node *p1,struct node *p2)
143: {
144: struct node *start3;
145: start3=NULL;
146: while(p1!=NULL && p2!=NULL)
147: {
148: if(p1->expo > p2->expo)
149: {
150: start3=insert(start3,p1->coef,p1->expo);
151: p1=p1->next;
152: }
153: else if(p2->expo > p1->expo)
154: {
155: start3=insert(start3,p2->coef,p2->expo);
156: p2=p2->next;
157: }
158: else if(p1->expo==p2->expo)
159: {
160: start3=insert(start3,p1->coef+p2->coef,p1->expo);
161: p1=p1->next;
162: p2=p2->next;
163: }
164: }
165: /*if poly2 has finished and elements left in poly1*/
166: while(p1!=NULL)
167: {
168: start3=insert(start3,p1->coef,p1->expo);
169: p1=p1->next;
170: }
171: /*if poly1 has finished and elements left in poly2*/
172: while(p2!=NULL)
173: {
174: start3=insert(start3,p2->coef,p2->expo);
175: p2=p2->next;
176: }
177: printf("\nAdded polynomial is : ");
178: display(start3);
179: }
180: void poly_mult(struct node *p1, struct node *p2)
181: {
182: struct node *start3;
183: struct node *p2_beg = p2;
184: start3=NULL;
185: if(p1==NULL || p2==NULL)
186: {
187: printf("\nMultiplied polynomial is zero polynomial\n");
188: return;
189: }
190: while(p1!=NULL)
191: {
192: p2=p2_beg;
193: while(p2!=NULL)
194: {
195: start3=insert_s(start3,p1->coef*p2->coef,p1->expo+p2->expo);
196: p2=p2->next;
197: }
198: p1=p1->next;
199: }
200: printf("\nMultiplied polynomial is : ");
201: display(start3);
202: }
203: ---------------OUTPUT--------------
204: 1.Create
205: 2.Display
206: 3.Addition
207: 4.Multiplication
208: 5.Exit
209: Enter Option : 1
210: Enter polynomial 1 :
211: Enter the number of terms : 3
212: Enter coeficient for term 1 : 6
213: Enter exponent for term 1 : 2
214: Enter coeficient for term 2 : 5
215: Enter exponent for term 2 : 1
216: Enter coeficient for term 3 : 8
217: Enter exponent for term 3 : 0
218: Enter polynomial 2 :
219: Enter the number of terms : 3
220: Enter coeficient for term 1 : 7
221: Enter exponent for term 1 : 2
222: Enter coeficient for term 2 : 81
223: Enter exponent for term 2 : 1
224: Enter coeficient for term 3 : 7
225: Enter exponent for term 3 : 0
226: Polynomial 1 is :
227: (6.0x^2) + (5.0x^1) + (8.0x^0)
228: Polynomial 2 is :
229: (7.0x^2) + (81.0x^1) + (7.0x^0)
230: 1.Create
231: 2.Display
232: 3.Addition
233: 4.Multiplication
234: 5.Exit
235: Enter Option : 3
236: Added polynomial is :
237: (13.0x^2) + (86.0x^1) + (15.0x^0)
238: 1.Create
239: 2.Display
240: 3.Addition
241: 4.Multiplication
242: 5.Exit
243: Enter Option : 4
244: Multiplied polynomial is :
245: (42.0x^4) + (486.0x^3) + (35.0x^3) + (42.0x^2) + (405.0x^2) + (56.0x^2) + (35.0x^1) + (648.0x^1) + (56.0x^0)
246: 1.Create
247: 2.Display
248: 3.Addition
249: 4.Multiplication
250: 5.Exit
251: Enter Option : 5
No comments:
Post a Comment