-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolynomialaddition.c
139 lines (123 loc) · 3.38 KB
/
polynomialaddition.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
135
136
137
138
139
#include <stdio.h>
#include <stdlib.h>
struct node {
int coefficient;
int exponent;
struct node *ptr;
};
struct node *poly1;
struct node *poly2;
void polynomialInput(struct node **, int);
void display(struct node *);
void additionPoly();
int main() {
int noOfTerms1, noOfTerms2;
printf("Enter the first polynomial details\n");
printf("Enter the number of terms : ");
scanf("%d", &noOfTerms1);
polynomialInput(&poly1, noOfTerms1);
display(poly1);
printf("\n\nEnter the second polynomial details\n");
printf("Enter the number of terms : ");
scanf("%d", &noOfTerms2);
polynomialInput(&poly2, noOfTerms2);
display(poly2);
printf("\n\nAddition of given polynomials : \n");
additionPoly();
return 0;
}
void polynomialInput(struct node **poly, int noOfTerms) {
int coeff, exp, cont;
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node *));
*poly = temp;
for(int i=0; i<noOfTerms; i++) {
printf("Exponent : ");
scanf("%d", &exp);
printf("Coeffecient of (x^%d) : ", exp);
scanf("%d", &coeff);
temp->coefficient = coeff;
temp->exponent = exp;
temp-> ptr = NULL;
if(i != noOfTerms-1){
temp->ptr = (struct node *)malloc(sizeof(struct node *));
temp = temp->ptr;
temp->ptr = NULL;
}
}
}
void display(struct node *poly) {
printf("\nYour polynomial expression is : \n");
while(poly != NULL){
printf("%d (x^%d)", poly->coefficient, poly->exponent);
poly = poly->ptr;
if(poly != NULL){
printf(" + ");
}
}
}
void additionPoly() {
int sum;
while(poly1!=NULL && poly2!=NULL) {
if(poly1->exponent == poly2->exponent) {
printf("%d (x^%d)", poly1->coefficient+poly2->coefficient, poly1->exponent);
poly1 = poly1->ptr;
poly2 = poly2->ptr;
if(poly1!=NULL || poly2!=NULL){
printf(" + ");
}
}
else if(poly1->exponent > poly2->exponent) {
printf("%d (x^%d)", poly1->coefficient, poly1->exponent);
poly1 = poly1->ptr;
if(poly1!=NULL || poly2!=NULL){
printf(" + ");
}
}
else{
printf("%d (x^%d)", poly2->coefficient, poly2->exponent);
poly2 = poly2->ptr;
if(poly1!=NULL || poly2!=NULL){
printf(" + ");
}
}
}
while(poly1 != NULL) {
printf("%d (x^%d)", poly1->coefficient, poly1->exponent);
poly1 = poly1->ptr;
if(poly1!=NULL || poly2!=NULL){
printf(" + ");
}
}
while(poly2 != NULL) {
printf("%d (x^%d)", poly2->coefficient, poly2->exponent);
poly2 = poly2->ptr;
if(poly1!=NULL || poly2!=NULL){
printf(" + ");
}
}
}
/*
Enter the first polynomial details
Enter the number of terms : 3
Exponent : 2
Coeffecient of (x^2) : 3
Exponent : 1
Coeffecient of (x^1) : 2
Exponent : 0
Coeffecient of (x^0) : 7
Your polynomial expression is :
3 (x^2) + 2 (x^1) + 7 (x^0)
Enter the second polynomial details
Enter the number of terms : 3
Exponent : 3
Coeffecient of (x^3) : 5
Exponent : 2
Coeffecient of (x^2) : 2
Exponent : 1
Coeffecient of (x^1) : 1
Your polynomial expression is :
5 (x^3) + 2 (x^2) + 1 (x^1)
Addition of given polynomials :
5 (x^3) + 5 (x^2) + 3 (x^1) + 7 (x^0)
*/