| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1454 人关注过本帖
标题:数据结构 求补充完整一元多项式减法的运算
只看楼主 加入收藏
cainiao一枚
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-5-29
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
数据结构 求补充完整一元多项式减法的运算
#include <stdio.h>
#include <malloc.h>

typedef struct
{
    int cff;//系数
    int exp;//指数
} datatype;

typedef struct Node
{
    datatype data_1;
    struct Node *next;
}listnode;//自定义struct数据类型为linklist,而不是定义linklist,所以不能在这里定义pa,pb

typedef listnode* linklist;

linklist create_list()//创建带头结点的链表,返回头结点,尾插法
{   
    linklist head=(linklist)malloc(sizeof(listnode));
    linklist  p, r;
    int xs;
    int zs;
    r=head;
    while(scanf("%d%d",&xs, &zs)
        && xs != 0
        || zs != 0)//输入00是结束
    {
        p=(linklist)malloc(sizeof(listnode));
        p->data_1.cff = xs;
        p->data_1.exp = zs;
        r->next = p;
        r = p;   
    }
    r->next = NULL;
    return (head);
}

void Select_sort(linklist l)//选择排序,由高到低
{
    linklist p, r, q;
    r = l->next;
    p = (linklist)malloc(sizeof(listnode));
    while( r != NULL)
    {  
        q = r->next;
        while( q != NULL)
        {
            if( q->data_1.exp > r->data_1.exp )
            {
                p->data_1.cff = r->data_1.cff;
                p->data_1.exp = r->data_1.exp;
                r->data_1.cff = q->data_1.cff;
                r->data_1.exp = q->data_1.exp;
                q->data_1.cff = p->data_1.cff;
                q->data_1.exp = p->data_1.exp;
            }
            q = q->next;
        }
        r = r->next;
    }
}

void merge_list(linklist l)//合并同类项
{
    linklist r, q, p;
    r = l;
    while( r != NULL && r->next != NULL)
    {  
        q = r->next;
        while( q->next != NULL && q != NULL
            && (q->data_1.exp == q->next->data_1.exp))
        {
            p = q->next;
            q->data_1.cff += p->data_1.cff;
            q->next = p->next;
            free(p);
        }
        r = r->next;
    }

    //清楚系数为0的项
    r = l;
    while(r != NULL && r->next != NULL)
    {
        if( r->next->data_1.cff == 0)
        {
            q = r->next;
            r->next = q->next;
            free(q);
        }
        r = r->next;
    }
}

linklist add_list(linklist l_fir, linklist l_sec)//表一表二相加并将结果存放在表三中
{
    linklist head=(linklist)malloc(sizeof(listnode));
    linklist p, q, r, s;
    r = head;
    p = l_fir->next;
    q = l_sec->next;
    while( p != NULL && q != NULL )//表一表二中均有数据
    {
        s = (linklist)malloc(sizeof(listnode));
        if(p->data_1.exp == q->data_1.exp)
        {
            
            s->data_1.cff = p->data_1.cff + q->data_1.cff;
            s->data_1.exp = p->data_1.exp;
            p = p->next;
            q = q->next;
        }
        else if(p->data_1.exp > q->data_1.exp)
        {  
            s->data_1.cff = p->data_1.cff;
            s->data_1.exp = p->data_1.exp;
            p = p->next;
        }
        else if(p->data_1.exp < q->data_1.exp)
        {
            s->data_1.cff = q->data_1.cff;
            s->data_1.exp = q->data_1.exp;
            q = q->next;
        }
        r->next = s;
        r = s;
    }
    while( p != NULL)//仅表一中有数据
    {
        s = (linklist)malloc(sizeof(listnode));
        s->data_1.cff = p->data_1.cff;
        s->data_1.exp = p->data_1.exp;
        p = p->next;
        r->next = s;
        r =s;
    }
    while( q != NULL)//仅表二中有数据
    {
        s = (linklist)malloc(sizeof(listnode));
        s->data_1.cff = q->data_1.cff;
        s->data_1.exp = q->data_1.exp;
        q = q->next;
        r->next = s;
        r =s;
    }
    r->next = NULL;

    //清楚系数为0的项
    r = head;
    while(r != NULL && r->next != NULL)
    {
        if( r->next->data_1.cff == 0)
        {
            q = r->next;
            r->next = q->next;
            free(q);
        }
        r = r->next;
    }
    return (head);
}

void result_out(linklist p)//输出结果
{
    linklist k;
    k = p;
    if(p->next == NULL)
        printf("empty list\n");
    while( p->next != NULL)
    {  
        if( p->next->data_1.cff < 0)
        {
            if(p == k)
                printf("-");
            else
                printf("- ");
            printf("%d^%d ", -p->next->data_1.cff, p->next->data_1.exp);
        }
        else if(p != k && p->next->data_1.cff > 0)
        {   
            printf("+ ");
            printf("%d^%d ", p->next->data_1.cff, p->next->data_1.exp);
        }
        else if(p == k && p->next->data_1.cff > 0)
        {
            printf("%d^%d ", p->next->data_1.cff, p->next->data_1.exp);
        }
        p = p->next;
    }
   
}
void free_list(linklist p)//释放空间
{
   linklist k, q;
   k = p;
   while(p != NULL && p->next != NULL)
   {
       q = p->next;
       p->next = q->next;
       free(q);
   }
}

