You are on page 1of 6

#include <stdio.

h>

#include <stdlib.h>

typedef struct Term {

float coefficient;

int exponent;

struct Term* next;

} Term;

void addTerm(Term** poly, float coeff, int exp) {

Term* newTerm = (Term*)malloc(sizeof(Term));

newTerm->coefficient = coeff;

newTerm->exponent = exp;

newTerm->next = NULL;

if (*poly == NULL) {

*poly = newTerm;

} else {

Term* temp = *poly;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newTerm;

void displayPolynomial(Term* poly) {

Term* temp = poly;

while (temp != NULL) {

printf("%.2fx^%d", temp->coefficient, temp->exponent);

temp = temp->next;

if (temp != NULL) {

printf(" + ");

}
}

printf("\n");

Term* addPolynomials(Term* poly1, Term* poly2) {

Term* result = NULL;

while (poly1 != NULL && poly2 != NULL) {

if (poly1->exponent > poly2->exponent) {

addTerm(&result, poly1->coefficient, poly1->exponent);

poly1 = poly1->next;

} else if (poly1->exponent < poly2->exponent) {

addTerm(&result, poly2->coefficient, poly2->exponent);

poly2 = poly2->next;

} else {

addTerm(&result, poly1->coefficient + poly2->coefficient, poly1->exponent);

poly1 = poly1->next;

poly2 = poly2->next;

while (poly1 != NULL) {

addTerm(&result, poly1->coefficient, poly1->exponent);

poly1 = poly1->next;

while (poly2 != NULL) {

addTerm(&result, poly2->coefficient, poly2->exponent);

poly2 = poly2->next;

return result;

Term* subtractPolynomials(Term* poly1, Term* poly2) {

Term* result = NULL;


while (poly1 != NULL && poly2 != NULL) {

if (poly1->exponent > poly2->exponent) {

addTerm(&result, poly1->coefficient, poly1->exponent);

poly1 = poly1->next;

} else if (poly1->exponent < poly2->exponent) {

addTerm(&result, -poly2->coefficient, poly2->exponent);

poly2 = poly2->next;

} else {

addTerm(&result, poly1->coefficient - poly2->coefficient, poly1->exponent);

poly1 = poly1->next;

poly2 = poly2->next;

while (poly1 != NULL) {

addTerm(&result, poly1->coefficient, poly1->exponent);

poly1 = poly1->next;

while (poly2 != NULL) {

addTerm(&result, -poly2->coefficient, poly2->exponent);

poly2 = poly2->next;

return result;

Term* multiplyPolynomials(Term* poly1, Term* poly2) {

Term* result = NULL;

Term* temp1 = poly1;

while (temp1 != NULL) {

Term* temp2 = poly2;

while (temp2 != NULL) {

addTerm(&result, temp1->coefficient * temp2->coefficient, temp1->exponent +


temp2->exponent);

temp2 = temp2->next;

temp1 = temp1->next;

return result;

Term* derivePolynomial(Term* poly) {

Term* result = NULL;

Term* temp = poly;

while (temp != NULL) {

if (temp->exponent != 0) {

addTerm(&result, temp->coefficient * temp->exponent, temp->exponent - 1);

temp = temp->next;

return result;

10

void freePolynomial(Term* poly) {

Term* temp;

while (poly != NULL) {

temp = poly;

poly = poly->next;

free(temp);

Term* inputPolynomial() {

Term* poly = NULL;

int numTerms,i;

printf("Enter the number of terms in the polynomial: ");


scanf("%d", &numTerms);

for (i = 0; i < numTerms; ++i) {

float coeff;

int exp;

printf("Enter coefficient and exponent for term %d: ", i + 1);

scanf("%f %d", &coeff, &exp);

addTerm(&poly, coeff, exp);

return poly;

int main() {

Term* poly1 = NULL;

Term* poly2 = NULL;

int choice;

11

printf("Choose the operation:\n");

printf("1. Addition\n");

printf("2. Subtraction\n");

printf("3. Multiplication\n");

printf("4. Derivation\n");

printf("Enter your choice: ");

scanf("%d", &choice);

printf("Enter polynomial 1:\n");

poly1 = inputPolynomial();

if (choice != 4) {

printf("Enter polynomial 2:\n");

poly2 = inputPolynomial();

switch (choice) {

case 1:

printf("Addition Result: ");


displayPolynomial(addPolynomials(poly1, poly2));

break;

case 2:

printf("Subtraction Result: ");

displayPolynomial(subtractPolynomials(poly1, poly2));

break;

case 3:

printf("Multiplication Result: ");

displayPolynomial(multiplyPolynomials(poly1, poly2));

break;

case 4:

12

printf("Derivation Result: ");

displayPolynomial(derivePolynomial(poly1));

break;

default:

printf("Invalid choice\n");

freePolynomial(poly1);

freePolynomial(poly2);

return 0;

You might also like