Untitled

 avatar
irfann
plain_text
2 years ago
4.5 kB
4
Indexable
#include <stdio.h>
#include <stdlib.h>
struct Term {
 int coefficient;
 int exponent;
 struct Term* next;
};
// Function to create a new term
struct Term* createTerm(int coeff, int exp) {
 struct Term* newTerm = (struct Term*)malloc(sizeof(struct Term));
 newTerm->coefficient = coeff;
 newTerm->exponent = exp;
 newTerm->next = NULL;
 return newTerm;
}
// Function to insert a term into the circular linked list
void insertTerm(struct Term** head, int coeff, int exp) {
 struct Term* newTerm = createTerm(coeff, exp);
 if (*head == NULL) {
 *head = newTerm;
 (*head)->next = *head;
 } else {
 struct Term* last = *head;
 while (last->next != *head) {
 last = last->next;
 }
 last->next = newTerm;
 newTerm->next = *head;
 }
}
// Function to display the polynomial
void displayPolynomial(struct Term* head) {
 if (head == NULL) {
 printf("Polynomial is empty.\n");
 return;
 }
 struct Term* current = head;
 do {
 printf("%dx^%d ", current->coefficient, current->exponent);
 current = current->next;
 if (current != head) {
 printf("+ ");
 }
 } while (current != head);
 printf("\n");
}
// Function to add two polynomials and return a new polynomial
struct Term* addPolynomials(struct Term* poly1, struct Term* poly2) {
 struct Term* result = NULL;
 struct Term* ptr1 = poly1;
 struct Term* ptr2 = poly2;
 do {
 if (ptr1->exponent > ptr2->exponent) {
 insertTerm(&result, ptr1->coefficient, ptr1->exponent);
 ptr1 = ptr1->next;
 } else if (ptr1->exponent < ptr2->exponent) {
 insertTerm(&result, ptr2->coefficient, ptr2->exponent);
 ptr2 = ptr2->next;
 } else {
 int sumCoeff = ptr1->coefficient + ptr2->coefficient;
 if (sumCoeff != 0) {
 insertTerm(&result, sumCoeff, ptr1->exponent);
 }
 ptr1 = ptr1->next;
 ptr2 = ptr2->next;
 }
 } while (ptr1 != poly1 && ptr2 != poly2);
 while (ptr1 != poly1) {
 insertTerm(&result, ptr1->coefficient, ptr1->exponent);
 ptr1 = ptr1->next;
 }
 while (ptr2 != poly2) {
 insertTerm(&result, ptr2->coefficient, ptr2->exponent);
 ptr2 = ptr2->next;
 }
 return result;
}
// Function to multiply two polynomials and return a new polynomial
struct Term* multiplyPolynomials(struct Term* poly1, struct Term* poly2) {
 struct Term* result = NULL;
 struct Term* ptr1 = poly1;
 do {
 struct Term* ptr2 = poly2;
 do {
 int coeff = ptr1->coefficient * ptr2->coefficient;
 int exp = ptr1->exponent + ptr2->exponent;
 insertTerm(&result, coeff, exp);
 ptr2 = ptr2->next;
 } while (ptr2 != poly2);
 ptr1 = ptr1->next;
 } while (ptr1 != poly1);
 return result;
}
// Function to evaluate the polynomial for a given value of 'x'
int evaluatePolynomial(struct Term* head, int x) {
 int result = 0;
 struct Term* current = head;
 do {
 result += current->coefficient * pow(x, current->exponent);
 current = current->next;
 } while (current != head);
 return result;
}
int main() {
 struct Term* poly1 = NULL;
 struct Term* poly2 = NULL;
 int choice, coeff, exp, x;
 while (1) {
 printf("\nMenu:\n");
 printf("1. Insert a term into Polynomial 1\n");
 printf("2. Insert a term into Polynomial 2\n");
 printf("3. Display Polynomial 1\n");
 printf("4. Display Polynomial 2\n");
 printf("5. Add Polynomials\n");
 printf("6. Multiply Polynomials\n");
 printf("7. Evaluate Polynomial\n");
 printf("8. Exit\n");
 printf("Enter your choice: ");
 scanf("%d", &choice);
 switch (choice) {
 case 1:
 printf("Enter the coefficient and exponent of the term: ");
 scanf("%d%d", &coeff, &exp);
 insertTerm(&poly1, coeff, exp);
 break;
 case 2:
 printf("Enter the coefficient and exponent of the term: ");
 scanf("%d%d", &coeff, &exp);
 insertTerm(&poly2, coeff, exp);
 break;
 case 3:
 printf("Polynomial 1: ");
 displayPolynomial(poly1);
 break;
 case 4:
 printf("Polynomial 2: ");
 displayPolynomial(poly2);
 break;
 case 5: {
 struct Term* result = addPolynomials(poly1, poly2);
 printf("Result of addition: ");
 displayPolynomial(result);
 break;
 }
 case 6: {
 struct Term* result = multiplyPolynomials(poly1, poly2);
 printf("Result of multiplication: ");
 displayPolynomial(result);
 break;
 }
 case 7:
 printf("Enter the value of x: ");
 scanf("%d", &x);
 printf("Value of Polynomial 1 at x = %d: %d\n", x, evaluatePolynomial(poly1, x));
 printf("Value of Polynomial 2 at x = %d: %d\n", x, evaluatePolynomial(poly2, x));
 break;
 case 8:
 exit(0);
 default:
 printf("Invalid choice! Try again.\n");
 }
 }
 return 0;
}
Editor is loading...
Leave a Comment