-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolynomialAddition.c
More file actions
100 lines (100 loc) · 2.7 KB
/
polynomialAddition.c
File metadata and controls
100 lines (100 loc) · 2.7 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
#include <stdio.h>
#include <stdlib.h>
#define max_trem 100
typedef struct {
float coef;
int exp;
}trem;
int avail;
trem polynomial[max_trem];
void attach(float co,int ex){
if(avail>=max_trem){
printf("limit Exist");
exit(0);
}
else{
polynomial[avail].coef=co;
polynomial[avail++].exp=ex;
}
}
void polyprint(int start,int end){
int i;
for(i=start;i<=end;i++)
printf("%.1f X^%d + ",polynomial[i].coef,polynomial[i].exp);
if(polynomial[i].exp!=0)
printf("%.1f X^%d\n",polynomial[i].coef,polynomial[i].exp);
else
printf("%.1f \n",polynomial[i].coef);
}
int compire(int a,int b){
if(a>b)
return 1;
if(a<b)
return -1;
if(a==b)
return 0;
}
void padd(int startA,int endA,int startB,int endB,int *startD,int *endD){
*startD=avail;
while(startA<=endA&&startB<=endB){
switch(compire(polynomial[startA].exp,polynomial[startB].exp)){
case 1:attach(polynomial[startA].coef,polynomial[startA].exp);
startA++;
break;
case 0:attach(polynomial[startA].coef+polynomial[startB].coef,polynomial[startA].exp);
startA++;
startB++;
break;
case -1:attach(polynomial[startB].coef,polynomial[startB].exp);
startB++;
break;
}
}
for( ;startA<=endA;startA++)
attach(polynomial[startA].coef,polynomial[startA].exp);
for( ;startB<=endB;startB++)
attach(polynomial[startB].coef,polynomial[startB].exp);
*endD=avail-1;
}
void pSort(int start,int end){
int i,j;
trem t;
for(i=start;i<end;i++)
for(j=i+1;j<=end;j++)
if(polynomial[i].exp<polynomial[j].exp){
t=polynomial[i];
polynomial[i]=polynomial[j];
polynomial[j]=t;
}
}
void pScan(int *start,int *end){
int n;
printf("enter number of trems of polynomial\t");
scanf("%d",&n);
float co;
int ex;
*start=avail;
for(int i=0;i<n;i++){
printf("Enter co-eficent\t");
scanf("%f",&co);
printf("Enter Exponent\t");
scanf("%d",&ex);
attach(co,ex);
}
*end =avail-1;
pSort(*start,*end);
}
void main(){
int startA,endA,startB,endB,startC,endC;
printf("Enter Detilus of First polinimial\n");
pScan(&startA,&endA);
printf("Enter Detilus of 2nd polinimial\n");
pScan(&startB,&endB);
printf("polinomial 1 is ");
polyprint(startA,endA);
printf("polinomial 2 is ");
polyprint(startB,endB);
padd(startA,endA,startB,endB,&startC,&endC);
printf("Result \n");
polyprint(startC,endC);
}