void main()
{
    linklist pa, pb, pc;
   
    printf("请输入第一个多项式:\n");
    pa = create_list();
    Select_sort(pa);
    merge_list(pa);
    printf("排序化简后的多项式为:\n");
    printf("                      \n");
    result_out(pa);
    printf("\n                    \n\n");
   
    printf("请输入第二个多项式:\n");
    pb = create_list();
    Select_sort(pb);
    merge_list(pb);
    printf("排序化简后的多项式为:\n");
    printf("                       \n");
    result_out(pb);
    printf("\n                     \n\n");
   
    printf("相加后的多项式为:\n");
    printf("                       \n");
    pc = add_list(pa,pb);
    result_out(pc);
    printf("\n                      \n\n");


   free_list(pa);
   free(pa);
   free_list(pb);
   free(pb);
   free_list(pc);
   free(pc);
}
搜索更多相关主题的帖子: include 多项式 
2016-06-02 10:31
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:20 
程序代码:
#include <stdio.h>
#include <malloc.h>

typedef struct
{
    int cff;//系数
    int exp;//指数
} datatype;

typedef struct Node
{
    datatype data_1;
    struct Node *next;
}listnode;//自定义struct数据类型为linklist,而不是定义linklist,所以不能在这里定义pa,pb

typedef listnode* linklist;

linklist create_list()//创建带头结点的链表,返回头结点,尾插法
{
    linklist head = (linklist)malloc(sizeof(listnode));
    linklist  p, r;
    int xs;
    int zs;
    r = head;
    while (scanf("%d%d", &xs, &zs)
        && xs != 0
        || zs != 0)//输入00是结束
    {
        p = (linklist)malloc(sizeof(listnode));
        p->data_1.cff = xs;
        p->data_1.exp = zs;
        r->next = p;
        r = p;
    }
    r->next = NULL;
    return (head);
}

void Select_sort(linklist l)//选择排序,由高到低
{
    linklist p, r, q;
    r = l->next;
    p = (linklist)malloc(sizeof(listnode));
    while (r != NULL)
    {
        q = r->next;
        while (q != NULL)
        {
            if (q->data_1.exp > r->data_1.exp)
            {
                p->data_1.cff = r->data_1.cff;
                p->data_1.exp = r->data_1.exp;
                r->data_1.cff = q->data_1.cff;
                r->data_1.exp = q->data_1.exp;
                q->data_1.cff = p->data_1.cff;
                q->data_1.exp = p->data_1.exp;
            }
            q = q->next;
        }
        r = r->next;
    }
}

void merge_list(linklist l)//合并同类项
{
    linklist r, q, p;
    r = l;
    while (r != NULL && r->next != NULL)
    {
        q = r->next;
        while (q->next != NULL && q != NULL
            && (q->data_1.exp == q->next->data_1.exp))
        {
            p = q->next;
            q->data_1.cff += p->data_1.cff;
            q->next = p->next;
            free(p);
        }
        r = r->next;
    }

    //清楚系数为0的项
    r = l;
    while (r != NULL && r->next != NULL)
    {
        if (r->next->data_1.cff == 0)
        {
            q = r->next;
            r->next = q->next;
            free(q);
        }
        r = r->next;
    }
}

linklist add_list(linklist l_fir, linklist l_sec)//表一表二相加并将结果存放在表三中
{
    linklist head = (linklist)malloc(sizeof(listnode));
    linklist p, q, r, s;
    r = head;
    p = l_fir->next;
    q = l_sec->next;
    while (p != NULL && q != NULL)//表一表二中均有数据
    {
        s = (linklist)malloc(sizeof(listnode));
        if (p->data_1.exp == q->data_1.exp)
        {

            s->data_1.cff = p->data_1.cff + q->data_1.cff;
            s->data_1.exp = p->data_1.exp;
            p = p->next;
            q = q->next;
        }
        else if (p->data_1.exp > q->data_1.exp)
        {
            s->data_1.cff = p->data_1.cff;
            s->data_1.exp = p->data_1.exp;
            p = p->next;
        }
        else if (p->data_1.exp < q->data_1.exp)
        {
            s->data_1.cff = q->data_1.cff;
            s->data_1.exp = q->data_1.exp;
            q = q->next;
        }
        r->next = s;
        r = s;
    }
    while (p != NULL)//仅表一中有数据
    {
        s = (linklist)malloc(sizeof(listnode));
        s->data_1.cff = p->data_1.cff;
        s->data_1.exp = p->data_1.exp;
        p = p->next;
        r->next = s;
        r = s;
    }
    while (q != NULL)//仅表二中有数据
    {
        s = (linklist)malloc(sizeof(listnode));
        s->data_1.cff = q->data_1.cff;
        s->data_1.exp = q->data_1.exp;
        q = q->next;
        r->next = s;
        r = s;
    }
    r->next = NULL;

    //清楚系数为0的项
    r = head;
    while (r != NULL && r->next != NULL)
    {
        if (r->next->data_1.cff == 0)
        {
            q = r->next;
            r->next = q->next;
            free(q);
        }
        r = r->next;
    }
    return (head);
}

