(easy)一元多项式表示
#include<iostream>
#include<cmath>
using namespace std;
struct node{
int coef; //系数
int expo; //指数
node *next;
};
int main()
{
cout<<"几项呀? \n";
int n; cin>>n;
node *now,*pre;
node *head = new node;
pre=head;
for(int i=1;i<=n;i++) //录入
{ int a,b;
cout<<"第"<<i<<"项系数为:" ;cin>>a; cout<<endl;
cout<<"第"<<i<<"项系数为:" ;cin>>b; cout<<endl;
now=new node;
now->coef=a;
now->expo=b;
pre->next=now;
pre=now;
}
cout<<"输出表达式... \n";
pre=head;
for(int i=1;i<=n;i++)
{
pre=pre->next;
if(pre->expo==0) cout<<pre->coef;
else cout<<pre->coef<<"x^"<<pre->expo;
if(pre->next->coef>0&&i!=n) cout<<"+";
} cout<<endl;
int x; cout<<"键入x值: "; cin>>x; cout<<"收到\n";
cout<<"计算表达式... \n";
int num=0;
pre=head;
for(int i=1;i<=n;i++)
{
pre=pre->next;
num+=pre->coef*pow(x,pre->expo);
}
cout<<"值是:"<<num<<endl;
//还可以增加求导功能,或是直接读入表达式进行计算巴拉巴拉,小可爱们自己去尝试吧
}
(hard)(读入不是太会,不想写)
题33:二元多项式基本运算
选择合适的存储结构表示二元多项式,并实现基本的加减运算
要求:
1)二元多项式的输入采用如下方式进行键盘输入
(5y^2+7)x^4 + (3y^4+2y+9)x^2 + (2y^2+y)x + (y+9)。
2)按照键盘输入的方式进行结果输出。
3)要求输出结果的升幂和降幂两种排列情况
这题找这位学长吧,他的课设