(C++)多项式加减法(一元)

(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)要求输出结果的升幂和降幂两种排列情况

这题找这位学长吧,他的课设

https://blog.csdn.net/kyl666/article/details/75158223

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值