Created
January 8, 2021 13:41
-
-
Save czynot/9a5ac020bef3d9ac933522ad51f8af60 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> | |
struct Node | |
{ | |
int coeff; | |
int pow; | |
struct Node* next; | |
}; | |
// Input one term at a time | |
// If exponent = 0 it means a purely numerical term | |
void readPolynomial(struct Node** poly) | |
{ | |
int coeff, exp, term; | |
struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); | |
*poly = temp; | |
do{ | |
printf("\n Enter coeffecient: "); | |
scanf("%d", &coeff); | |
printf("\n Enter exponent: "); | |
scanf("%d", &exp); | |
temp->coeff = coeff; | |
temp->pow = exp; | |
temp-> next = NULL; | |
printf("\n Enter 1 if you have more terms, else enter 0: "); | |
scanf("%d", &term); | |
if(term) | |
{ | |
temp->next = (struct Node*)malloc(sizeof(struct Node)); | |
temp = temp->next; | |
temp->next = NULL; | |
} | |
}while(term); | |
} | |
void displayPolynomial(struct Node* poly) | |
{ | |
printf("\n The polynomial expression is: "); | |
while(poly != NULL) | |
{ | |
printf("%dx^%d", poly->coeff, poly->pow); | |
poly = poly->next; | |
if(poly != NULL) | |
printf("+"); | |
} | |
} | |
// Not my original add implementation, inspired from and improved from a GFG C++ program | |
// Passing polynomials as parameters haha | |
void addPolynomials(struct Node* first, struct Node* second, struct Node** result) | |
{ | |
struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); | |
temp->next = NULL; | |
*result = temp; | |
while(first && second) | |
{ | |
if(first->pow > second->pow) | |
{ | |
temp->coeff = first->coeff; | |
temp->pow = first->pow; | |
first = first->next; | |
} | |
else if(first->pow < second->pow) | |
{ | |
temp->coeff = second->coeff; | |
temp->pow = second->pow; | |
second = second->next; | |
} | |
else | |
{ | |
temp->coeff = first->coeff + second->coeff; | |
temp->pow = first->pow; | |
first = first->next; | |
second = second->next; | |
} | |
if(first && second) | |
{ | |
temp->next = (struct Node*)malloc(sizeof(struct Node)); | |
temp = temp->next; | |
temp->next = NULL; | |
} | |
} | |
while(first || second) | |
{ | |
temp->next = (struct Node*)malloc(sizeof(struct Node)); | |
temp = temp->next; | |
temp->next = NULL; | |
if(first) | |
{ | |
temp->coeff = first->coeff; | |
temp->pow = first->pow; | |
first = first->next; | |
} | |
else if(second) | |
{ | |
temp->coeff = second->coeff; | |
temp->pow = second->pow; | |
second = second->next; | |
} | |
} | |
} | |
int main() | |
{ | |
struct Node* first = NULL; | |
struct Node* second = NULL; | |
struct Node* result = NULL; | |
printf("\nEnter the corresponding data:-\n"); | |
printf("\nFirst polynomial:\n"); | |
readPolynomial(&first); | |
displayPolynomial(first); | |
printf("\nSecond polynomial:\n"); | |
readPolynomial(&second); | |
displayPolynomial(second); | |
addPolynomials(first, second, &result); | |
displayPolynomial(result); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment