栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【c++练习题】c++数组实现多项式的加减法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【c++练习题】c++数组实现多项式的加减法

很久没写代码了偶然看到这么个题做了做。
题目要求是利用结构体数组创建一元多项式,并实现多项式的加减。
代码:

#include 
#include 

typedef struct
{
    float p;
    int e;
}ElemType;

static int CreatePolyn(ElemType p[],int m)
{// 创建一个多项式
    for(int i=0;i
        float p1;
        int e1;
        scanf("%f %d",&p1,&e1);
        p[i].p=p1;
        p[i].e=e1;
    }
    //sort
    for(int i=0;i
        for(int j=0;j
            if(p[i].e
                ElemType temp=p[i];
                p[i]=p[i+1];
                p[i+1]=temp;
            }
        }
    }
    return 0;
}

static int AddPolyn(ElemType pa[],int a,ElemType pb[],int b,ElemType pResult[])
{
    for(int i=0;i
        pResult[i]=pa[i];
    }
    for(int i=a;i
        pResult[i]=pb[i-a];
    }
    //合并同类项
    for(int i=0;i
        for(int j=i+1;j
            if(pResult[i].e==pResult[j].e)
            {
                pResult[j].p+=pResult[i].p;
                pResult[i].p=0;
                break;
            }
        }
    }
    // 再排序
    for (int i=0;i
        for (int j=0;j
            if(pResult[j].e
                ElemType temp=pResult[j];
                pResult[j]=pResult[j+1];
                pResult[j+1]=temp;
            }
        }
    }
    return 0;
}

static int SubtractPolyn(ElemType pa[],int a,ElemType pb[],int b,ElemType pResult[])
{//减法,把其中一个的p全部*-1然后相加
    ElemType pbSub[b];
    for(int i=0;i
        pbSub[i].p=pb[i].p*-1;
        pbSub[i].e=pb[i].e;
    }
    AddPolyn(pa,a,pbSub,b,pResult);
    return 0;
}

static int PrintPolyn(ElemType p[],int s)
{//格式化输出
    for(int i=0;i
        if(p[i].p!=0)
        {
            if(p[i].e!=0)
            {
                if(p[i].p>0)
                {
                    printf("+");
                }
                printf("%5.2fx^%d",p[i].p,p[i].e);
            }else
            {
                if(p[i].p>0)
                {
                    printf("+");
                }
                printf("%.2f",p[i].p);
            }
        }
    }
    printf("n");//换行
    return 0;
}

static int DestroyPolyn(ElemType p[])
{//释放
    int s=sizeof(p)/sizeof(p[0]);
    for(int i=0;i
        free(&p[i]);
    }
    return 0;
}

调用:

#include 
#include 
#include 
#include "polyn.c"
int main()
{
    ElemType a1;
    a1.e=1;
    a1.p=1.2;
    ElemType a2;
    a2.e=3;
    a2.p=45.4;
    ElemType a3;
    a3.e=8;
    a3.p=44.8;
    ElemType pa[]={a1,a2,a3};
    ElemType pb[2];
    int a=CreatePolyn(pb,2);
    if (a==0)
    {
        printf("create okn");
    }
    ElemType ap[2+3]={0};
    AddPolyn(pa,3,pb,2,ap);
    ElemType sp[2+3]={0};
    SubtractPolyn(pa,3,pb,2,sp);
    PrintPolyn(pa,3);
    PrintPolyn(pb,2);
    PrintPolyn(ap,5);
    PrintPolyn(sp,5);
    DestroyPolyn(pa);
    DestroyPolyn(pb);
    DestroyPolyn(ap);
    DestroyPolyn(sp);
}

输入:

-12.23 3
234.654 0

输出结果为:

-12.23 3
234.654 0
create ok
+ 1.20x^1+45.40x^3+44.80x^8
-12.23x^3+234.65
+44.80x^8+33.17x^3+ 1.20x^1+234.65
+44.80x^8+57.63x^3+ 1.20x^1-234.65

Process returned 0 (0x0)   execution time : 10.488 s
Press any key to continue.
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/960786.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号