Created
November 9, 2019 18:42
-
-
Save mekhti11/26114eb69568b36b94c3141112425ce4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
struct Polinomial{ | |
int coeff; | |
int exponent; | |
}; | |
struct Polinomial poly1[64],poly2[64],sum[64],product[64]; | |
int readPoly(struct Polinomial poly[64]){ | |
//Kullanicidan polinom alma | |
int count,i ; | |
printf("Enter the total number of terms in the polynomial => "); | |
scanf("%d",&count); | |
printf("{INFO}Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER\n"); | |
for (i = 0; i < count; i++) { | |
printf("Enter the Coefficient of [%d].term =>",i+1); | |
scanf("%d",&poly[i].coeff); | |
printf("Enter the Exponent of [%d].term =>",i+1); | |
scanf("%d",&poly[i].exponent); | |
} | |
return count; | |
} | |
void displayPoly(int len, struct Polinomial p[64]){ | |
int i; | |
for(i=0;i<len-1;i++){ | |
if(p[i].coeff != 0) | |
printf("%dx^%d + ",p[i].coeff,p[i].exponent ); | |
} | |
printf("%dx^%d\n",p[len-1].coeff,p[len-1].exponent ); | |
} | |
int add2Polinomials(struct Polinomial p1[64],int len1, struct Polinomial p2[64],int len2){ | |
int i=0,j=0,k=0,t=0; | |
int expo,coeff,tmp; | |
while(i<len1 && j<len2){ | |
expo = p1[i].exponent;//1st poly expo | |
coeff = p1[i].coeff;//1st poly coeff | |
if(coeff != 0){ | |
tmp = 0; | |
t=i+1; | |
while(expo == p1[t].exponent && t<len1){ | |
coeff+=p1[t].coeff; | |
p1[t].coeff = 0; | |
p1[i].coeff = coeff; | |
t++; | |
} | |
while(expo<=p2[j].exponent && j<len2){ | |
if (expo == p2[j].exponent) { | |
sum[k].exponent = expo; | |
coeff+=p2[j].coeff; | |
sum[k].coeff = coeff; | |
tmp = 1; | |
} | |
else{ | |
sum[k].exponent = p2[j].exponent; | |
sum[k].coeff = p2[j].coeff; | |
k++; | |
} | |
j++; | |
} | |
if (tmp == 0) { // there arent any expo = p1[i].expo in p2 | |
sum[k].exponent = expo; | |
sum[k].coeff = coeff; | |
} | |
k++; | |
} | |
i++; | |
} | |
/* for rest over terms of polynomial 1 */ | |
while(i<len1){ | |
if(p1[i].coeff != 0){ | |
sum[k].coeff=p1[i].coeff; | |
sum[k].exponent=p1[i].exponent; | |
k++; | |
} | |
i++; | |
} | |
/* for rest over terms of polynomial 2 */ | |
while(j<len2){ | |
if(p2[j].coeff){ | |
sum[k].coeff=p2[j].coeff; | |
sum[k].exponent=p2[j].exponent; | |
k++; | |
} | |
j++; | |
} | |
return k; /* k is number of terms in resultant polynomial*/ | |
} | |
int multiply(struct Polinomial p1[64],int len1, struct Polinomial p2[64],int len2){ | |
int i=0,j=0,k=0,t,coeff; | |
for(i=0;i<len1;i++){ | |
for(j=0;j<len2;j++){ | |
product[k].exponent = p1[i].exponent + p2[j].exponent; | |
product[k].coeff = p1[i].coeff * p2[j].coeff; | |
k++; | |
} | |
} | |
while(i<k){ | |
t=i+1; | |
coeff = product[i].coeff; | |
while(product[i].exponent == product[t].exponent && t<k){ | |
coeff += product[t].coeff; | |
product[t].coeff=0; | |
t++; | |
} | |
product[i].coeff=coeff; | |
i=t; | |
} | |
return k; | |
} | |
void writeToTXT(int len,struct Polinomial p[64]){ | |
FILE *f; | |
int i; | |
f = fopen("output.txt", "a"); | |
for(i=0;i<len-1;i++){ | |
if(p[i].coeff != 0) | |
fprintf(f,"%dx^%d + ",p[i].coeff,p[i].exponent ); | |
} | |
fprintf(f,"%dx^%d\n",p[len-1].coeff,p[len-1].exponent ); | |
fclose(f); | |
} | |
int main() { | |
int length_p1,length_p2,length_sum,length_prod; | |
printf("\n...Enter the first Polinomial...\n"); | |
length_p1 = readPoly(poly1); | |
printf("First Polinomial: "); | |
displayPoly(length_p1,poly1); | |
printf("\n...Enter the second Polinomial...\n"); | |
length_p2 = readPoly(poly2); | |
printf("First Polinomial: "); | |
displayPoly(length_p2,poly2); | |
length_sum = add2Polinomials(poly1,length_p1,poly2,length_p2); | |
printf(" \n\n Resultant polynomial after addition : "); | |
displayPoly(length_sum,sum); | |
printf("\n"); | |
length_prod = multiply(poly1,length_p1,poly2,length_p2); | |
printf(" \n\n Resultant polynomial after multiplication : "); | |
displayPoly(length_prod,product); | |
printf("\n"); | |
writeToTXT(length_sum,sum); | |
writeToTXT(length_prod,product); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Polinomial Addition and Multiplication