- #include <stdio.h>
- #include <stdlib.h>
- typedef struct poly_node *poly_pointer;
- typedef struct poly_node {
- int coef;
- int expon;
- poly_pointer link;
- } poly_node;
- void attach_poly(int coefficient, int exponent, poly_pointer *ptr)
- {
- /* create a new node with coef = coefficient, poly_pointer and
- * expon = exponent, attach it to the node pointer to by ptr.
- * ptr is update to pointer to this new node
- */
- poly_pointer temp;
- temp = (poly_pointer)malloc(sizeof(poly_node));
- temp->coef = coefficient;
- temp->expon = exponent;
- (*ptr)->link = temp;
- *ptr = temp;
- }
- poly_pointer poly_add(poly_pointer poly_aug, poly_pointer poly_add)
- {
- /* return a polynominal which is the sum of poly_aug and poly_add */
- poly_pointer front, rear, temp;
- int sum;
- rear = (poly_pointer)malloc(sizeof(poly_node));
- front = rear;
- while (poly_aug && poly_add) {
- if (poly_aug->expon < poly_add->expon) {
- attach_poly(poly_add->coef, poly_add->expon, &rear);
- poly_add = poly_add->link;
- }
- else {
- if (poly_aug->expon == poly_add->expon) {
- sum = poly_aug->coef + poly_add->coef;
- if (sum)
- attach_poly(sum, poly_aug->expon, &rear);
- poly_add = poly_add->link;
- poly_aug = poly_aug->link;
- }
- else {
- attach_poly(poly_aug->coef, poly_aug->expon, &rear);
- poly_aug = poly_aug->link;
- }
- }
- }//while
- /* copy rest of list poly_aug and then list poly_add */
- for ( ; poly_aug; poly_aug = poly_aug->link)
- attach_poly(poly_aug->coef, poly_aug->expon, &rear);
- for ( ; poly_add; poly_add = poly_add->link)
- attach_poly(poly_add->coef, poly_add->expon, &rear);
- rear->link = NULL;
- /* delete extra initial node */
- temp = front;
- front = front->link;
- free(temp);
- return front;
- }
- poly_pointer poly_sub(poly_pointer poly_minu, poly_pointer poly_sub)
- {
- /* return a polynominal which is the sub of poly_minu and poly_sub */
- poly_pointer front, rear, temp;
- int diff;
- rear = (poly_pointer)malloc(sizeof(poly_node));
- front = rear;
- while (poly_minu && poly_sub) {
- if (poly_minu->expon < poly_sub->expon) {
- attach_poly(-poly_sub->coef, poly_sub->expon, &rear);
- poly_sub = poly_sub->link;
- }
- else {
- if (poly_minu->expon == poly_sub->expon) {
- diff = poly_minu->coef - poly_sub->coef;
- if (diff)
- attach_poly(diff, poly_minu->expon, &rear);
- poly_sub = poly_sub->link;
- poly_minu = poly_minu->link;
- }
- else {
- attach_poly(poly_minu->coef, poly_minu->expon, &rear);
- poly_minu = poly_minu->link;
- }
- }
- }//while
- /* copy rest of list poly_minu and then list poly_sub */
- for ( ; poly_minu; poly_minu = poly_minu->link)
- attach_poly(poly_minu->coef, poly_minu->expon, &rear);
- for ( ; poly_sub; poly_sub = poly_sub->link)
- attach_poly(-poly_sub->coef, poly_sub->expon, &rear);
- rear->link = NULL;
- /* delete extra initial node */
- //temp = front;
- //front = front->link;
- //free(temp);
- return front;
- }
- poly_pointer create_poly(int poly_size)
- {
- int i;
- int coef;
- int expon;
- poly_pointer p, front, rear;
- rear = (poly_pointer)malloc(sizeof(poly_node));
- front = rear;
- for (i = 1; i <= poly_size; ++i) { // Generator polynomial
- p = (poly_pointer)malloc(sizeof(poly_node));
- printf("Please enter one yuan polynomial coefficients and exponents: ");
- scanf("%d %d", &coef, &expon);
- p->coef = coef;
- p->expon = expon;
- rear->link = p;
- rear = p;
- p->link = NULL;
- }
- return front;
- }
- void print_poly(poly_pointer p)
- {
- p = p->link;
- if ( p ) {
- printf("%dX^%d", p->coef, p->expon);
- p = p->link;
- while ( p ) {
- if (p->coef > 0)
- printf("+%dX^%d", p->coef, p->expon);
- else
- printf("%dX^%d", p->coef, p->expon);
- p = p->link;
- }
- }
- else
- printf("0");
- printf("\\n");
- }
- int main()
- {
- int poly_a_number, poly_b_number;
- poly_pointer poly_a, poly_b, poly_sum, poly_diff;
- printf("Please enter a polynomial number of items poly_a: ");
- scanf("%d", &poly_a_number);
- poly_a = create_poly(poly_a_number);
- printf("The polynomial a is: ");
- print_poly(poly_a);
- printf("\\nPlease enter a polynomial number of items poly_b: ");
- scanf("%d", &poly_b_number);
- poly_b = create_poly(poly_b_number);
- printf("The polynomial b is: ");
- print_poly(poly_b);
- printf("\\nThe result is:\\n");
- poly_sum = poly_add(poly_a, poly_b);
- printf("poly_a + poly_b = ");
- print_poly(poly_sum);
- poly_diff = poly_sub(poly_a, poly_b);
- printf("poly_a - poly_d = ");
- print_poly(poly_diff);
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/100320148936.html
来源: http://www.codesnippet.cn/detail/100320148936.html