数据结构课程设计:顺序结构、动态链表结构下的一元多项式的加法、减法、乘法的实现
原来做的数据结构课程设计,今天整理资料时偶然发现了,自己留着没啥意思,共享一下吧,互相交流学习
要求
设有一元多项式Am(x)和Bn(x).
Am(x)=A0+A1x1+A2x2+A3x3+… +Amxm
Bn(x)=B0+B1x1+B2x2+B3x3+… +Bnxn
请实现求M(x)= Am(x)+Bn(x)、M(x)= Am(x)-Bn(x)和M(x)= Am(x)×Bn(x)。
要求:
1)首先判定多项式是否稀疏
2)分别采用顺序和动态存储结构实现;
3)结果M(x)中无重复阶项和无零系数项;
4)要求输出结果的升幂和降幂两种排列情况
算法分析:
以后再写。
代码实现:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct data{
int xishu;
int zhishu;
};
struct node{
data dat;
node *next;
};
int cmp(const void *a,const void *b)
{
return ((data*)a)->zhishu-((data*)b)->zhishu;
}
void output(node *head)
{
node *p;
node *shengxu;
shengxu=new node;
shengxu->next=NULL;
printf("升序排列\n");
p=head->next;
while(p)
{
if(p->dat.xishu<0)
printf("\b");
if(p->dat.xishu!=0)
{
if(p->dat.xishu==1)
printf("X^%d+",p->dat.zhishu);
else if(p->dat.xishu==-1)
printf("-X^%d+",p->dat.zhishu);
else
printf("%dX^%d+",p->dat.xishu,p->dat.zhishu);
}
node *q;
q=new node;
q->dat.zhishu=p->dat.zhishu;
q->dat.xishu=p->dat.xishu;
q->next=shengxu->next;
shengxu->next=q;
p=p->next;
}
printf("\b \n");
printf("降序排列\n");
p=shengxu->next;
while(p)
{
if(p->dat.xishu<0)
printf("\b");
if(p->dat.xishu!=0)
{
if(p->dat.xishu==1)
printf("X^%d+",p->dat.zhishu);
else if(p->dat.xishu==-1)
printf("-X^%d+",p->dat.zhishu);
else
printf("%dX^%d+",p->dat.xishu,p->dat.zhishu);
}
p=p->next;
}
printf("\b \n");
}
int main()
{
int sum1,sum2;
data *d1,*d2;
int zhishu_max1=-1,zhishu_max2=-1;
int i,j;
int *biaodashi1,*biaodashi2;
node *he,*cha;
node *head1,*head2,*tail1,*tail2;
head1=(node *)malloc(sizeof(node));
head2=(node *)malloc(sizeof(node));
head1->next=NULL;
head2->next=NULL;
tail1=head1;
tail2=head2;
//输入第一个表达式
printf("请输入第一个多项式的项数:");
scanf("%d",&sum1);
d1=(data*)malloc(sum1*sizeof(data));
printf("请依次输入每项多项式的系数和指数:\n");
for(i=0;i<sum1;i++)
{
scanf("%d%d",&d1[i].xishu,&d1[i].zhishu);
}
//输入第二个表达式
printf("请输入第二个多项式的项数:");
scanf("%d",&sum2);
d2=(data*)malloc(sum2*sizeof(data));
printf("请依次输入每项多项式的系数和指数:\n");
for(i=0;i<sum2;i++)
{
scanf("%d%d",&d2[i].xishu,&d2[i].zhishu);
}
//排序
qsort(d1,sum1,sizeof(data),cmp);
qsort(d2,sum2,sizeof(data),cmp);
zhishu_max1=d1[sum1-1].zhishu;
zhishu_max2=d2[sum2-1].zhishu;
for(i=0;i<30;i++)
printf("==");
printf("\n");
//输出第一个表达式
printf("第一个表达式为:\n");
for(i=0;i<sum1;i++)
{
if(d1[i].xishu!=0)
{
if(d1[i].xishu==1)
printf("X^%d",d1[i].zhishu);
else if(d1[i].xishu==-1)
printf("-X^%d",d1[i].zhishu);
else
printf("%dX^%d",d1[i].xishu,d1[i].zhishu);
}
if(i<sum1-1)
printf("+");
}
printf("\n");
//判断是否稀疏
if(zhishu_max1-d1[0].zhishu>(sum1*sizeof(node)))
{
printf("第一个表达式稀疏\n\n");
for(i=0;i<sum1;i++)
{
node *p;
p=(node*)malloc(sizeof(node));
p->dat.zhishu=d1[i].zhishu;
p->dat.xishu=d1[i].xishu;
tail1->next=p;
p->next=NULL;
tail1=p;
}
}
else
{
printf("第一个表达式稠密!\n\n");
biaodashi1=(int*)malloc(zhishu_max1*sizeof(int));
j=0;
for(i=0;i<=zhishu_max1;i++)
{
if(i==d1[j].zhishu){
biaodashi1[i] =d1[j].xishu;
j++;
}
else
biaodashi1[i]=0;
}
}
//输出第二个表达式
printf("第二个表达式为:\n");
for(i=0;i<sum2;i++)
{
if(d2[i].xishu!=0)
{
if(d2[i].xishu==1)
printf("X^%d",d2[i].zhishu);
else if(d2[i].xishu==-1)
printf("X^%d",d2[i].zhishu);
else
printf("%dX^%d",d2[i].xishu,d2[i].zhishu);
}
if(i<sum2-1)
printf("+");
}
printf("\n");
//判断是否稀疏
if(zhishu_max2-d2[0].zhishu>(sum2*sizeof(node)))
{
printf("第一个表达式稀疏\n\n");
for(i=0;i<sum2;i++)
{
node *p;
p=(node*)malloc(sizeof(node));
p->dat.zhishu=d2[i].zhishu;
p->dat.xishu=d2[i].xishu;
tail2->next=p;
p->next=NULL;
tail2=p;
}
}
else
{
printf("第二个表达式稠密!\n\n");
biaodashi2=(int*)malloc(zhishu_max2*sizeof(int));
j=0;
for(i=0;i<=zhishu_max2;i++)
{
if(i==d2[j].zhishu){
biaodashi2[i]=d2[j].xishu;
j++;
}
else
biaodashi2[i]=0;
}
}
//求两个多项式的和和差
he=new node;
cha=new node;
he->next=NULL;
cha->next=NULL;
node *cha_tail,*he_tail;
he_tail=he;
cha_tail=cha;
for(i=0,j=0;i<sum1&&j<sum2;)
{
node *p,*p2;
p=new node;
p2=new node;
if(d1[i].zhishu<d2[j].zhishu)
{
p->dat.zhishu=d1[i].zhishu;
p->dat.xishu=d1[i].xishu;
p2->dat.zhishu=d1[i].zhishu;
p2->dat.xishu=d1[i].xishu;
i++;
}
else if(d1[i].zhishu>d2[j].zhishu)
{
p->dat.zhishu=d2[j].zhishu;
p->dat.xishu=d2[j].xishu;
p2->dat.zhishu=d2[j].zhishu;
p2->dat.xishu=-d2[j].xishu;
j++;
}
else
{
p->dat.zhishu=d1[i].zhishu;
p->dat.xishu=d1[i].xishu+d2[j].xishu;
p2->dat.zhishu=d1[i].zhishu;
p2->dat.xishu=d1[i].xishu-d2[j].xishu;
i++;
j++;
}
he_tail->next=p;
p->next=NULL;
he_tail=p;
cha_tail->next=p2;
p2->next=NULL;
cha_tail=p2;
}
//将剩余的项加到表达式中
if(sum1<sum2)
{
for(;j<sum2;j++)
{
node *p,*p2;
p=new node;
p2=new node;
p->dat.zhishu=d2[j].zhishu;
p->dat.xishu=d2[j].xishu;
p2->dat.zhishu=d2[j].zhishu;
p2->dat.xishu=-d2[j].xishu;
he_tail->next=p;
p->next=NULL;
he_tail=p;
cha_tail->next=p2;
p2->next=NULL;
cha_tail=p2;
}
}
else if(sum1>sum2)
{
j=i;
for(;j<sum1;j++)
{
node *p,*p2;
p=new node;
p2=new node;
p->dat.zhishu=d2[j].zhishu;
p->dat.xishu=d2[j].xishu;
p2->dat.zhishu=d2[j].zhishu;
p2->dat.xishu=d2[j].xishu;
he_tail->next=p;
p->next=NULL;
he_tail=p;
cha_tail->next=p2;
p2->next=NULL;
cha_tail=p2;
}
}
//输出两个表达式的和
printf("两个表达式的和为:\n");
output(he);
//输出两个表达式的差
printf("\n两个表达式的差为:\n");
output(cha);
//表达式乘法
node *chengfa;
chengfa = new node;
chengfa -> next = NULL;
for(i=0;i<sum1;i++)
{
for(j=0;j<sum2;j++)
{
node *p;
p = new node;
p -> dat.zhishu = d1[i].zhishu + d2[j].zhishu;
p -> dat.xishu = d1[i].xishu * d2[j].xishu;
p->next=NULL;
node *q;
q = chengfa;
while(q->next)
{
if(q->next->dat.zhishu>p->dat.zhishu)
{
p->next=q->next;
q->next=p;
break;
}
else if(q->next->dat.zhishu==p->dat.zhishu)
{
q->next->dat.xishu+=p->dat.xishu;
break;
}
q=q->next;
}
if(q->next==NULL)
{
p->next=q->next;
q->next=p;
}
}
}
printf("\n两个表达式的乘积为:\n");
output(chengfa);
for(i=0;i<30;i++)
printf("==");
}
#include<stdlib.h>
struct data{
int xishu;
int zhishu;
};
struct node{
data dat;
node *next;
};
int cmp(const void *a,const void *b)
{
return ((data*)a)->zhishu-((data*)b)->zhishu;
}
void output(node *head)
{
node *p;
node *shengxu;
shengxu=new node;
shengxu->next=NULL;
printf("升序排列\n");
p=head->next;
while(p)
{
if(p->dat.xishu<0)
printf("\b");
if(p->dat.xishu!=0)
{
if(p->dat.xishu==1)
printf("X^%d+",p->dat.zhishu);
else if(p->dat.xishu==-1)
printf("-X^%d+",p->dat.zhishu);
else
printf("%dX^%d+",p->dat.xishu,p->dat.zhishu);
}
node *q;
q=new node;
q->dat.zhishu=p->dat.zhishu;
q->dat.xishu=p->dat.xishu;
q->next=shengxu->next;
shengxu->next=q;
p=p->next;
}
printf("\b \n");
printf("降序排列\n");
p=shengxu->next;
while(p)
{
if(p->dat.xishu<0)
printf("\b");
if(p->dat.xishu!=0)
{
if(p->dat.xishu==1)
printf("X^%d+",p->dat.zhishu);
else if(p->dat.xishu==-1)
printf("-X^%d+",p->dat.zhishu);
else
printf("%dX^%d+",p->dat.xishu,p->dat.zhishu);
}
p=p->next;
}
printf("\b \n");
}
int main()
{
int sum1,sum2;
data *d1,*d2;
int zhishu_max1=-1,zhishu_max2=-1;
int i,j;
int *biaodashi1,*biaodashi2;
node *he,*cha;
node *head1,*head2,*tail1,*tail2;
head1=(node *)malloc(sizeof(node));
head2=(node *)malloc(sizeof(node));
head1->next=NULL;
head2->next=NULL;
tail1=head1;
tail2=head2;
//输入第一个表达式
printf("请输入第一个多项式的项数:");
scanf("%d",&sum1);
d1=(data*)malloc(sum1*sizeof(data));
printf("请依次输入每项多项式的系数和指数:\n");
for(i=0;i<sum1;i++)
{
scanf("%d%d",&d1[i].xishu,&d1[i].zhishu);
}
//输入第二个表达式
printf("请输入第二个多项式的项数:");
scanf("%d",&sum2);
d2=(data*)malloc(sum2*sizeof(data));
printf("请依次输入每项多项式的系数和指数:\n");
for(i=0;i<sum2;i++)
{
scanf("%d%d",&d2[i].xishu,&d2[i].zhishu);
}
//排序
qsort(d1,sum1,sizeof(data),cmp);
qsort(d2,sum2,sizeof(data),cmp);
zhishu_max1=d1[sum1-1].zhishu;
zhishu_max2=d2[sum2-1].zhishu;
for(i=0;i<30;i++)
printf("==");
printf("\n");
//输出第一个表达式
printf("第一个表达式为:\n");
for(i=0;i<sum1;i++)
{
if(d1[i].xishu!=0)
{
if(d1[i].xishu==1)
printf("X^%d",d1[i].zhishu);
else if(d1[i].xishu==-1)
printf("-X^%d",d1[i].zhishu);
else
printf("%dX^%d",d1[i].xishu,d1[i].zhishu);
}
if(i<sum1-1)
printf("+");
}
printf("\n");
//判断是否稀疏
if(zhishu_max1-d1[0].zhishu>(sum1*sizeof(node)))
{
printf("第一个表达式稀疏\n\n");
for(i=0;i<sum1;i++)
{
node *p;
p=(node*)malloc(sizeof(node));
p->dat.zhishu=d1[i].zhishu;
p->dat.xishu=d1[i].xishu;
tail1->next=p;
p->next=NULL;
tail1=p;
}
}
else
{
printf("第一个表达式稠密!\n\n");
biaodashi1=(int*)malloc(zhishu_max1*sizeof(int));
j=0;
for(i=0;i<=zhishu_max1;i++)
{
if(i==d1[j].zhishu){
biaodashi1[i] =d1[j].xishu;
j++;
}
else
biaodashi1[i]=0;
}
}
//输出第二个表达式
printf("第二个表达式为:\n");
for(i=0;i<sum2;i++)
{
if(d2[i].xishu!=0)
{
if(d2[i].xishu==1)
printf("X^%d",d2[i].zhishu);
else if(d2[i].xishu==-1)
printf("X^%d",d2[i].zhishu);
else
printf("%dX^%d",d2[i].xishu,d2[i].zhishu);
}
if(i<sum2-1)
printf("+");
}
printf("\n");
//判断是否稀疏
if(zhishu_max2-d2[0].zhishu>(sum2*sizeof(node)))
{
printf("第一个表达式稀疏\n\n");
for(i=0;i<sum2;i++)
{
node *p;
p=(node*)malloc(sizeof(node));
p->dat.zhishu=d2[i].zhishu;
p->dat.xishu=d2[i].xishu;
tail2->next=p;
p->next=NULL;
tail2=p;
}
}
else
{
printf("第二个表达式稠密!\n\n");
biaodashi2=(int*)malloc(zhishu_max2*sizeof(int));
j=0;
for(i=0;i<=zhishu_max2;i++)
{
if(i==d2[j].zhishu){
biaodashi2[i]=d2[j].xishu;
j++;
}
else
biaodashi2[i]=0;
}
}
//求两个多项式的和和差
he=new node;
cha=new node;
he->next=NULL;
cha->next=NULL;
node *cha_tail,*he_tail;
he_tail=he;
cha_tail=cha;
for(i=0,j=0;i<sum1&&j<sum2;)
{
node *p,*p2;
p=new node;
p2=new node;
if(d1[i].zhishu<d2[j].zhishu)
{
p->dat.zhishu=d1[i].zhishu;
p->dat.xishu=d1[i].xishu;
p2->dat.zhishu=d1[i].zhishu;
p2->dat.xishu=d1[i].xishu;
i++;
}
else if(d1[i].zhishu>d2[j].zhishu)
{
p->dat.zhishu=d2[j].zhishu;
p->dat.xishu=d2[j].xishu;
p2->dat.zhishu=d2[j].zhishu;
p2->dat.xishu=-d2[j].xishu;
j++;
}
else
{
p->dat.zhishu=d1[i].zhishu;
p->dat.xishu=d1[i].xishu+d2[j].xishu;
p2->dat.zhishu=d1[i].zhishu;
p2->dat.xishu=d1[i].xishu-d2[j].xishu;
i++;
j++;
}
he_tail->next=p;
p->next=NULL;
he_tail=p;
cha_tail->next=p2;
p2->next=NULL;
cha_tail=p2;
}
//将剩余的项加到表达式中
if(sum1<sum2)
{
for(;j<sum2;j++)
{
node *p,*p2;
p=new node;
p2=new node;
p->dat.zhishu=d2[j].zhishu;
p->dat.xishu=d2[j].xishu;
p2->dat.zhishu=d2[j].zhishu;
p2->dat.xishu=-d2[j].xishu;
he_tail->next=p;
p->next=NULL;
he_tail=p;
cha_tail->next=p2;
p2->next=NULL;
cha_tail=p2;
}
}
else if(sum1>sum2)
{
j=i;
for(;j<sum1;j++)
{
node *p,*p2;
p=new node;
p2=new node;
p->dat.zhishu=d2[j].zhishu;
p->dat.xishu=d2[j].xishu;
p2->dat.zhishu=d2[j].zhishu;
p2->dat.xishu=d2[j].xishu;
he_tail->next=p;
p->next=NULL;
he_tail=p;
cha_tail->next=p2;
p2->next=NULL;
cha_tail=p2;
}
}
//输出两个表达式的和
printf("两个表达式的和为:\n");
output(he);
//输出两个表达式的差
printf("\n两个表达式的差为:\n");
output(cha);
//表达式乘法
node *chengfa;
chengfa = new node;
chengfa -> next = NULL;
for(i=0;i<sum1;i++)
{
for(j=0;j<sum2;j++)
{
node *p;
p = new node;
p -> dat.zhishu = d1[i].zhishu + d2[j].zhishu;
p -> dat.xishu = d1[i].xishu * d2[j].xishu;
p->next=NULL;
node *q;
q = chengfa;
while(q->next)
{
if(q->next->dat.zhishu>p->dat.zhishu)
{
p->next=q->next;
q->next=p;
break;
}
else if(q->next->dat.zhishu==p->dat.zhishu)
{
q->next->dat.xishu+=p->dat.xishu;
break;
}
q=q->next;
}
if(q->next==NULL)
{
p->next=q->next;
q->next=p;
}
}
}
printf("\n两个表达式的乘积为:\n");
output(chengfa);
for(i=0;i<30;i++)
printf("==");
}
博主ma6174对本博客文章(除转载的)享有版权,未经许可不得用于商业用途。转载请注明出处http://www.cnblogs.com/ma6174/
对文章有啥看法或建议,可以评论或发电子邮件到ma6174@163.com