⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c2_list.cpp

📁 这个是严蔚敏版的数据结构上机教程中的部分源代码
💻 CPP
字号:
/*
一元多项式的加减法
BY:wangyucao
*/
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -1
//#define DEBUG

typedef struct Lnode{
       int zs;
       int xs;
       Lnode* next;
       }*Linklist;

int CreateList(Linklist &L)
{
     int m;
     Lnode *p;
     L=(Lnode*)malloc(sizeof(Lnode));
     L->next=NULL;
     printf("多项式项数:");
     scanf("%d",&m);
     printf("分别输入每个项的系数和指数:\n");
     if(m<1) return ERROR;
     p=(Lnode *)malloc(sizeof(Lnode));        //插入第一项 
     if(!p) return OVERFLOW;
     p->next=NULL;
     L->next=p;
     scanf("%d%d",&p->xs,&p->zs);
     m--;
     while(m>0)                              //表头插入其余项 
     {
         p=(Lnode *)malloc(sizeof(Lnode));
         if(!p) return OVERFLOW;
         p->next=L->next;
         L->next=p;
         scanf("%d%d",&p->xs,&p->zs);
         m--;
     }
     //  # ifdef DEBUG: printf("create OK");
     return OK;
}

int ListPlus(Linklist &L1,Linklist &L2,Linklist &L3)
{
    Lnode *p,*q,*r,*d;
    p=L1;
    q=L2;
	L3=(Lnode*)malloc(sizeof(Lnode));
    r=L3;
    do
    {
        if(p->next->zs == q->next->zs)              //指数相等 
        {
            p->next->xs+=q->next->xs;                         
            if(p->next->xs)                             //系数相加 不为零 
            {
                r->next=p->next;
                p->next=p->next->next;
                r=r->next;
                d=q->next;
                q->next=q->next->next;
                free(d);
            }
            else                                  //系数相加为零 
            {d=p->next;p->next=p->next->next;free(d);
            d=q->next;q->next=q->next->next;free(d);
            }
        }
        else if(p->next->zs < q->next->zs)
        {
            r->next=p->next;
            p->next=p->next->next;
            r=r->next;
        }
        else 
        {
            r->next=q->next;
            q->next=q->next->next;
            r=r->next;
        }
    }while(p->next && q->next);
    if(p->next) {r->next=p->next;  L1->next=NULL;}
    else {r->next=q->next;  L2->next=NULL;}
    return OK;
}

int ListDec(Linklist &L1,Linklist &L2,Linklist &L3)
{
    Lnode *p,*q,*r,*d;
    p=L1;
    q=L2;
	L3=(Lnode*)malloc(sizeof(Lnode));
    r=L3;
    do
    {
        if(p->next->zs == q->next->zs)              //指数相等 
        {
            p->next->xs-=q->next->xs;                         
            if(p->next->xs)                             //系数相减 不为零 
            {
                r->next=p->next;
                p->next=p->next->next;
                r=r->next;
                d=q->next;
                q->next=q->next->next;
                free(d);
            }
            else                                  //系数相减为零 
            {d=p->next;p->next=p->next->next;free(d);
             d=q->next;q->next=q->next->next;free(d);
            }
        }
        else if(p->next->zs < q->next->zs)
        {
            r->next=p->next;
            p->next=p->next->next;
            r=r->next;
        }
        else 
        {
            q->next->xs*=-1; 
            r->next=q->next;
            q->next=q->next->next;
            r=r->next;
        }
    }while(p->next && q->next);
    if(p->next) {r->next=p->next;  L1->next=NULL;}
    else {
         r->next=q->next;
         while(q->next!=NULL)
         {
             q->next->xs*=-1;
             q->next=q->next->next;
         } 
         L2->next=NULL;}
    return OK;
}

int PrintList(Linklist &L)
{
    Lnode* p=L->next;
    if(p->zs>0)
    { 
     if(p->xs > 1) printf("%dX",p->xs);
     else if(p->xs == 1) printf("X",p->xs);
     else if(p->xs <0) printf("%dX",p->xs);
     if(p->zs>1) printf("^%d",p->zs);
     p=p->next;
    }
    else
    {
        if(p->xs > 1) printf("%d",p->xs);
        else if(p->xs == 1) printf("%d",p->xs);
        else if(p->xs <0) printf(" %d",p->xs);
        p=p->next;
    }
    while(p != NULL)
    {
 
       if(p->zs>0)
       { 
        if(p->xs > 1) printf(" + %dX",p->xs);
        else if(p->xs == 1) printf("X",p->xs);
        else if(p->xs <0) printf(" %dX",p->xs);
        if(p->zs>1) printf("^%d",p->zs);
        p=p->next;
        }
        else
        {
            if(p->xs > 1) printf(" + %d",p->xs);
            else if(p->xs == 1) printf("%d",p->xs);
            else if(p->xs <0) printf("%d",p->xs);
            p=p->next;
        }
    }
    //if(p->zs == 0) printf("%d",p->xs);
    //else printf("%dX^%d",p->xs,p->zs);
    return OK;
}
int main()
{
    Linklist L1,L2,L3;
    int op;
    CreateList(L1);
    if(!L1) exit(ERROR);
    CreateList(L2);
    if(!L2) exit(ERROR);
    printf("请选择加法或减法运算:\n1.加法\n2.减法\n");
    scanf("%d",&op);
    if(op == 1) {
          PrintList(L1);
      //    ifdef DEBUG:printf("P1OK"); 
          getchar();
          printf("\n+\n");
          PrintList(L2);
          printf("\n=\n");
          ListPlus(L1,L2,L3);
          PrintList(L3);
          }
    else{
          PrintList(L1);
   //       ifdef DEBUG:printf("P2OK"); 
          getchar();
          printf("\n-\n");
          PrintList(L2);
          printf("\n=\n");
          ListDec(L1,L2,L3);
          PrintList(L3);
          }
    getchar();
    return OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -