-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingly.c
More file actions
131 lines (97 loc) · 3.12 KB
/
singly.c
File metadata and controls
131 lines (97 loc) · 3.12 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
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
#include <stdio.h>
#include <stdlib.h>
struct Node{
int coefficient;
int exponent;
struct Node*next;
};
int main (){
struct Node*poly1=NULL,*poly2=NULL,*result=NULL;
struct Node*temp1,*temp2,*tempResult;
int terms,coef,exp;
printf ("Enter the number of terms in the first polynomial :");
scanf("%d",&terms);
printf("Enter each term in the format: coefficient : exponent:\n");
for (int i=0; i<terms; i++){
printf("Term %d:", i+1);
scanf ("%d %d", &coef, &exp);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->coefficient = coef;
newNode->exponent = exp;
newNode->next = NULL;
if (poly1 ==NULL){
poly1 =newNode;
} else{
temp1= poly1;
while (temp1-> next!=NULL){
temp1=temp1->next;
}
temp1->next=newNode;
}
}
printf ("\nEnter the number of terms in the second polynomial :");
scanf("%d",&terms);
printf("Enter each term in the format: coefficient : exponent:\n");
for (int i=0; i<terms; i++){
printf("Term %d:", i+1);
scanf ("%d %d", &coef, &exp);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->coefficient = coef;
newNode->exponent = exp;
newNode->next = NULL;
if (poly2 ==NULL){
poly2 = newNode;
} else{
temp2= poly2;
while (temp2-> next!=NULL){
temp2=temp2->next;
}
temp2->next=newNode;}}
temp1=poly1;
temp2=poly2;
while (temp1!=NULL || temp2!=NULL){
int coef=0, exp=0;
if (temp1==NULL){
coef=temp2->coefficient;
exp=temp2->exponent;
temp2=temp2->next;
}else if (temp2==NULL){
coef=temp1->coefficient;
exp=temp1->exponent;
temp1=temp1->next;
}else if(temp1->exponent> temp2->exponent){
coef=temp1->coefficient;
exp=temp1->exponent;
temp1=temp1->next;
}else if(temp1->exponent< temp2->exponent){
coef=temp2->coefficient;
exp=temp2->exponent;
temp2=temp2->next;
}else{
coef=temp1->coefficient+temp2->coefficient;
exp=temp1->exponent;
temp1=temp1->next;
temp2=temp2->next;
}
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->coefficient = coef;
newNode->exponent = exp;
newNode->next = NULL;
if (result ==NULL){
result= newNode;
} else{
tempResult= result;
while (tempResult-> next!=NULL){
tempResult=tempResult->next;
}
tempResult->next=newNode; } }
printf ("\nthe sum of the polynomials is :\n ");
tempResult=result;
while (tempResult!=NULL){
if (tempResult->coefficient>=0 && tempResult!=result){
printf("+");
}
printf("%dx^%d",tempResult->coefficient,tempResult->exponent);
tempResult= tempResult ->next; }
printf("\n");
return 0; }