#include
#include
typedef struct polynomial{ //项的数据类型
float coef; //系数
int expn; //指数
}polynomial,*polynomialptr; //类型名
typedef struct polynode{ //一元多项式的结点,即项
polynomial data; //数据域为polynomial型
struct Node *next; //指针域指向下一个项结点
}polynode,*polynodeptr; //结点类型名
polynodeptr producehead(polynodeptr head); //建立头结点
void creatpolynomial(polynodeptr head,polynomial n,int m); //生成一元多项式
void Display(polynodeptr head); //显示一元多项式
void addpolynomial(polynodeptr head1,polynodeptr head2,int m1,int m2); //两个多项式相加
void minuspolynomial(polynodeptr head1,polynodeptr head2,int m1,int m2); //两个多项式相减
//void multiplypolynomial(polynodeptr head1,polynodeptr head2,int m1,int m2); //两个多项式相乘
int main()
{
polynodeptr head1,head2; //head为头结点,是全局变量
polynomial p1,p2;
int m1,m2; //m1,m2分别为两个多项式的最大指数
head1=producehead(head1);
head2=producehead(head2);
printf("请输入第一个一元多项式的最大指数:");
scanf("%d",&m1);
creatpolynomial(head1,p1,m1);
printf("\n");
Display(head1);
printf("\n\n请输入第二个一元多项式的最大指数:");
scanf("%d",&m2);
creatpolynomial(head2,p2,m2);
printf("\n");
Display(head2);
printf("\n\n");
int n;
printf("请问要执行什么运算?\n执行加法运算请输入1\n执行减法运算请输入2\n执行加法运算请输入3\n");
scanf("%d",&n);
printf("\n");
while(n<4&&n>0)
{
switch(n)
{
case 1:{addpolynomial(head1,head2,m1,m2);printf("\n\n");break;}
case 2:{minuspolynomial(head1,head2,m1,m2);printf("\n\n");break;}
//case 3:{multiplypolynomial(head1,head2,m1,m2);printf("\n\n");break;}
}
getch();
printf("请问要执行什么运算?\n执行加法运算请输入1\n执行减法运算请输入2\n执行乘法运算请输入3\n");
scanf("%d",&n);
printf("\n");
}
return 0;
}
polynodeptr producehead(polynodeptr head){ //建立头结点
head=(polynodeptr)malloc(sizeof(polynode));
head->next=NULL;
return head;
}
void creatpolynomial(polynodeptr head,polynomial n,int m){ //生成一元多项式
int i;
for(i=0;i<=m;i++){
printf("请输入该一元多项式的指数为%d的项的指数与对应系数:",m-i);
scanf("%d%f",&n.expn,&n.coef);
polynodeptr p;
p=(polynodeptr)malloc(sizeof(polynode));
p->data=n;
p->next=head->next;
head->next=p;
}
}
void Display(polynodeptr head) //显示一元多项式
{
head=head->next;
if(head==NULL)
{
printf("无多项式\n");return;
}
printf("%.3fX(%d)",head->data.coef,head->data.expn); //处理首项无+的问题
head=head->next;
while(head)
{
if(head&&head->data.coef>0) printf("+");
if(head->data.coef!=0) printf("%.3fX(%d)",head->data.coef,head->data.expn);
head=head->next;
}
}
void addpolynomial(polynodeptr head1,polynodeptr head2,int m1,int m2){ //两个多项式相加
int i;
float coef;//系数
head1=head1->next;
head2=head2->next;
if(m1==m2){//m1,m2分别为两个多项式的最大指数
for(i=0;i<=m1;i++){
coef=head1->data.coef+head2->data.coef;
if(i!=0&&coef>0) printf("+");
if(coef!=0) printf("%.3fX(%d)",coef,i);
head1=head1->next;
head2=head2->next;
}
}
if(m1
for(i=0;i
coef=head1->data.coef+head2->data.coef;
if(i!=0&&coef>0) printf("+");
if(coef!=0) printf("%.3fX(%d)",coef,i);
head1=head1->next;
head2=head2->next;
}
for(;i<=m2;i++){
if(i!=(m2-1)&&head2->data.coef>0) printf("+");
if(head2->data.coef!=0) printf("%.3fX(%d)",head2->data.coef,i);
head2=head2->next;
}
}
if(m1>m2){
for(i=0;i<=m2;i++){
coef=head1->data.coef+head2->data.coef;
if(i!=0&&coef>0) printf("+");
if(coef!=0) printf("%.3fX(%d)",coef,i);
head1=head1->next;
head2=head2->next;
}
for(;i<=m1;i++){
if(i!=(m1-1)&&head1->data.coef>0) printf("+");
if(head1->data.coef!=0) printf("%.3fX(%d)",head1->data.coef,i);
head1=head1->next;
}
}
}
void minuspolynomial(polynodeptr head1,polynodeptr head2,int m1,int m2){ //两个多项式相减
int i;
float coef;
head1=head1->next;
head2=head2->next;
if(m1==m2){
for(i=0;i<=m1;i++){
coef=head1->data.coef-head2->data.coef;
if(i!=0&&coef>0) printf("+");
if(coef!=0) printf("%.3fX(%d)",coef,i);
head1=head1->next;
head2=head2->next;
}
}
if(m1
for(i=0;i
coef=head1->data.coef-head2->data.coef;
if(i!=0&&coef>0&&i!=m1) printf("+");
if(coef!=0) printf("%.3fX(%d)",coef,i);
head1=head1->next;
head2=head2->next;
}
for(;i<=m2;i++){
if(i!=(m2-1)&&head2->data.coef>0) printf("-");
if(head2->data.coef!=0) printf("%.3fX(%d)",head2->data.coef,i);
head2=head2->next;
}
}
if(m1>m2){
for(i=0;i<=m2;i++){
coef=head1->data.coef-head2->data.coef;
if(i!=0&&coef>0&&i!=m2) printf("+");
if(coef!=0) printf("%.3fX(%d)",coef,i);
head1=head1->next;
head2=head2->next;
}
for(;i<=m1;i++){
if(i!=(m1-1)&&head1->data.coef>0) printf("+");
if(head1->data.coef!=0) printf("%.3fX(%d)",head1->data.coef,i);
head1=head1->next;
}
}
}