Chinaunix首页 | 论坛 | 博客
  • 博客访问: 156996
  • 博文数量: 36
  • 博客积分: 802
  • 博客等级: 准尉
  • 技术积分: 717
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-02 22:47
文章分类
文章存档

2012年(36)

分类: C/C++

2012-08-24 10:02:49

这是一个链表的应用题目,写这个的时候加减运算很好写,乘法还是废了一定时间的,因为忘记修改,导致了一系列错误,最终终于成型了。就是期间q刚开始一直忘记给他赋值,因而只有LA第一个与LB的所有选项的乘积。

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define TRUE 1
  4. #define FALSE 0
  5. typedef struct node
  6. {
  7.     int exp;
  8.     int coef;
  9.     struct node *next;
  10. }Node,*LinkNode;

  11. void Init(LinkNode *L)
  12. {
  13.     *L=(LinkNode)malloc(sizeof(Node));
  14.     (*L)->next=NULL;
  15. }
  16. void meun()
  17. {
  18.     printf("输入您想要的算数运算\n");
  19.     printf("1:相加\n");
  20.     printf("2:相减\n");
  21.     printf("3:相乘\n");
  22. }
  23. void Polycreate(LinkNode L)
  24. {
  25.     Node *r,*s;
  26.     int c,e;
  27.     r=L;
  28.     printf("please input the coef and exp\n");
  29.     scanf("%d,%d",&c,&e);
  30.     while(c!=0)
  31.     {
  32.         s=(Node *)malloc(sizeof(Node));
  33.         s->coef=c;
  34.         s->exp=e;
  35.         r->next=s;
  36.         r=s;
  37.         scanf("%d,%d",&c,&e);
  38.     }
  39.     r->next=NULL;
  40. }
  41. void Polyadd(LinkNode la,LinkNode lb)
  42. {
  43.     Node *p,*q,*tail,*temp;
  44.     int sum;
  45.     p=la->next;
  46.     q=lb->next;
  47.     tail=la;
  48.     while(p!=NULL&&q!=NULL)
  49.     {
  50.         if(p->exp==q->exp)
  51.         {
  52.             sum=p->coef+q->coef;
  53.             if(sum!=0)
  54.             {
  55.                 p->coef=sum;
  56.                 tail->next=p;
  57.                 tail=p;
  58.                 p=p->next;
  59.                 temp=q;
  60.                 q=q->next;
  61.                 free(temp);
  62.             }
  63.             else
  64.             {
  65.                 temp=p;
  66.                 p=p->next;
  67.                 free(temp);
  68.                 temp=q;
  69.                 q=q->next;
  70.                 free(temp);
  71.             }
  72.         }
  73.         else if(p->exp<q->exp)
  74.         {
  75.             tail->next=p;
  76.             tail=p;
  77.             p=p->next;
  78.         }
  79.         else
  80.         {
  81.             tail->next=q;
  82.             tail=q;
  83.             q=q->next;
  84.         }
  85.     }
  86.     if(p!=NULL)
  87.         tail->next=p;
  88.     else
  89.         tail->next=q;

  90. }
  91. void Polysub(LinkNode la,LinkNode lb)
  92. {
  93.     Node *p,*q,*tail,*temp;
  94.     int sub;
  95.     p=la->next;
  96.     q=lb->next;
  97.     tail=la;
  98.     while(p!=NULL&&q!=NULL)
  99.     {
  100.         if(p->exp<q->exp)
  101.         {
  102.             tail->next=p;
  103.             tail=tail->next;
  104.             p=p->next;
  105.         }
  106.         else if(p->exp==q->exp)
  107.         {
  108.             if(p->coef<q->coef)
  109.                 sub=q->coef-p->coef;
  110.             else
  111.              sub=p->coef-q->coef;
  112.             if(sub)
  113.             {
  114.                 if(p->coef<q->coef)
  115.                     p->coef=-sub;
  116.                 else
  117.                     p->coef=sub;
  118.                 tail->next=p;
  119.                 tail=tail->next;
  120.                 p=p->next;
  121.                 temp=q;
  122.                 q=q->next;
  123.                 free(temp);
  124.             }
  125.             else
  126.             {
  127.                 temp=p;
  128.                 p=p->next;
  129.                 free(p);
  130.                 temp=q;
  131.                 q=q->next;
  132.                 free(q);
  133.             }
  134.         }
  135.         else
  136.         {
  137.             tail->next=q;
  138.             tail=q;
  139.             q=q->next;
  140.         }
  141.     }
  142.     if(p)
  143.         tail->next=p;
  144.     else
  145.         tail->next=q;

  146. }
  147. void Polytimes(LinkNode la,LinkNode lb)
  148. {
  149.     Node *p,*q,*tail,*temp,*s;
  150.     int times,niem;
  151.     p=la->next;
  152.     tail=la;
  153.     while(p!=NULL)
  154.     {
  155.         q=lb->next;
  156.         for(q;q!=NULL; )
  157.         {
  158.             s=(Node *)malloc(sizeof(Node));
  159.             times=(p->coef)*(q->coef);
  160.             niem=(p->exp)+(q->exp);
  161.             s->coef=times;
  162.             s->exp=niem;
  163.             times=0;
  164.             niem=0;
  165.             tail->next=s;
  166.             tail=s;
  167.         //    p=p->next;
  168.         //    temp=q;
  169.             q=q->next;
  170.         //    free(temp);
  171.         }
  172.         p=p->next;
  173.     }
  174. }
  175. void Print(LinkNode L)
  176. {
  177.     Node *p;
  178.     p=L->next;
  179.     while(p)
  180.     {
  181.         printf("%d %d ",p->coef,p->exp);
  182.         p=p->next;
  183.     }
  184.     printf("\n");
  185. }
  186. int main()
  187. {
  188.     LinkNode la,lb;
  189.     char c;
  190.     int a;
  191.     Init(&la);
  192.     Init(&lb);
  193.     printf("请输入第一个一元多项式:");
  194.     Polycreate(la);
  195.     printf("la的结果是:");
  196.     Print(la);
  197.     printf("请输入第二个一元多项式:");
  198.     Polycreate(lb);
  199.     printf("lb的结果是:");
  200.     Print(lb);
  201.     meun();
  202.     scanf("%d",&a);
  203.     switch(a)
  204.     {
  205.         case 1 :printf("请让他们相加!\n");
  206.              Polyadd(la,lb);
  207.      printf("相加后的结果是:");
  208.                 Print(la);
  209.                 printf("请按任意键返回!");
  210.                 break;
  211.         case 2: printf("请让他们相减!\n");
  212.                 Polysub(la,lb);
  213.                 printf("相减的结果是:");
  214.                 Print(la);
  215.                 break;
  216.         case 3:    printf("请让他们相乘:");
  217.                 Polytimes(la,lb);
  218.                 printf("相乘后的结果是:");
  219.                 Print(la);
  220.                 break;
  221.         default:
  222.                 printf("输入错误");
  223.                 break;
  224.     }

  225. }

阅读(4789) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~