-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolyArray.c
More file actions
95 lines (91 loc) · 2.06 KB
/
PolyArray.c
File metadata and controls
95 lines (91 loc) · 2.06 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
#include<stdio.h>
#define max 100
#define COMPARE(x,y)((x==y?0:(x>y)?1:-1))
typedef struct
{
int expo;
int coeff;
}poly;
int avail=0;
void attach(int coeff, int expo, poly p[])
{
p[avail].coeff=coeff;
p[avail].expo=expo;
avail++;
}
void addPoly(poly p[], int sa, int ea, int sb, int eb, int*sc, int *ec)
{
*sc=avail;
int coeff;
while(sa<=ea&&sb<=eb)
{
switch(COMPARE(p[sa].expo,p[sb].expo))
{
case -1:
attach(p[sb].coeff, p[sb].expo, p);
sb++;
break;
case 1:
attach(p[sa].coeff, p[sa].expo, p);
sa++;
break;
case 0:
coeff=p[sa].coeff+p[sb].coeff;
if(coeff!=0)
{
attach(coeff, p[sa].expo, p);
}
sa++;
sb++;
break;
}
}
while(sa<=ea)
{
attach(p[sa].coeff, p[sa].expo, p);
sa++;
}
while(sb<=eb)
{
attach(p[sb].coeff, p[sb].expo, p);
sb++;
}
*ec=avail-1;
}
poly readPoly(poly p[], int* start, int* end)
{
*start=avail;
do
{
printf("Enter coeff and exponent");
scanf("%d%d",&p[avail].coeff,&p[avail].expo);
avail++;
}while(p[avail-1].expo!=0);
*end=avail-1;
}
void print(poly p[], int start, int end)
{
int i=start;
while(i<end)
{
printf("%d x ^ %d", p[i].coeff, p[i].expo);
i++;
if(p[i].coeff>0)
printf("+ ");
}
printf("%d x ^ %d ", p[i].coeff, p[i].expo);
}
void main()
{
poly p[max];
int startA,startB,startC;
int endA,endB,endC;
printf("Enter the first polynomial\n");
readPoly(p,&startA, &endA);
print(p,startA, endA);
printf("Enter the second polynomial\n");
readPoly(p,&startB, &endB);
print(p,startB, endB);
addPoly(p, startA, endA, startB, endB, &startC, &endC);
print(p,startC, endC);
}