用C++编一个一元多项式计算器 大虾们

用C++编一个一元多项式计算器 大虾们
设计一个稀疏多项式简单计算器
基本要求:
(1) 输入并分别建立多项式A和B.
(2) 输入输出多项式,输出形式为整数序列:
n,c1,e1,c2,e2……,
其中n是多项式的项数,ci和ei是第i项的系数和指数,序列按指数降序排列.
(3)完成两个多项式的相加、相减,并将结果输出.
测试数据:
(1)A+B A=3x14-8x8+6x2+2;
B=2x10+4x8+-6x2?
(2) A-B A=11x14+3x10+2x8+10x6+5 ;
B=2x14+3x8+5x6+7
(3) A+B A=x3+x1 ; B=-x3-x1
(4) A+B A=0 ; B=x7+x5+x3+x1
(5)A-B A=100x100+50x50+20x20+x ;
B=10x100+10x50+10x20+x
选作内容:
(1)多项式在x=1时的运算结果;
(2)求多项式A和B的乘积.
需要在VC++6.0上运行编译
goalknight 1年前 已收到1个回答 举报

tyty68ty 幼苗

共回答了28个问题采纳率:85.7% 举报

选做内容也给你做了,给我加分不?:)
VC++2008下编译运行通过,输入输出格式都是n,c1,e1,c2,e2……
#include
#include
#include
using namespace std;
typedef map PnType;
void readPn(PnType &pn)
{
int n,c,e;
scanf("%d",&n);
while (n--) {
scanf(",%d,%d",&c,&e);
pn[e] = c;
}
}
void writePn(const PnType &pn)
{
cout second;
}
void mulPn(const PnType &a,const PnType &b,PnType &c)
{
c.clear();
for (PnType::const_iterator it1 = a.begin(); it1 != a.end(); ++it1)
for (PnType::const_iterator it2 = b.begin(); it2 != b.end(); ++it2)
c[it1->first + it2->first] += it1->second * it2->second;
}
int calcX1(const PnType &a)
{
int n=0;
for (PnType::const_iterator it = a.begin(); it != a.end(); ++it)
n += it->second;
return n;
}
void main()
{
PnType A,B,C;
cout

1年前

7
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 16 q. 0.021 s. - webmaster@yulucn.com