linklist del_list(linklist l_fir, linklist l_sec)//表一表二相减并将结果存放在表三中
{
    linklist head = (linklist)malloc(sizeof(listnode));
    linklist p, q, r, s;
    r = head;
    p = l_fir->next;
    q = l_sec->next;
    while (p != NULL && q != NULL)//表一表二中均有数据
    {
        s = (linklist)malloc(sizeof(listnode));
        if (p->data_1.exp == q->data_1.exp)
        {

            s->data_1.cff = p->data_1.cff - q->data_1.cff;
            s->data_1.exp = p->data_1.exp;
            p = p->next;
            q = q->next;
        }
        else if (p->data_1.exp > q->data_1.exp)
        {
            s->data_1.cff = p->data_1.cff;
            s->data_1.exp = p->data_1.exp;
            p = p->next;
        }
        else if (p->data_1.exp < q->data_1.exp)
        {
            s->data_1.cff = q->data_1.cff;
            s->data_1.exp = q->data_1.exp;
            q = q->next;
        }
        r->next = s;
        r = s;
    }
    while (p != NULL)//仅表一中有数据
    {
        s = (linklist)malloc(sizeof(listnode));
        s->data_1.cff = p->data_1.cff;
        s->data_1.exp = p->data_1.exp;
        p = p->next;
        r->next = s;
        r = s;
    }
    while (q != NULL)//仅表二中有数据
    {
        s = (linklist)malloc(sizeof(listnode));
        s->data_1.cff = q->data_1.cff;
        s->data_1.exp = q->data_1.exp;
        q = q->next;
        r->next = s;
        r = s;
    }
    r->next = NULL;

    //清楚系数为0的项
    r = head;
    while (r != NULL && r->next != NULL)
    {
        if (r->next->data_1.cff == 0)
        {
            q = r->next;
            r->next = q->next;
            free(q);
        }
        r = r->next;
    }
    return (head);
}


void result_out(linklist p)//输出结果
{
    linklist k;
    k = p;
    if (p->next == NULL)
        printf("empty list\n");
    while (p->next != NULL)
    {
        if (p->next->data_1.cff < 0)
        {
            if (p == k)
                printf("-");
            else
                printf("- ");
            printf("%d^%d ", -p->next->data_1.cff, p->next->data_1.exp);
        }
        else if (p != k && p->next->data_1.cff > 0)
        {
            printf("+ ");
            printf("%d^%d ", p->next->data_1.cff, p->next->data_1.exp);
        }
        else if (p == k && p->next->data_1.cff > 0)
        {
            printf("%d^%d ", p->next->data_1.cff, p->next->data_1.exp);
        }
        p = p->next;
    }

}
void free_list(linklist p)//释放空间
{
    linklist k, q;
    k = p;
    while (p != NULL && p->next != NULL)
    {
        q = p->next;
        p->next = q->next;
        free(q);
    }
}

void main()
{
    linklist pa, pb, pc,pd;

    printf("请输入第一个多项式:\n");
    pa = create_list();
    Select_sort(pa);
    merge_list(pa);
    printf("排序化简后的多项式为:\n");
    printf("                      \n");
    result_out(pa);
    printf("\n                    \n\n");

    printf("请输入第二个多项式:\n");
    pb = create_list();
    Select_sort(pb);
    merge_list(pb);
    printf("排序化简后的多项式为:\n");
    printf("                       \n");
    result_out(pb);
    printf("\n                     \n\n");

    printf("相加后的多项式为:\n");
    printf("                       \n");
    pc = add_list(pa, pb);
    result_out(pc);
    printf("\n                      \n\n");

    printf("相减后的多项式为:\n");
    printf("                       \n");
    pd = del_list(pa, pb);
    result_out(pd);
    printf("\n                      \n\n");

    free_list(pa);
    free(pa);
    free_list(pb);
    free(pb);
    free_list(pc);
    free(pc);
    free_list(pd);
    free(pd);
}
收到的鲜花
  • cainiao一枚2016-06-03 21:34 送鲜花  3朵   附言:我很赞同 谢谢
2016-06-03 13:24
cainiao一枚
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-5-29
收藏
得分:0 
回复 2楼 grmmylbs
相减编的有点问题
2016-06-03 21:51
快速回复:数据结构 求补充完整一元多项式减法的运算
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031810 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved