📄 add.c
字号:
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct function)
struct function
{
int xishu;
int zhishu;
struct function *next;
};
struct function *input()
{
struct function *head,*p1,*p2;
int i,j;
head=(struct function*)malloc(LEN);
p1=head;
printf("请输入系数i=\n指数j=\n 以逗号分割,若系数为0,则表示输入结束\n");
scanf("%d,%d",&i,&j);
while(i!=0)
{
p2=(struct function*)malloc(LEN);
p2->xishu=i;
p2->zhishu=j;
p1->next=p2;
p1=p2;
printf("请输入系数i=\n指数j=\n若系数为0,则表示输入结束\n");
scanf("%d,%d",&i,&j);
}
p1->next=NULL;
head=head->next;
return(head);
}
struct function *add(struct function *head1,struct function *head2)
{
struct function *newhead,*pre,*q,*q1,*q2;
int addnumber;
q1=head1;
q2=head2;
newhead=(struct function*)malloc(LEN);
pre=newhead;
while(q1!=NULL&&q2!=NULL)
{
if(q1->zhishu==q2->zhishu)
{
addnumber=q1->xishu+q2->xishu;
if(addnumber!=0)
{
q=(struct function*)malloc(LEN);
q->xishu=addnumber;
q->zhishu=q1->zhishu;
pre->next=q;
pre=q;
}
q1=q1->next;
q2=q2->next;
}
else if(q1->zhishu<q2->zhishu)
{
q=(struct function*)malloc(LEN);
q->zhishu=q2->zhishu;
q->xishu=q2->xishu;
pre->next=q;
pre=q;
q2=q2->next;
}
else
{
q=(struct function*)malloc(LEN);
q->zhishu=q1->zhishu;
q->xishu=q1->xishu;
pre->next=q;
pre=q;
q1=q1->next;
}
}
while(q1!=NULL)
{
q=(struct function*)malloc(LEN);
q->zhishu=q1->zhishu;
q->xishu=q1->xishu;
pre->next=q;
pre=q;
q1=q1->next;
}
while(q2!=NULL)
{
q=(struct function*)malloc(LEN);
q->zhishu=q2->zhishu;
q->xishu=q2->xishu;
pre->next=q;
pre=q;
q2=q2->next;
}
pre->next=NULL;
q=newhead;
newhead=newhead->next;
free(q);
return(newhead);
}
void print(struct function *head)
{
struct function *p;
p=head;
if(head!=NULL)
{
do
{
printf("%3d,%3d;",p->xishu,p->zhishu );
p=p->next;
}while(p!=NULL);
}
}
void main()
{
struct function *heada,*headb,*headc;
heada=input();
printf("请输入下一多项式\n");
headb=input();
headc=add(heada,headb);
print(headc);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -