Created
November 10, 2019 20:19
-
-
Save mekhti11/42a0e61f8621c2a21dab28629b4611fd 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> | |
#include<stdlib.h> | |
typedef struct link { | |
int coeff; | |
int pow; | |
struct link * next; | |
} poly; | |
/** The prototypes */ | |
void create_poly(poly **); | |
void show_poly(poly *); | |
void add_poly(poly **, poly *, poly *); | |
void multiply_poly(poly **, poly *, poly *); | |
void write_to_txt(poly *); | |
int main(void) { | |
poly * poly1, * poly2, * poly3,* poly4; | |
printf("\n...Enter the first Polinomial...\n"); | |
create_poly(&poly1); | |
printf("First Polinomial: "); | |
show_poly(poly1); | |
printf("\n...Enter the second Polinomial...\n"); | |
create_poly(&poly2); | |
printf("Second Polinomial: "); | |
show_poly(poly2); | |
add_poly(&poly3, poly1, poly2); | |
printf(" \n\n Resultant polynomial after addition : "); | |
show_poly(poly3); | |
multiply_poly(&poly4, poly1, poly2); | |
printf(" \n\n Resultant polynomial after multiplication : "); | |
show_poly(poly4); | |
write_to_txt(poly3); | |
write_to_txt(poly4); | |
return 0; | |
} | |
void create_poly(poly ** node) { | |
int flag; //A flag to control the menu | |
int coeff, pow; | |
poly * tmp_node; //To hold the temporary last address | |
tmp_node = (poly *) malloc(sizeof(poly)); //create the first node | |
*node = tmp_node; //Store the head address to the reference variable | |
printf("{INFO}Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER\n"); | |
do { | |
printf("Enter COEFFICIENT =>"); | |
scanf("%d", &coeff); | |
tmp_node->coeff = coeff; | |
printf("Enter EXPONENT =>"); | |
scanf("%d", &pow); | |
tmp_node->pow = pow; | |
tmp_node->next = NULL; | |
//Ask user for continuation | |
printf("\nContinue adding more terms to the polynomial list?(Yes = 1/No = 0): "); | |
scanf("%d", &flag); | |
if(flag) { | |
tmp_node->next = (poly *) malloc(sizeof(poly)); //Grow the list | |
tmp_node = tmp_node->next; | |
tmp_node->next = NULL; | |
} | |
} while (flag); | |
} | |
void show_poly(poly * node) { | |
while(node != NULL) { | |
if(node->coeff != 0){ | |
printf("%dx^%d", node->coeff, node->pow); | |
} | |
node = node->next; | |
if(node != NULL) | |
printf(" + "); | |
} | |
printf("\n"); | |
} | |
void write_to_txt(poly * node) { | |
FILE *f; | |
f = fopen("output.txt", "a"); | |
while(node != NULL) { | |
if(node->coeff != 0){ | |
fprintf(f,"%dx^%d", node->coeff, node->pow); | |
} | |
node = node->next; | |
if(node != NULL) | |
fprintf(f," + "); | |
} | |
fprintf(f,"\n"); | |
fclose(f); | |
} | |
void add_poly(poly ** result, poly * poly1, poly * poly2) { | |
poly * tmp_node; //Temporary storage for the linked list | |
tmp_node = (poly *) malloc(sizeof(poly)); | |
tmp_node->next = NULL; | |
*result = tmp_node; //Copy the head address to the result linked list | |
//Loop while both of the linked lists have value | |
while(poly1 && poly2) { | |
if (poly1->pow > poly2->pow) { | |
tmp_node->pow = poly1->pow; | |
tmp_node->coeff = poly1->coeff; | |
poly1 = poly1->next; | |
} | |
else if (poly1->pow < poly2->pow) { | |
tmp_node->pow = poly2->pow; | |
tmp_node->coeff = poly2->coeff; | |
poly2 = poly2->next; | |
} | |
else { | |
tmp_node->pow = poly1->pow; | |
tmp_node->coeff = poly1->coeff + poly2->coeff; | |
poly1 = poly1->next; | |
poly2 = poly2->next; | |
} | |
//Grow the linked list on condition | |
if(poly1 && poly2) { | |
tmp_node->next = (poly *) malloc(sizeof(poly)); | |
tmp_node = tmp_node->next; | |
tmp_node->next = NULL; | |
} | |
} | |
while(poly1 || poly2) { | |
tmp_node->next = (poly *) malloc(sizeof(poly)); | |
tmp_node = tmp_node->next; | |
tmp_node->next = NULL; | |
if(poly1) { | |
tmp_node->pow = poly1->pow; | |
tmp_node->coeff = poly1->coeff; | |
poly1 = poly1->next; | |
} | |
if(poly2) { | |
tmp_node->pow = poly2->pow; | |
tmp_node->coeff = poly2->coeff; | |
poly2 = poly2->next; | |
} | |
} | |
} | |
void multiply_poly(poly **result, poly * poly1, poly * poly2) { | |
poly * tmp_node; //Temporary storage for the linked list | |
poly * temp1=poly1; | |
poly * temp2=poly2; | |
tmp_node = (poly *) malloc(sizeof(poly)); | |
tmp_node->next = NULL; | |
*result = tmp_node; //Copy the head address to the result linked list | |
if(!poly1 || !poly2) { | |
printf("0\n"); | |
}else{ | |
while(temp1 !=NULL){ | |
while(temp2 !=NULL){ | |
tmp_node->coeff = temp1->coeff * temp2->coeff; | |
tmp_node->pow = temp1->pow + temp2->pow; | |
temp2 = temp2->next; | |
tmp_node->next = (poly *) malloc(sizeof(poly)); | |
tmp_node = tmp_node->next; | |
tmp_node->next = NULL; | |
} | |
temp1 = temp1->next; | |
temp2=poly2; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Polinomial Addition and Multiplication using struct and linked list