Untitled

 avatar
unknown
plain_text
12 days ago
3.3 kB
4
Indexable
#include <stdio.h>
#include <stdlib.h>

typedef struct Term {
    int coeff; // Coefficient
    int x_exp; // Exponent of x
    int y_exp; // Exponent of y
    struct Term* next; // Pointer to the next term
} Term;

// Function to create a new term
Term* createTerm(int coeff, int x_exp, int y_exp) {
    Term* newTerm = (Term*)malloc(sizeof(Term));
    newTerm->coeff = coeff;
    newTerm->x_exp = x_exp;
    newTerm->y_exp = y_exp;
    newTerm->next = NULL;
    return newTerm;
}

// Function to read a polynomial
Term* readPolynomial() {
    Term* head = NULL;
    Term* tail = NULL;
    int coeff, x_exp, y_exp;

    printf("Enter terms of the polynomial (coeff x_exp y_exp), end with -1 -1 -1:\n");
    while (1) {
        scanf("%d %d %d", &coeff, &x_exp, &y_exp);
        if (coeff == -1 && x_exp == -1 && y_exp == -1) {
            break;
        }
        Term* newTerm = createTerm(coeff, x_exp, y_exp);
        if (head == NULL) {
            head = newTerm;
            tail = newTerm;
        } else {
            tail->next = newTerm;
            tail = newTerm;
        }
    }
    return head;
}

// Function to add two polynomials
Term* addPolynomials(Term* poly1, Term* poly2) {
    Term* result = NULL;
    Term* tail = NULL;

    while (poly1 != NULL || poly2 != NULL) {
        Term* newTerm = NULL;

        if (poly1 == NULL) {
            newTerm = createTerm(poly2->coeff, poly2->x_exp, poly2->y_exp);
            poly2 = poly2->next;
        } else if (poly2 == NULL) {
            newTerm = createTerm(poly1->coeff, poly1->x_exp, poly1->y_exp);
            poly1 = poly1->next;
        } else if (poly1->x_exp == poly2->x_exp && poly1->y_exp == poly2->y_exp) {
            newTerm = createTerm(poly1->coeff + poly2->coeff, poly1->x_exp, poly1->y_exp);
            poly1 = poly1->next;
            poly2 = poly2->next;
        } else if (poly1->x_exp > poly2->x_exp || (poly1->x_exp == poly2->x_exp && poly1->y_exp > poly2->y_exp)) {
            newTerm = createTerm(poly1->coeff, poly1->x_exp, poly1->y_exp);
            poly1 = poly1->next;
        } else {
            newTerm = createTerm(poly2->coeff, poly2->x_exp, poly2->y_exp);
            poly2 = poly2->next;
        }

        if (result == NULL) {
            result = newTerm;
            tail = newTerm;
        } else {
            tail->next = newTerm;
            tail = newTerm;
        }
    }
    return result;
}

// Function to print a polynomial
void printPolynomial(Term* poly) {
    if (poly == NULL) {
        printf("0\n");
        return;
    }

    while (poly != NULL) {
        printf("%d x^%d y^%d", poly->coeff, poly->x_exp, poly->y_exp);
        poly = poly->next;
        if (poly != NULL) {
            printf(" + ");
        }
    }
    printf("\n");
}

// Main function
int main() {
    printf("Read first polynomial:\n");
    Term* poly1 = readPolynomial();
    
    printf("Read second polynomial:\n");
    Term* poly2 = readPolynomial();
    
    Term* result = addPolynomials(poly1, poly2);
    
    printf("Resultant polynomial:\n");
    printPolynomial(result);
    
    // Free memory (not shown for brevity)
    // You should implement a function to free the linked list memory

    return 0;
}
Editor is loading...
Leave a Comment