一元多项式的存储合并

来源:互联网 发布:优化储存空间照片恢复 编辑:程序博客网 时间:2024/06/06 00:19
#include<stdio.h>#include<malloc.h>#define MAX 20//多项式的最多项数typedef struct POLY//定义存放多项式的系数和指数{    double coef;//系数    int exp;//指数}polyarray[MAX];typedef struct pnode{    double coef;    int exp;    struct pnode *next;}polynode;//定义单链表节点类型。void creatpolylist(polynode **poly,polyarray a,int num);void destorylist(polynode **poly);void displist(polynode *poly);void sortlist(polynode *poly);void add(polynode *poly1,polynode *poly2,polynode **poly3);void creatpolylist(polynode **poly,polyarray a,int num)//尾插法建表{    polynode *p,*res;    int i=0;    (*poly) = malloc(sizeof(polynode));    p = *poly;          while(i<num)    {        res = malloc(sizeof(polynode));        res->coef = a[i].coef;        res->exp = a[i].exp;        p->next = res;        p = res;        i++;    }    p->next = NULL;     }void destorylist(polynode **poly){    polynode *p = *poly,*q ;    while(p)    {        q = p->next;        free(p);        p = q;          }    *poly = NULL;}void displist(polynode *poly){    polynode *res = poly->next;    int flag = 1;    while(res)    {        if(res->coef == 0)        {            res = res->next;            continue;        }        if(flag != 1&&res->coef > 0)        printf("+");        if(res->exp == 0 )            printf("%lf",res->coef);        else if(res->exp == 1)            printf("%lfX",res->coef);              else                  printf("%lfX^%d",res->coef,res->exp);    res = res->next;    flag++;    }}void sortlist(polynode *poly)//构造只有一个元素的有序表,从而扩大即可。{    //按照指数从大从小的顺序排序;    polynode *p = poly->next,*h;//h记录res,要准备插的值;    polynode *s = poly;//遍历有序表。    polynode *res = p->next;    p->next = NULL;    while(res)    {        s = poly;        while(s->next&&s->next->exp > res->exp)//找res适合位置的前驱节点。        {            s = s->next;        }        h = res->next;        res->next = s->next;        s->next = res;        res = h;    }}void add(polynode *poly1,polynode *poly2,polynode **poly3){    polynode *p1 = poly1->next;    polynode *p2 = poly2->next;    polynode *res,*h;//res指向尾节点。    (*poly3) = malloc(sizeof(polynode));    res = (*poly3);    while(p1&&p2)    {        if(p1->exp > p2->exp)        {            h = malloc(sizeof(polynode));            h->coef = p1->coef;            h->exp = p1->exp;            res->next = h;            res = h;            p1 = p1->next;        }        else if(p1->exp < p2->exp)        {            h = malloc(sizeof(polynode));            h->coef = p2->coef;            h->exp = p2->exp;            res->next = h;            res = h;                p2 = p2->next;        }            else        {            h = malloc(sizeof(polynode));            h->coef = p1->coef+p2->coef;            h->exp = p1->exp;            res->next = h;            res = h;            p1 = p1->next;            p2 = p2->next;        }    }      if(p1)        p2 = p1;      while(p2)      {            h = malloc(sizeof(polynode));            h->coef = p2->coef;            h->exp = p2->exp;            res->next = h;            res = h;                p2 = p2->next;      }        res->next = NULL;}int main(void){    polyarray array1={{1.2,0},{2.5,1},{3.2,3},{-2.5,5}};    polyarray array2={{-1.2,0},{2.5,1},{3.2,3},{2.5,5},{5.4,10}};    polynode *poly1 = NULL;    polynode *poly2 = NULL;    polynode *poly3 = NULL;    creatpolylist(&poly1,array1,4);    creatpolylist(&poly2,array2,5);    printf("多项式一排序前:\n");    displist(poly1);    printf("\n多项式二排序前:\n");    displist(poly2);    printf("\n");    sortlist(poly1);    sortlist(poly2);    printf("多项式一排序后:\n");    displist(poly1);    printf("\n多项式二排序后:\n");    displist(poly2);    add(poly1,poly2,&poly3);    printf("\n合并多项式\n");    displist(poly3);    printf("\n");    destorylist(&poly3);    destorylist(&poly1);    destorylist(&poly2);}
0 0
原创粉丝点击