赞
踩
- #pragma warning(disable:4996)
-
- #include <stdio.h>
- #include <stdlib.h>
-
- typedef struct Node *List;
- struct Node
- {
- int coef;//系数
- int expon;//指数
- List next;
- };
-
- List CreateList(int n);
- List Mul(List list1,List list2);
- List Add(List list1,List list2);
- void Print(List L);
-
- int main()
- {
- List list1, list2,mul,add;
- int number1, number2;
- scanf("%d",&number1);
- list1 = CreateList(number1);
- scanf("%d",&number2);
- list2 = CreateList(number2);
-
- mul = Mul(list1,list2);
- add = Add(list1,list2);
-
- Print(mul);
- printf("\n");
- Print(add);
-
- return 0;
- }
-
- List CreateList(int n)
- {
- List L,t,cycle;
- L = (List)malloc(sizeof(struct Node));
- L->next = NULL;
- t = L;
- for (int i=0;i<n;i++)
- {
- List temp;
- temp = (List)malloc(sizeof(struct Node));
- scanf("%d",&temp->coef);
- scanf("%d",&temp->expon);
- t->next = temp;
- t = temp;
- temp->next = NULL;
- }
- t->next = NULL;
- cycle = L;
- L = L->next;
- free(cycle);
- return L;
- }
-
- void Print(List L)
- {
- if (!L)
- {
- printf("0 0");
- }
- else
- {
- printf("%d %d", L->coef, L->expon);
- L = L->next;
- }
- while (L)
- {
- printf(" %d %d",L->coef,L->expon);
- L = L->next;
- }
- }
-
- List Mul(List list1,List list2)
- {
- List L,t,l1,l2;
- int flag;
- L = (List)malloc(sizeof(struct Node));
- L->next = NULL;
- flag = 0;
- l1 = list1;
-
-
- while (l1)
- {
- List t2,t3,cycle;
- t2= (List)malloc(sizeof(struct Node));
- t2->next = NULL;
- t3 = t2;
- t = L;
- l2 = list2;
- while (l2)
- {
- List temp;
- temp = (List)malloc(sizeof(struct Node));
- temp->next = NULL;
- temp->coef = l1->coef * l2->coef;
- temp->expon = l1->expon + l2->expon;
- t3->next = temp;
- t3 = temp;
- l2 = l2->next;
- }
- cycle = t2;
- t2 = t2->next;
- free(cycle);
-
- if (flag == 0)
- {
-
- flag = 1;
- cycle = t;
- t = t ->next;
- free(cycle);
- }
- L = Add(t, t2);
- l1 = l1->next;
- }
-
- return L;
- }
-
- List Add(List list1, List list2)
- {
- List L, t, cycle,l1,l2;
- L = (List)malloc(sizeof(struct Node));
- L->next = NULL;
- t = L;
- l1 = list1;
- l2 = list2;
-
- while (l1 && l2)
- {
- if (l1->expon>l2->expon)
- {
- List temp;
- temp = (List)malloc(sizeof(struct Node));
- temp->expon = l1->expon;
- temp->coef = l1->coef;
- if (temp->coef!=0)
- {
- t->next = temp;
- t = temp;
- }
- l1 = l1->next;
- temp->next = NULL;
- }
- else if (l1->expon < l2->expon)
- {
- List temp;
- temp = (List)malloc(sizeof(struct Node));
- temp->expon = l2->expon;
- temp->coef = l2->coef;
- if (temp->coef != 0)
- {
- t->next = temp;
- t = temp;
- }
- l2 = l2->next;
- temp->next = NULL;
- }
- else
- {
- List temp;
- temp = (List)malloc(sizeof(struct Node));
- temp->expon = l1->expon;
- temp->coef = l1->coef+l2->coef;
- if (temp->coef != 0)
- {
- t->next = temp;
- t = temp;
- }
- l1 = l1->next;
- l2 = l2->next;
- temp->next = NULL;
- }
- }
-
- while (l1)
- {
- List temp;
- temp = (List)malloc(sizeof(struct Node));
- temp->expon = l1->expon;
- temp->coef = l1->coef;
- if (temp->coef != 0)
- {
- t->next = temp;
- t = temp;
- }
- l1 = l1->next;
- temp->next = NULL;
- }
- while (l2)
- {
- List temp;
- temp = (List)malloc(sizeof(struct Node));
- temp->expon = l2->expon;
- temp->coef = l2->coef;
- if (temp->coef != 0)
- {
- t->next = temp;
- t = temp;
- }
- l2 = l2->next;
- temp->next=NULL;
- }
- cycle = L;
- L = L->next;
- free(cycle);
- return L;
- }

题源中国mooc
p.s:这个练习里变量还挺多的,大家多加注意。(来自一个debug,de了几个小时,最后发现是变量的区分后缀打错了人的自我感言。)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。