一元多项式的C程序
我现在学的是数据结构,老师留个作业是C语言完成一元多项式的相加,麻烦各位能给我一个具体点的思路,如果可以最好给下程序的大概过程,2项相加或3项相加都可以,请注明。
//发个c++的 简陋点
#include <iostream>
#include <list>
#include <cstdlib>
using namespace std;
class Node
{
public:
int exp; //指数
int coef; //系数
Node():exp(0),coef(0){}
};
class Polynomial
{
private:
list<Node>first_poly; //第一个多项式
list<Node>second_poly; //第二个多项式
list<Node>result_poly; //储存结果
list<Node> MpAdd(list<Node>& t_first_poly,\
list<Node>& t_second_poly);
public:
void MultipPoly(); //多项式相乘
void AddPoly(); //多项式相加
void InPut(); //输入多项式
void OutPut(); //输出多项式
};
void Polynomial::InPut()
{
Node temp;
int n;
cout<<"\t***********多项式的加法和乘法***********"<<endl;
cout<<"请输入第一个多项式的项数"<<endl;
cin>>n;
cout<<"请按照降幂的顺序输入指数"<<endl;
for(int i=1;i<=n;i++)
{
cout<<"输入第"<<i<<"项的系数和指数"<<endl;
cin>>temp.coef>>temp.exp;
first_poly.push_back(temp);
}
n=0;
cout<<"请输入第二个多项式的项数"<<endl;
cin>>n;
cout<<"请按照降幂的顺序输入指数"<<endl;
for(int i=1;i<=n;i++)
{
cout<<"输入第"<<i<<"项的系数和指数"<<endl;
cin>>temp.coef>>temp.exp;
second_poly.push_back(temp);
}
}
void Polynomial::OutPut()
{
list<Node>::iterator iter=result_poly.begin();
for(;iter!=result_poly.end();)
{
Node temp=*iter;
cout<<temp.coef<<"x^"<<temp.exp;
if(++iter!=result_poly.end())
cout<<"+";
}
}
void Polynomial::AddPoly()
{
list<Node>::iterator fiter=first_poly.begin();
list<Node>::iterator siter=second_poly.begin();
while(fiter!=first_poly.end()&&siter!=second_poly.end())
{
Node temp;
Node ftemp=(Node)*fiter;
Node stemp=(Node)*siter;
if(ftemp.exp>stemp.exp)
{
result_poly.push_back(ftemp);
fiter++;
}
else if(ftemp.exp<stemp.exp)
{
result_poly.push_back(stemp);
siter++;
}
else { temp.coef=ftemp.coef+stemp.coef;
temp.exp=ftemp.exp+stemp.exp;
result_poly.push_back(temp);
fiter++;
siter++;
}
}
for(;fiter!=first_poly.end();fiter++)
{
result_poly.push_back(*fiter);
}
for(;siter!=second_poly.end();siter++)
{
result_poly.push_back(*siter);
}
}
void Polynomial::MultipPoly()
{
list<Node>::iterator fiter=first_poly.begin();
list<Node>temp_result_poly;
for(;fiter!=first_poly.end();fiter++)
{
list<Node>stemp_result_poly;
list<Node>::iterator siter=second_poly.begin();
for(;siter!=second_poly.end();siter++)
{
Node temp;
Node ftemp=(Node)*fiter;
Node stemp=(Node)*siter;
temp.coef=ftemp.coef*stemp.coef;
temp.exp=ftemp.exp+stemp.exp;
stemp_result_poly.push_back(temp);
}
temp_result_poly=MpAdd(stemp_result_poly,temp_result_poly);
}
result_poly=temp_result_poly;
}
list<Node> Polynomial::MpAdd(list<Node>& t_first_poly,\
list<Node>& t_second_poly)
{
list<Node>::iterator fiter=t_first_poly.begin();
list<Node>::iterator siter=t_second_poly.begin();
list<Node>temp_result_poly;
while(fiter!=t_first_poly.end()&&siter!=t_second_poly.end())
{
Node temp;
Node ftemp=(Node)*fiter;
Node stemp=(Node)*siter;
if(ftemp.exp>stemp.exp)
{
temp_result_poly.push_back(ftemp);
fiter++;
}
else if(ftemp.exp<stemp.exp)
{
temp_result_poly.push_back(stemp);
siter++;
}
else { temp.coef=ftemp.coef+stemp.coef;
temp.exp=ftemp.exp+stemp.exp;
temp_result_poly.push_back(temp);
fiter++;
siter++;
}
}
for(;fiter!=t_first_poly.end();fiter++)
{
temp_result_poly.push_back(*fiter);
}
for(;siter!=t_second_poly.end();siter++)
{
temp_result_poly.push_back(*fiter);
}
return temp_result_poly;
}
int main()
{
Polynomial poly_a;
poly_a.InPut();
poly_a.AddPoly();
cout<<"多项式加法的运算结果:"<<endl;
poly_a.OutPut();
cout<<endl;
poly_a.MultipPoly();
cout<<"多项式乘法的运算结果:"<<endl;
poly_a.OutPut();
system("pause");
return 0;
}